Presentation is loading. Please wait.

Presentation is loading. Please wait.

Ping Zhang 10/08/2010.  You can get data from the user (input) and display information to the user (output).  However, you must include the library.

Similar presentations


Presentation on theme: "Ping Zhang 10/08/2010.  You can get data from the user (input) and display information to the user (output).  However, you must include the library."— Presentation transcript:

1 Ping Zhang 10/08/2010

2  You can get data from the user (input) and display information to the user (output).  However, you must include the library file that contains functions needed for input and output operations.  #include  Includes the necessary printf and scanf functions.  The printf function is used to output text to the screen.  The scanf function is used to get input from the user.

3  The printf function  Syntax: printf (format string);  Example: printf (“This is a line of text”);  The format string is enclosed by double quotes “” and may contain any literal text, placeholders, and format control characters.  Literal text is interpreted literally by the compiler and doesn’t cause the program to perform any action.  Format control characters cause the program to perform and action like moving to the next line. i.Example: printf (“Move to the next line \n”); Format control character to create a new line.

4  The printf function continued…  The printf function can also take another form, which is used to display the contents of one or more variables.  Syntax: printf (format string, output list);  The output list consists of a list of variables or values that will replace the corresponding placeholder in the format string.  A placeholder always begins with the symbol % and is used to mark the display position for a variable’s value.  Example: printf (“The distance traveled is %d”, kms); printf (“The distance traveled is %d and cost of trip is %d”, kms, cost); Placeholder character to insert the value of variable kms.

5  The scanf function  Syntax: scanf (format string, input list);  Example: scanf (“%d”, &miles);  The format string is enclosed by double quotes “” and contains the data types of the input to read in. You need to use format specifiers to specify the data types.  The input list is a list of variables that will get values. The order of the variables in this list must correspond with the order of the format specifiers in the format string. i.The first variable in the input list will get the value of the first format specifier in the format string, the second variable in the input list gets the value of the second format specifier in the format string, etc. ii.Each variable must be declared before using them in the input list and you must put the ampersand (&) character in front of the variable’s name.

6  Placeholders – Format Specifiers %c – placeholder used for a char in printf and scanf. %d – placeholder used for an int in printf and scanf. %f – placeholder used for a double in printf. %lf – placeholder used for a long double in printf and scanf.  Special Characters – Format Control Characters \n – used to create a newline. \t – used to add a tab space to the output line. \\ – used to display a backslash in the output. \” – used to display a double quote output.

7 #include int main ( ) { int quantity; double price; printf (“Enter the quantity and price > “); scanf (“%d%lf”, &quantity, &price); return (0); }

8 #include int main ( ) { char first; char middle; char last; printf (“Enter the initials for your first, middle, and last names > “); scanf (“%c%c%c”, &first, &middle, &last); printf(“Hello there %c%c%c and welcome to this program.”, first, middle, last); return (0); }

9  In C, the output of all numbers will be in their normal notation unless you specify the format.  Format specifiers can have modifiers to control the total number of columns and decimal places to display.  Syntax: field_width.decimal_places  Example: %5.2f%5.2f %8.3f%8.3f %5d%5d

10  Example 1: printf (“%6.2f”, 4.9653); will display in 6 columns with 2 decimal places and 1 decimal point as ##4.97  Example 2: printf (“%.1f”, 4.9653); will display to 1 decimal place and 1 decimal point as 5.0  Example 3: printf (“%5.1f”, 25.3); will display in 5 columns with 1 decimal places and 1 decimal point as #25.3

11  Formatting Values of Type int: printf (“ The average of all exams is %5d”, average); ValueFormatOutput Display 234%5d 234 -234%5d -234 %6d -234 %1d-234 Note: C will expand the field width if it is too small for the integer value. *** From Table 2.11 on page 85 in our textbook.

12  Formatting Values of Type double: printf (“ The average of all exams is %5.2f”, average); ValueFormatOutput Display 85.123474%5.2f85.12 2.5421%4.2f2.54 3.14159%3.1f3.1 3.14159%5.1f 3.1 -.025%8.3 -0.025 2. 71828183%.4f2.7182 *** See Table 2.13 on page 87 in our textbook.


Download ppt "Ping Zhang 10/08/2010.  You can get data from the user (input) and display information to the user (output).  However, you must include the library."

Similar presentations


Ads by Google