printf
int  printf ( const char * format [ , argument , ...] );
stdio.h
  cplusplus.com  

Print formatted data to stdout.
  Prints to standard output (stdout) a sequence of arguments formatted as the format argument specifies.

Parameters.

format
String that contains the text to be printed.
Optionally it can contain format tags that are substituted by the values specified in subsequent argument(s) and formatted as requested.
The number of format tags must correspond to the number of additional arguments that follows.
The format tags follow this prototype:
 
%[flags][width][.precision][modifiers]type
 
  where type is the most significant and defines how the value will be printed:
typeOutputExample
c
Charactera
d or i
Signed decimal integer392
e
Scientific notation (mantise/exponent) using e character3.9265e2
E
Scientific notation (mantise/exponent) using E character3.9265E2
f
Decimal floating point392.65
g
Use shorter %e or %f392.65
G
Use shorter %E or %f392.65
o
Signed octal610
s
String of characterssample
u
Unsigned decimal integer7235
x
Unsigned hexadecimal integer7fa
X
Unsigned hexadecimal integer (capital letters)7FA
p
Address pointed by the argumentB800:0000
n
Nothing printed. The argument must be a pointer to integer where the number of characters written so far will be stored. 

  the other flags, width, .precision and modifiers sub-parameters are optional and follow these specifications:
 
flags
meaning
-
Left align within the given width. (right align is the default).
+
Forces to preceed the result with a sign (+ or -) if signed type. (by default only - (minus) is printed).
blank
If the argument is a positive signed value, a blank is inserted before the number.
#
Used with o, x or X type the value is preceeded with 0, 0x or 0X respectively if non-zero.
Used with e, E or f forces the output value to contain a decimal point even if only zeros follow.
Used with g or G the result is the same as e or E but trailing zeros are not removed.
 
width
meaning
number
Minimum number of characters to be printed. If the value to be printed is shorter than this number the result is padded with blanks. The value is never truncated even if the result is larger.
0number
Same as above but filled with 0s instead of blanks.
*
The width is not specified in the format string, it is specified by an integer value preceding the argument thas has to be formatted.
 
.precision
meaning
.number
for d, i, o, u, x, X types: precision specifies the minimum number of decimal digits to be printed. If the value to be printed is shorter than this number the result is padded with blanks. The value is never truncated even if the result is larger.(if nothing specified default is 1).
for e, E, f types: number of digits to be printed after de decimal point. (if nothing specified default is 6).
for g, G types : maximum number of significant numbers to be printed.
for s type: maximum number of characters to be printed. (default is to print until first null character is encountered).
for c type : (no effect).
 
modifier
meaning (affects on how arguments are interpreted by the function)
h
argument is interpreted as short int (integer types).
l
argument is interpreted as long int (interger types) or double (floating point types).
L
argument is interpreted as long double (floating point types).

argument(s)
Optional parameter(s) that contain the data to be inserted instead of % tags specified in format parameter. There must be the same number of these parameter than the number format tags.

Return Value.
  On success, the total number of characters printed is returned.
  On error, a negative number is returned.

Example.

/* fprintf example: some format examples */
#include <stdio.h>

main()
{
   printf ("Characters: %c %c \n", 'a', 65);
   printf ("Decimals: %d %ld\n", 1977, 650000);
   printf ("Preceding with blanks: %10d \n", 1977);
   printf ("Preceding with zeros: %010d \n", 1977);
   printf ("Some different radixes: %d %x %o %#x %#o \n", 100, 100, 100, 100, 100);
   printf ("floats: %4.2f %+.0e %E \n", 3.1416, 3.1416, 3.1416);
   printf ("Width trick: %*d \n", 5, 10);
   printf ("%s \n", "A string");
   return 0;
}

And here is the output:
Characters: a A
Decimals: 1977 650000
Preceding with blanks:       1977
Preceding with zeros: 0000001977
Some different radixes: 100 64 144 0x64 0144
floats: 3.14 +3e+000 3.141600E+000
Width trick:    10
A string

See also.
  fprintf, scanf


© The C++ Resources Network, 2000