#include <cstdio> int printf( const char *format, ... );
printf function prints output to
stdout, according to
format and other
arguments passed to
printf. The string
format consists of two types of items:
characters that will be printed to the screen, and format commands that
define how the other arguments to
printf are displayed. Basically, you
specify a format string that has text in it, as well as “special” characters
that map to the other arguments of
printf. For example, this code
char name = "Bob"; int age = 21; printf( "Hello %s, you are %d years old\n", name, age );
displays the following output:
Hello Bob, you are 21 years old
%s means, “insert the first argument, a string, right here.” The
indicates that the second argument (an integer) should be placed there.
The return value of
printf is the number of characters printed, or a negative
number if an error occurred.
There are different %-codes for different variable types, as well as options to limit the length of the variables and whatnot.
|%I64d||long long (8B integer), MS-specific|
|%I64u||unsigned long long (8B integer), MS-specific|
|%e||scientific notation, with a lowercase “e”|
|%E||scientific notation, with a uppercase “E”|
|%g||use %e or %f, whichever is shorter|
|%G||use %E or %f, whichever is shorter|
|%s||a string of characters|
|%x||unsigned hexadecimal, with lowercase letters|
|%X||unsigned hexadecimal, with uppercase letters|
|%n||the argument shall be a pointer to an integer into which is placed the number of characters written so far|
An integer placed between a % sign and the format command acts as a minimum field width specifier, and pads the output with spaces or zeros to make it long enough. If you want to pad with zeros, place a zero before the minimum field width specifier:
You may also specify the minimum field width in an int variable if instead of a number you put the * sign:
int width = 12; int age = 100; printf("%*d", width, age);
You can also include a precision modifier, in the form of a
N is some
number, before the format command:
The precision modifier has different meanings depending on the format command being used:
%f, the precision modifier lets you specify the number of decimal places desired. For example,
%12.6fwill display a floating number at least 12 digits wide, with six decimal places.
%G, the precision modifier determines the maximum number of significant digits displayed.
%s, the precision modifier simply acts as a maximum field length, to complement the minimum field length that precedes the period.
As with field width specifier, you may use an int variable to specify the precision modifier by using the * sign:
const char* msg = "Hello printf"; int string_size = strlen (msg); printf("msg: %.*s", string_size, msg);
printf's output is right-justified, unless you place a minus sign
right after the % sign. For example,
will display a floating point number with a minimum of 12 characters, 4 decimal places, and left justified.
You may modify the
specifiers with the letter
l and the letter
h to specify
%hd means a
%g type specifiers can have the letter
l before them to indicate that a double follows.
%e type specifiers can be preceded with the character
# to ensure that
the decimal point will be present, even if there are no decimal digits.
The use of the
# character with the
%x type specifier indicates that the hexidecimal
number should be printed with the
The use of the
%o type specifier indicates that the octal value should be displayed
Inserting a plus sign
+ into the type specifier will force positive values to
be preceded by a
+ sign. Putting a space character ' ' there will force
positive values to be preceded by a single space character.
You can also include constant escape sequences in the output string.