C examples for stdio.h:printf
function
<cstdio> <stdio.h>
Print formatted data to stdout
int printf ( const char * format, ... );
Parameter | Description |
---|---|
stream | Pointer to a FILE object. |
format | C string format specifiers |
A format specifier follows this prototype:
%[flags] [width] [.precision] [length] specifier
specifier character defines the type and the interpretation of its corresponding argument:
specifier | Output |
---|---|
d or i | Signed decimal integer |
u | Unsigned decimal integer |
o | Unsigned octal |
x | Unsigned hexadecimal integer |
X | Unsigned hexadecimal integer (uppercase) |
f | Decimal floating point, lowercase |
F | Decimal floating point, uppercase |
e | Scientific notation (mantissa/exponent), lowercase |
E | Scientific notation (mantissa/exponent), uppercase |
g | Use the shortest representation: %e or %f |
G | Use the shortest representation: %E or %F |
a | Hexadecimal floating point, lowercase |
A | Hexadecimal floating point, uppercase |
c | Character |
s | String of characters |
p | Pointer address |
n | Nothing printed. The argument must be a pointer to a signed int. The number of characters written so far is saved to the signed int. |
% | A % followed by another % character will write a single % to the stream. |
The format specifier can contain the following sub-specifiers. They are optional.
The flags can be the following value.
flags | description |
---|---|
- | Left-justify within the given field width; Right justification is the default. |
+ | Add a plus or minus sign (+ or -) for numbers. By default, only negative numbers are preceded with a - sign. |
(space) | If no sign is written, a blank space is inserted before the value. |
# | Used with o, x or X specifiers, the value is preceeded with 0, 0x or 0X respectively for nonzero values. |
0 | Left-pads the number with 0 instead of spaces when padding is specified. |
The width can the following values;.
width | description |
---|---|
(number) | Minimum number of characters to be printed. If the value is shorter, blank spaces is padding. If the result is wider, it is not truncated. |
* | An additional integer value preceding the argument that has to be formatted. |
The .precision can have the following value.
.precision | description |
---|---|
.number | For specifiers (d, i, o, u, x, X): precision sets the minimum number of digits. If the value is shorter, the result is padded with leading zeros. No truncating if the result is wider. A precision of 0 means that no character is written for the value 0. For a, A, e, E, f and F specifiers: it sets the number of digits after the decimal point. Default is 6. For g and G: it is the maximum number of significant digits. For s: it is the maximum number of characters to be printed. By default all characters are printed until the ending null character is encountered. If the period is specified without a value for precision, 0 is assumed. |
.* | The precision is not set, but as an additional integer value argument preceding the argument that has to be formatted. |
On success, the total number of characters written is returned.
#include <stdio.h> int main()//ww w. j a v a 2 s.co m { printf ("Characters: %c %c \n", 'a', 65); printf ("Decimals: %d %ld\n", 2020, 650000L); printf ("Preceding with blanks: %10d \n", 2020); printf ("Preceding with zeros: %010d \n", 2020); printf ("Some different radices: %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; }