Formatted IO Standard printf family of functions Standard

  • Slides: 6
Download presentation
Formatted I/O ä Standard ä printf() family of functions ä Standard ä scanf() Output

Formatted I/O ä Standard ä printf() family of functions ä Standard ä scanf() Output Input family of functions

Standard Output #include <stdio. h> ä ä printf(“format string”, arg 1, arg 2, .

Standard Output #include <stdio. h> ä ä printf(“format string”, arg 1, arg 2, . . arg. N); // to the screen fprintf(file_ptr, “format string”, arg 1, arg 2, . . arg. N); // to a file sprintf(str_ptr, “format string”, arg 1, arg 2, . . arg. N); // to a string each returns the number of characters printed. Format String ä ä All but two characters printed verbatim. % character used to format and output the next argument. character used to print characters that can’t be typed directly. Must be at least as many arguments as % specifiers.

% - printf() conversion specifiers Format: %[flags][width][. precision][modifier]type_character äAll except type_character are optional. äConversion

% - printf() conversion specifiers Format: %[flags][width][. precision][modifier]type_character äAll except type_character are optional. äConversion specifier is everything from % sign to the first type_character. ä[flags] - controls justification, leading spaces, sign, etc. {-, +, , #, 0} ä[width] - sets the minimum width of the field - may be longer. ä[. precision] - sets the number of digits following the decimal point. ä Usually used for floating points, but also affects character and integer types as well. ä[modifier] - combines with type_character to determine argument type. ätype_character - the basic type of that argument. ä ä {c, d, e, E, f, g, G, i, o, p, s, u, x, X, %} In general, look up what you need. You will tend to remember the forms you use most often.

 - printf() escape sequences Format: (character) or (number) äIf a character follows the

- printf() escape sequences Format: (character) or (number) äIf a character follows the , the action indicated by the character is performed. n - newline ä r - return w/o line feed. ä ” - print double quote ä ’ - print single quote (aka apostrophe) ä a - sound bell ä b - backspace ä \ - print backslash ä ? - question mark äIf a number follows the , the ASCII character of the octal number is printed. ä

Standard Input #include <stdio. h> ä ä scanf(“format string”, arg 1, arg 2, .

Standard Input #include <stdio. h> ä ä scanf(“format string”, arg 1, arg 2, . . arg. N); // from the keyboard scanf(file_ptr, “format string”, arg 1, arg 2, . . arg. N); // from a file sscanf(str_ptr, “format string”, arg 1, arg 2, . . arg. N); // from a string each returns the number of successful conversions. Format String ä ä ä Literal characters in format string can be used to skip characters. % conversion specifiers similar (but not identical) to printf(). Arguments need to be memory locations where values will be stored. Big Time Caveat ä ä ä If input does not adequately match format, results can be VERY unpredictable. Many programmers avoid the use of scanf() at nearly any cost. Use of fscanf() for file I/O is generally safe provided the file format is adequately constrained.

Common mistakes with scanf() Passing values instead of memory locations. ä The scanf() function

Common mistakes with scanf() Passing values instead of memory locations. ä The scanf() function read values and store them at the memory locations you supply. k = 10; scanf(“%i”, k); ä Tells scanf() to format the value as an integer and store it at location 10. ä But the memory location for variable k is almost certainly not 10. ä The address operator, &, returns the memory location of the specified variable. scanf(“%i”, &k); ä Tells scanf() to format the value as an integer and store it at the memory address used for variable k. Using %lf for doubles. ä printf() uses %lf for both floats and doubles because the compiler promotes all arguments of type float to double. ä scanf() cannot due this. It must know which type the variable is so that the number and format of the bytes stored is correct.