Unformatted text preview:

3. FORMATTED INPUT/OUTPUTThe printf FunctionSlide 3Conversion SpecificationsSlide 5printf ExampleEscape SequencesSlide 8The scanf FunctionSlide 10How scanf WorksSlide 12The scanf Format StringSlide 14Slide 153. FORMATTED INPUT/OUTPUTThe printf Function•The first argument in a call of printf is a string, which may contain both ordinary characters and conversion specifications, which begin with the % symbol.•Ordinary characters are printed as they appear in the string; conversion specifications are matched one-by-one with the remaining arguments in the call of printf:int i, j;float x, y;printf("i = %d, j = %d, x = %f, y = %f\n", i, j, x, y);The printf Function•Warning: C compilers don’t check that the number of conversion specifications in a format string matches the number of output items:printf("%d %d\n", i); /* too few items */printf("%d\n", i, j); /* too many items */•Furthermore, there’s no check that a conversion specification is appropriate for the type of item being printed:printf("%f %d\n", i, x);Conversion SpecificationsA conversion specification has the form %-m.pX• m (optional) specifies the minimum field width.• - (optional) indicates left justification within the field; the default is right justification.• .p (optional) specifies:–Number of digits after the decimal point (for a floating point number)–Minimum number of digits to print (for an integer)Conversion Specifications•X is one of the following letters:d - decimal signed integere - exponential floating pointf - fixed decimal floating pointg - whichever of e or f produces a shorter string•This is not a complete description of conversion specifications.printf Example• Sample program:#include <stdio.h>int main(void) { int i; float f; i = 40; f = 839.21; printf("|%d|%5d|%-5d|%5.3d|\n", i, i, i, i); printf("|%10.3f|%10.3e|%-10g|\n", f, f, f); return 0;}• Output:|40| 40|40 | 040|| 839.210| 8.392e+02|839.21 |Escape SequencesString literals sometimes need to contain control characters (such as tab and newline) or characters that have a special meaning in C (such as "). These characters can be represented by escape sequences.Character escapes consist of \ followed by one character: alert (bell) \a backslash \\ backspace \b question mark \? form feed \f single quote \' new-line \n double quote \" carriage return \r horizontal tab \t vertical tab \vEscape Sequences• Writing the \a escape causes a beep.• Writing the \n escape causes the cursor to move to the beginning of the next line.• Writing the \t escape causes the cursor to move to the next tab stop.• The \" escape allows us to print the double quote character: printf("\"Hello!\"");• String literals may contain any number of escape sequences:printf("Item\tUnit\tPurchase\n\tPrice\tDate\n");The scanf Function• scanf requires a format string, followed by variables into which input is to be stored. In most cases, each variable must be preceded by the symbol &.• Warning: Forgetting to put the & symbol in front of a variable will have unpredictable — and possibly disastrous — results.The scanf Function• scanf format strings are similar to printf format strings, but usually contain only conversion specifications: int i, j; float x, y; scanf("%d%d%f%f", &i, &j, &x, &y);• Note: When used with scanf, the e, f, and g conversions are identical: each causes scanf to read a floating-point number. A sign, exponent, and decimal point may or may not be present.How scanf Worksscanf ignores white-space characters (blanks, tabs, and new-line characters) when searching for an input item. If scanf is used in the following way:scanf("%d%d%f%f", &i, &j, &x, &y);the input could be split over several lines:1-20 .3-4.0e3or put on a single line:1 -20 .3 -4.0e3How scanf Works• When scanf encounters a character that can’t be part of the current item, this character is read again during the scanning of the next input item or during the next call of scanf. Using the same call of scanf as above:scanf("%d%d%f%f", &i, &j, &x, &y);If the input is 1-20.3-4.0e3zThen i is assigned the value 1, j is –20, x is .3, and y is –4.0e3.The scanf Format String• A scanf format string may contain ordinary characters in addition to conversion specifications.• A non-white-space character in a format string must match the next input character or scanf terminates without reading further. (The non-matching character can be read by a later call of scanf.) The callscanf("%d/%d/%d", &month, &day, &year);will read5/ 28/ 2002but not5 / 28 / 2002The scanf Format String• A white-space character in a format string matches zero or more white-space characters in the input. The callscanf("%d /%d /%d", &month, &day, &year);will read5 / 28 / 2002and all similar inputs, regardless of the amount of space before and after each / symbol.The scanf Format String Warning: Putting a new-line character at the end of a scanf format string is usually a bad idea: scanf("%d\n", &i);• After reading an integer, scanf skips white-space characters until it finds a non-white-space character. An interactive program will hang until the user enters a nonblank


View Full Document

UF CGS 3460 - FORMATTED INPUT OUTPUT

Download FORMATTED INPUT OUTPUT
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view FORMATTED INPUT OUTPUT and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view FORMATTED INPUT OUTPUT 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?