Print Conversion Specifiers

Following any precision, you must write a one-character print conversion specifier, possibly preceded by a one-character qualifier. Each combination determines the type required of the next argument (if any) and how the library functions alter the argument value before converting it to a text sequence. The integer and floating-point conversions also determine what base to use for the text representation. If a conversion specifier requires a precision p and you do not provide one in the format, then the conversion specifier chooses a default value for the precision. The following table lists all defined combinations and their properties.

Conversion  Argument        Converted      Default  Pre-
 Specifier    Type            Value          Base  cision
   %c       int x          (unsigned char)x
  %lc       wint_t x       wchar_t a[2] = {x}
   %d       int x          (int)x             10     1
  %hd       int x          (short)x           10     1
  %ld       long x         (long)x            10     1
   %e       double x       (double)x          10     6
  %Le       long double x  (long double)x     10     6
   %E       double x       (double)x          10     6
  %LE       long double x  (long double)x     10     6
   %f       double x       (double)x          10     6
  %Lf       long double x  (long double)x     10     6
   %g       double x       (double)x          10     6
  %Lg       long double x  (long double)x     10     6
   %G       double x       (double)x          10     6
  %LG       long double x  (long double)x     10     6
   %i       int x          (int)x             10     1
  %hi       int x          (short)x           10     1
  %li       long x         (long)x            10     1
   %n       int *x
  %hn       short *x
  %ln       long *x
   %o       int x          (unsigned int)x     8     1
  %ho       int x          (unsigned short)x   8     1
  %lo       long x         (unsigned long)x    8     1
   %p       void *x        (void *)x
   %s       char x[]       x[0]...                 large
  %ls       wchar_t x[]    x[0]...                 large
   %u       int x          (unsigned int)x    10     1
  %hu       int x          (unsigned short)x  10     1
  %lu       long x         (unsigned long)x   10     1
   %x       int x          (unsigned int)x    16     1
  %hx       int x          (unsigned short)x  16     1
  %lx       long x         (unsigned long)x   16     1
   %X       int x          (unsigned int)x    16     1
  %hX       int x          (unsigned short)x  16     1
  %lX       long x         (unsigned long)x   16     1
   %%       none           '%'

The print conversion specifier determines any behavior not summarized in this table. In the following descriptions, p is the precision. Examples follow each of the print conversion specifiers. A single conversion can generate up to 509 characters.

You write %c to generate a single character from the converted value.

    printf(“%c”, 'a')               generates a
    printf(“<%3c|%-3c>”, 'a', 'b')  generates <  a|b  >

For a wide stream, conversion of the character x occurs as if by calling btowc(x).

    wprintf(L“%c”, 'a')             generates btowc(a)

You write %lc to generate a single character from the converted value. Conversion of the character x occurs as if it is followed by a null character in an array of two elements of type wchar_t converted by the conversion specification ls.

    printf(“%lc”, L'a')             generates a
    wprintf(L“lc”, L'a')            generates L'a'

You write %d, %i, %o, %u, %x, or %X to generate a possibly signed integer representation. %d or %i specifies signed decimal representation, %o unsigned octal, %u unsigned decimal, %x unsigned hexadecimal using the digits 0-9 and a-f, and %X unsigned hexadecimal using the digits 0-9 and A-F. The conversion generates at least p digits to represent the converted value. If p is zero, a converted value of zero generates no digits.

    printf(“%d %o %x”, 31, 31, 31)  generates 31 37 1f
    printf(“%hu”, 0xffff)           generates 65535
    printf(“%#X %+d”, 31, 31)       generates 0X1F +31

You write %e or %E to generate a signed fractional representation with an exponent. The generated text takes the form ±d.dddE±dd, where ± is either a plus or minus sign, d is a decimal digit, the dot (.) is the decimal point for the current locale, and E is either e (for %e conversion) or E (for %E conversion). The generated text has one integer digit, a decimal point if p is nonzero or if you specify the # format flag, p fraction digits, and at least two exponent digits. The result is rounded. The value zero has a zero exponent.

    printf(“%e”, 31.4)              generates 3.140000e+01
    printf(“%.2E”, 31.4)            generates 3.14E+01

You write %f to generate a signed fractional representation with no exponent. The generated text takes the form ±d.ddd, where ± is either a plus or minus sign, d is a decimal digit, and the dot (.) is the decimal point for the current locale. The generated text has at least one integer digit, a decimal point if p is nonzero or if you specify the # format flag, and p fraction digits. The result is rounded.

    printf(“%f”, 31.4)              generates 31.400000
    printf(“%.0f %#.0f”, 31.0, 31.0)generates 31 31.

You write %g or %G to generate a signed fractional representation with or without an exponent, as appropriate. For %g conversion, the generated text takes the same form as either %e or %f conversion. For %G conversion, it takes the same form as either %E or %f conversion. The precision p specifies the number of significant digits generated. (If p is zero, it is changed to 1.) If %e conversion would yield an exponent in the range [-4, p), then %f conversion occurs instead. The generated text has no trailing zeros in any fraction and has a decimal point only if there are nonzero fraction digits, unless you specify the # format flag.

    printf(“%.6g”, 31.4)            generates 31.4
    printf(“%.1g”, 31.4)            generates 3.14e+01

You write %n to store the number of characters generated (up to this point in the format) in the object of type int whose address is the value of the next successive argument.

    printf(“abc%n”, &x)                   stores 3

You write %p to generate an external representation of a pointer to void. The conversion is implementation defined.

    printf(“%p”, (void *)&x)        generates, e.g. F4C0

You write %s to generate a sequence of characters from the values stored in the argument C string.

    printf(“%s”, “hello”)           generates hello
    printf(“%.2s”, “hello”)         generates he

For a wide stream, conversion occurs as if by repeatedly calling mbrtowc, beginning in the initial conversion state. The conversion generates no more than p characters, up to but not including the terminating null character.

    wprintf(L“%s”, “hello”)         generates hello

You write %ls to generate a sequence of characters from the values stored in the argument wide-character string. For a byte stream, conversion occurs as if by repeatedly calling wcrtomb, beginning in the initial conversion state, so long as complete multibyte characters can be generated. The conversion generates no more than p characters, up to but not including the terminating null character.

    printf(“%ls”, L“hello”)         generates hello
    wprintf(L“%.2s”, L“hello”)      generates he

You write %% to generate the percent character (%).

    printf(“%%”)                    generates %

Copyright note

Certain materials included or referred to in this document are copyright P.J. Plauger and/or Dinkumware, Ltd. or are based on materials that are copyright P.J. Plauger and/or Dinkumware, Ltd.

Notwithstanding the meta-data for this document, copyright information for this document is as follows:

Copyright © IBM Corp. 1999, 2013. & Copyright © P.J. Plauger and/or Dinkumware, Ltd. 1992-2006.