1 / 12

Handling Input & Output

Handling Input & Output. Ping Zhang 10/08/2010. Simple Input & Output Operations. 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.

ivan
Download Presentation

Handling Input & Output

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Handling Input & Output Ping Zhang 10/08/2010

  2. Simple Input & Output Operations • 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 <stdio.h> • 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. Output Operations • 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. • Example: printf (“Move to the next line \n”); Format control character to create a new line.

  4. Output Operations • 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. Input Operations • 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. • 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. • 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. Format String Characters • 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. Printf & Scanf Example #include <stdio.h> int main ( ) { int quantity; double price; printf (“Enter the quantity and price > “); scanf (“%d%lf”, &quantity, &price); return (0); }

  8. Printf & Scanf Example #include <stdio.h> 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. Formatting Output • 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 %8.3f %5d

  10. Examples: Formatting Output • 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 Output • Formatting Values of Type int: printf (“ The average of all exams is %5d”, average);

  12. Formatting Output • Formatting Values of Type double: printf (“ The average of all exams is %5.2f”, average);

More Related