300 likes | 316 Views
Input/Output Operations & Functions. Chapter 9. Input/output functions provided by I/O library. Header file is stdio.h. Two general I/O functions: printf() for output, scanf() for input. Input/Output. Format: printf( format-control-string , print-list ); Example:
E N D
Input/Output Operations & Functions Chapter 9 Chapter 9: Input/Output Operations & Functions
Input/output functions provided by I/O library. Header file is stdio.h. Two general I/O functions: printf() for output, scanf() for input. Input/Output Input/Output
Format: printf(format-control-string, print-list); Example: printf("That equals %.2f centimetres.\n", cm); print-list format-control-string conversion specification escape sequence Formatting output: printf() Formatting output: printf()
printf() displays the value of its format control string after substituting in left-to-right order the value of the expressions in the print list for their conversion specifications in the format string and after replacing escape sequences by their meanings Formatting output: printf() Formatting output: printf()
Example: sum = 20; count = 30; printf("%d divided by %d equals %f\n", sum, count, (float)sum/count); Output: 20 divided by 3 equals 6.666667 Formatting output: printf() Formatting output: printf()
Conversion specification Conversion specification
Conversion specification Conversion specification
Conversion specification Conversion specification
Conversion specification Conversion specification
Field: place where an argument is displayed Field width: number of characters in field Field width and precision are specified between the % symbol and the conversion specifier. Example: %9.3f Display right justified if field width is larger than what the value requires. For numeric data, if field width is too small, it is ignored. Field Width & Precision Field Width & Precision
Field Width & Precision Field Width & Precision
Precision: for specifier d: minimum number of digits (default = 1) for e, E and f: number of digits after decimal point for g and G: maximum number of significant digits for s: maximum number of characters Field Width & Precision Field Width & Precision
Field Width & Precision Field Width & Precision
To provide more formatting features Flags Flags
Flags Flags
Flags Flags
Flags Flags
Flags Flags
Flags Flags
Non-printing characters: \n, \0. Escape sequences to represent special characters. Examples: \n, \t. Literals to appear in string: ", \, etc. Need to be escaped since " is used to delimit strings, and \ to start an escape sequence. Literals & Escape Sequences Literals & Escape Sequences
Literals & Escape Sequences Literals & Escape Sequences
Literals & Escape Sequences Literals & Escape Sequences
Format: printf(format-control-string, scan-list); Example: scanf("%2f", &inch); format-control-string scan-list Formatting input: scanf() Formatting input: scanf()
Conversion specification Conversion specification
Conversion specification Conversion specification
Conversion specification Conversion specification
Conversion specification Conversion specification
Field Width Field Width
Assignment Suppression Character Assignment Suppression Character
Try exercises behind chapter 9. Homework Homework