60 likes | 156 Views
Formatted I/O. Standard Output printf() family of functions Standard Input scanf() family of functions. Standard Output. #include <stdio.h> printf(“format string”, arg1, arg2, .... argN); // to the screen fprintf(file_ptr, “format string”, arg1, arg2, .... argN); // to a file
E N D
Formatted I/O • Standard Output • printf() family of functions • Standard Input • scanf() family of functions
Standard Output #include <stdio.h> • printf(“format string”, arg1, arg2, .... argN); // to the screen • fprintf(file_ptr, “format string”, arg1, arg2, .... argN); // to a file • sprintf(str_ptr, “format string”, arg1, arg2, .... argN); // 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 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 \, 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”, arg1, arg2, .... argN); // from the keyboard • scanf(file_ptr, “format string”, arg1, arg2, .... argN); // from a file • sscanf(str_ptr, “format string”, arg1, arg2, .... argN); // 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 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.