Format Specifiers in C/C++
Go Up to C Run-Time Library Reference
Format specifiers are used in many C functions and in RTL for classes like UnicodeString.
Format strings contain two types of objects: plain characters and format specifiers. Plain characters are copied verbatim to the resulting string. Format specifiers fetch arguments from the argument list and apply formatting to them.
Note: See scanf Format Specifiers and printf Format Specifiers.
C-runtime Format Types:
Specifier | Meaning |
---|---|
d, i |
Decimal or integer. The argument must be an integer value. The value is converted to a string of decimal digits. If the format string contains a precision specifier, it indicates that the resulting string must contain at least the specified number of digits; if the value has less digits, the resulting string is left-padded with zeros. |
u |
Unsigned int. Similar to "d" and "i", but it has no sign. |
o |
Octal. The argument must be an integer value. The value is converted to a string of octal digits. If the format string contains a precision specifier, it indicates that the resulting string must contain at least the specified number of digits; if the value has less digits, the resulting string is left-padded with zeros. |
x,X |
Hexadecimal. The argument must be an integer value. The value is converted to a string of decimal digits. If the format string contains a precision specifier, it indicates that the resulting string must contain at least the specified number of digits; if the value has less digits, the resulting string is left-padded with zeros. |
f |
Floating point. The argument must be a floating-point value. The value is converted to a string of the form "-ddd.ddd...". The resulting string starts with a minus sign if the number is negative. The number of digits after the decimal point is given by the precision specifier in the format string; a default of 2 decimal digits is assumed if no precision specifier is present. |
e |
Scientific. The argument must be a floating-point value. The value is converted to a string of the form "-d.ddd...E+ddd". The resulting string starts with a minus sign if the number is negative. One digit always precedes the decimal point. The total number of digits in the resulting string (including the one before the decimal point) is given by the precision specifier in the format string; a default precision of 15 is assumed if no precision specifier is present. The "E" exponent character in the resulting string is always followed by a plus or minus sign and at least 3 digits. |
g |
Double. The argument must be a floating-point value. The value is converted to the shortest possible decimal string using fixed or scientific format. The number of significant digits in the resulting string is given by the precision specifier in the format string; a default precision of 15 is assumed if no precision specifier is present. Trailing zeros are removed from the resulting string, and a decimal point appears only if necessary. The resulting string uses fixed point format if the number of digits to the left of the decimal point in the value is less than or equal to the specified precision, and if the value is greater than or equal to 0.00001. Otherwise the resulting string uses scientific format. |
c |
Character. The argument must be a single character value. |
s |
String. The argument must be a character, a string, or a char* value. The string or character is inserted in place of the format specifier. The precision specifier, if present in the format string, specifies the maximum length of the resulting string. If the argument is a string that is longer than this maximum, the string is truncated. |
n |
Pointer to int. Stores (in the location pointed to by the input argument) a count of the chars written so far. |
p |
Pointer. Prints the input argument as a pointer; format depends on which memory model was used. It will be either XXXX:YYYY or YYYY (offset only). |
Note: The "l" prefix is used for long versions of their respective data types; "h" is used for short versions. They can be applied to "d", "i", "u", "o', "x", "f", "g" specifiers.
For instance, using "%lf" instead of "%f" requires you to pass a double value as its respective parameter, instead of a float value.
The following examples use the printf and wprintf functions, as follows:
int/decimal:
printf("%d\n", -5); //-5 printf("%i\n", -5); //-5
unsigned:
printf("%u\n", 5); //5
octal:
printf("%o\n", 9); //11
lowercase and uppercase hexadecimal:
printf("%x\n", 15); //f printf("%X\n", 15); //F
float and longfloat:
printf("%%f is : %f\n", 1.0/3.0); //0.333333 printf("%%lf is : %lf\n", 5000000000000000.0/2.3); //A big number
scientific:
printf("%e\n", 5.5); //5.500000e+00
double:
printf("%g\n", 1.0/3.0); //0.333333
width and precision specifiers:
printf("%f\n", 1.0/3.0); //0.333333 printf("%-5.1f\n", 1.0/3.0); //0.3 printf("%5.1f\n", 1.0/3.0); // 0.3--Two white spaces at the start
char and string:
printf("%c",'a'); printf("%s","string");
widestrings:
wprintf(L"%s", L"string");