C Standard Libraries C++

printf()

Declaration

int printf(const char* kcpFormatString, ...);

Description

This function outputs the formatted string "kcpFormatString" to the standard output stream "stdout." If successful, the function returns the number of characters output. Otherwise, the function returns a negative value to indicate an error.

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).



FlagsEffect

-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.
0Add a zero prefix to extend numerical output to the minimum width of the field. By default, there is no prefix.
spaceAdd 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.



WidthEffect

integerSpecifies 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.



PrecisionEffect

integerSpecifies 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.



SizeEffect

hArgument 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.
lArgument 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.
llArgument types d, i, o, u, x, and X are specified as long long.
LArgument type f is specified as a long double.
wArgument types c and s are specified as multi-byte UNICODE.



TypeEffect

a, AOutput "doubles" in hexadecimal floating-point format using lower or upper case letters for hexadecimal digits with 'a' or 'A', respectively.
cOutput "chars" in single-byte ASCII format.
dOutput "ints" as signed decimal integers.
e, EOutput "doubles" in decimal floating-point format using 'e' or 'E' for the exponent with 'e' or 'E', respectivley.
fOutput "doubles" in decimal fixed-point format.
g, GOutput "doubles" in decimal fixed-point or floating-point format using 'e' or 'E' for the exponent with 'g' or 'G', respectively.
iOutput "ints" as signed decimal integers.
nOutput the number of characters written so far into an int pointer.
oOutput "ints" as unsigned octal integers.
pOutput "void pointers" as a hexadecimal integer address.
sOutput a null-terminated ASCII character string.
uOutput "ints" as unsigned decimal integers.
x, XOutput "ints" as unsigned hexadecimal integers, using lower or upper case letters with 'x' or 'X', respectively.

Example

#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;
}

Output

printf() Output
 

© 2007–2024 XoaX.net LLC. All rights reserved.