Tuesday 1 October 2013

Formatted Input and Output

printf

			printf( "format string " [ , expression ] ... );
  • where:
    • The format string is a quoted string, optionally containing one or more format specifiers, beginning with % symbols.
    • After the format string come zero or more expressions to be printed.
    • Specifically there must be exactly as many expressions following the format string as there are format specifiers in the string.
    • Expressions to be printed may be any combination of constants and variables that match the data types of the format specifiers.

printf format specifiers

  • printf format specifiers take the form of
			%[flags][width][.precision][length]specifier
  • where:
    • specifier is the most important and only required part, indicating the type of data to be printed. e.g. "%f"
      • See below for a complete table
    • width specifies the total number of spaces to use to print the number, filling in with blanks if necssary
      • Example: "%15f" would print a float in a space 15 characters wide
      • If an asterisk ( * ) is used, then one of the data expressions is used to determine the width used.
    • .precision indicates how many digits of a number to print, or sometimes the number to the right of the decimal point.
      • Examples:
        • "%15.3f" would print 10 * pi as 31.416, with three digits to the right of the decimal point.
        • %15.3e" would print 10 * pi as 3.14e1, with three total digits of precision
      • If a dot asterisk ( .* ) is used, then one of the data expressions is used to determine the width used.
    • [flags] are optional, and can include::
      • A minus sign ( - ) to left justify the output. Default is right justified.
      • A plus sign ( + ) to force a sign to be printed.
      • ( space ) to inhibit printing of the sign
      • zero ( 0 ) to left pad the number with zeros instead of spaces
      • hash ( # ) serves different purposes depending on the specifier used.
        • With octal or hexidecimal output, specifies a leading 0 or 0x for non-zero data values
        • With floating point types, requires printing a decimal point even if there are no digits following it.
    • [length] is a subspecifier for unsigned, short, long, etc. types. See table below

printf specifiers

Note: CS 107 students are not responsible for knowing the a, A, n, or p specifiers.
specifier Output Example
d or i Signed decimal integer 392
u Unsigned decimal integer 7235
o Unsigned octal 610
x Unsigned hexadecimal integer 7fa
X Unsigned hexadecimal integer (uppercase) 7FA
f Decimal floating point, lowercase 392.65
F Decimal floating point, uppercase 392.65
e Scientific notation (mantissa/exponent), lowercase 3.9265e+2
E Scientific notation (mantissa/exponent), uppercase 3.9265E+2
g Use the shortest representation: %e or %f 392.65
G Use the shortest representation: %E or %F 392.65
a Hexadecimal floating point, lowercase -0xc.90fep-2
A Hexadecimal floating point, uppercase -0XC.90FEP-2
c Character a
s String of characters sample
p Pointer address b8000000
n Nothing printed.
The corresponding argument must be a pointer to a signed int.
The number of characters written so far is stored in the pointed location.
 
% A % followed by another % character will write a single % to the stream. %

printf length sub-specifiers

Note: CS 107 students are not responsible for knowing j, z, or t length specifiers, or wide character types
  specifiers
length d i u o x X f F e E g G a A c s p n
(none) int unsigned int double int char* void* int*
hh signed char unsigned char         signed char*
h short int unsigned short int         short int*
l long int unsigned long int   wint_t wchar_t*   long int*
ll long long int unsigned long long int         long long int*
j intmax_t uintmax_t         intmax_t*
z size_t size_t         size_t*
t ptrdiff_t ptrdiff_t         ptrdiff_t*
L     long double        
 

scanf

			printf( "format string " [ , expression ] ... );
  • where:
    • The format string is a quoted string, normally consisting only of one or more format specifiers, beginning with % symbols.
    • After the format string come zero or more expressions to be printed.
    • Specifically there must be exactly as many expressions following the format string as there are format specifiers in the string.
    • Expressions in scanf need to be memory addresses of where the scanned data is to be stored, which are usually generated by applying the "address of" operator, &, to a variable name, for example, &X yields the address of X, i.e. the location of X in memory.

Scanf format specifiers

  • scanf format specifiers take the form of
			%[*][width][length]specifier
  • where:
    • specifier is the most important and only required part, indicating the type of data to be scanned. e.g. "%f"
      • See below for a complete table
    • Asterisk, *, indicates to read in the data from the input stream, but not store it. ( I.e. read and discard )
    • width specifies the maximum number of characters to read in
    • length is a modifier for unsigned, short, long,etc data types.
      • See below for a complete table

scanf specifiers

Note: CS 107 students are not required to know the a,p, [ ], [^ ], or n specifiers
specifier Description Characters extracted
i, u Integer Any number of digits, optionally preceded by a sign (+ or -).
Decimal digits assumed by default (0-9), but a 0 prefix introduces octal digits (0-7), and 0x hexadecimal digits (0-f).
d Decimal integer Any number of decimal digits (0-9), optionally preceded by a sign (+ or -).
o Octal integer Any number of octal digits (0-7), optionally preceded by a sign (+ or -).
x Hexadecimal integer Any number of hexadecimal digits (0-9, a-f, A-F), optionally preceded by 0x or 0X, and all optionally preceded by a sign (+ or -).
f, e, g Floating point number A series of decimal digits (or hexadecimal, if preceded by 0x or 0X), optionally containing a decimal point, optionally preceeded by a sign (+ or -) and optionally followed by the e or E character and a decimal integer (or p or P and an hexadecimal integer). Or any other sequence supported by strtod.
a
c Character The next character. If a width other than 1 is specified, the function reads exactly width characters and stores them in the successive locations of the array passed as argument. No null character is appended at the end.
s String of characters Any number of non-whitespace characters, stopping at the first whitespace character found. A terminating null character is automatically added at the end of the stored sequence.
p Pointer address A sequence of characters representing a pointer. The particular format used depends on the system and library implementation, but it is the same as the one used to format %p in fprintf.
[characters] Scanset Any number of the characters specified between the brackets.
A dash (-) that is not the first character may produce non-portable behavior in some library implementations.
[^characters] Negated scanset Any number of characters none of them specified as characters between the brackets.
n Count No input is consumed.
The number of characters written so far is stored in the pointed location.
% % A % followed by another % matches a single %.

scanf length specifiers

Note: CS 107 students are not required to know the j, z, or t specifiers, or wide ( long ) character types

specifiers
length d i u o x f e g a c s [] [^] p n
(none) int* unsigned int* float* char* void** int*
hh signed char* unsigned char* signed char*
h short int* unsigned short int* short int*
l long int* unsigned long int* double* wchar_t* long int*
ll long long int* unsigned long long int* long long int*
j intmax_t* uintmax_t* intmax_t*
z size_t* size_t* size_t*
t ptrdiff_t* ptrdiff_t* ptrdiff_t*
L long double*

Related functions

  • fprintf and fscanf are used for formatted data transfer into and out of files respectively.
  • sprintf and sscanf are used for formatted data transfer into and out of memory respectively, e.g. "printng" into a character array.
  • getc,putc, getchar and putchar transfer individual characters, faster and at a more controlled level than printf( "%c", c ) and scanf( "%c", c ) respectively.

No comments:

Post a Comment