int printf(const char* kcpFormatString, ...);
Format Specifiers
The format specification takes the following form:
%<flags><width><precision><size><type>
The '%' character signals the potential insertion of an argument into the string, and the remaining optional fields specify the format and type of the argument (see the tables below).
Flags | Effect |
- | Left-justify the output inside the field. By default it is right-justified. |
+ | Add a +/- prefix for numerical output. By default, the sign is only shown for negative numbers. |
0 | Add a zero prefix to extend numerical output to the minimum width of the field. By default, there is no prefix. |
space | Add a space as a prefix for positive numerical values. By default, positive values have no prefix. |
# | Add a prefix of 0, 0x, or 0X to nonzero arguments of type o, x, or X, respectively. Add a decimal point to arguments of the type e, E, f, a, or A even when no digits follow it. Always use a decimal point with arguments of the type g and G and do not truncate trailing zeros. |
Width | Effect |
integer | Specifies the minimum bytes printed for the argument's output. If the number of character outputted for the argument is less than this, the output is padded with spaces. |
* | The width is specified by an int argument, which precedes the formatted type. |
Precision | Effect |
integer | Specifies the minimum number of bytes used for integer types: d, i, o, u, x, X; extra zeros are prepended to reach the minimum for these types, with the default being 1. For the types, a, A, e, E, and f, this integer specifies the number of digits after the radix point. For the types, g and G, this specifies the maximum number of significant figures. For the types, s and S, this specifies the maximum number of characters to be printed from the string. |
* | The precision is specified by an int argument, which precedes the formatted type. |
Size | Effect |
h | Argument types d, i, o, x, and X are specified as short. Argument type u is specified as unsigned short. Argument types c and s are specified as single byte ASCII. |
l | Argument types d, i, o, x, and X are specified as long. Argument type u is specified as unsigned long. Argument type f is specified as long double. Argument types c and s are specified as multi-byte UNICODE. |
ll | Argument types d, i, o, u, x, and X are specified as long long. |
L | Argument type f is specified as a long double. |
w | Argument types c and s are specified as multi-byte UNICODE. |
Type | Effect |
a, A | Output "doubles" in hexadecimal floating-point format using lower or upper case letters for hexadecimal digits with 'a' or 'A', respectively. |
c | Output "chars" in single-byte ASCII format. |
d | Output "ints" as signed decimal integers. |
e, E | Output "doubles" in decimal floating-point format using 'e' or 'E' for the exponent with 'e' or 'E', respectivley. |
f | Output "doubles" in decimal fixed-point format. |
g, G | Output "doubles" in decimal fixed-point or floating-point format using 'e' or 'E' for the exponent with 'g' or 'G', respectively. |
i | Output "ints" as signed decimal integers. |
n | Output the number of characters written so far into an int pointer. |
o | Output "ints" as unsigned octal integers. |
p | Output "void pointers" as a hexadecimal integer address. |
s | Output a null-terminated ASCII character string. |
u | Output "ints" as unsigned decimal integers. |
x, X | Output "ints" as unsigned hexadecimal integers, using lower or upper case letters with 'x' or 'X', respectively. |
#include <cstdio> int main() { char caString[] = "formatted"; // Write a formatted string to the stdout. int iRet = printf("Writing a %s string.\n", caString); // Check whether or not some characters were written if (iRet > 0) { printf("Characters written:%i\n", iRet); } else { perror("printf() call failed"); } return 0; }
© 20072024 XoaX.net LLC. All rights reserved.