Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: rewrote table introduction, since i found it confusing; made some wordsmithing modifications to first paragraph; combined the x and X rows in the table; corrected entries in table (some of the typenames were wrong I think, the o,u,x,X specifiers interpret h, hh, l, ll as unsigned values, and d matched with hh interprets the argument specifically as a signed char); added some explanation text to the first NCCE

The formatted IO formatted output functions fprintf(), printf(), sprintf(), snprintf(), vfprintf(), vprintf(), vsprintf(), and vsnprintf((fprintf() and related functions) convert, format, and print their arguments under control of a format string. Subclause , defined as follows by the C Standard, subclause 7.21.6.1 of the C Standard , paragraph 3 [ISO/IEC 9899:2011] states:

The format shall be a multibyte character sequence, beginning and ending in its initial shift state. The format is composed of zero or more directives: ordinary multibyte characters (not %), which are copied unchanged to the output stream; and conversion specifications, each of which results in fetching zero or more subsequent arguments, converting them, if applicable, according to the corresponding conversion specifier, and then writing the result to the output stream.

...

The following table summarizes C-compliant conversion specifiers along with the flag characters valid for each specification the compliance of various conversion specifications.  The first column contains a conversion specifier character (or characters).  The next four columns consider the combination of the specifier character(s) with the various flags (the apostrophe ['], -, +, the space character, and # in columns 2 through 5) and ).  The next eight columns consider the combination of the specifier character(s) with the various length modifiers (h, hh, l, ll, j, z, t, and L in columns 6 through 13) and the type of the expected argument. Valid and meaningful combinations of a conversion specification, flag character, and length modifier are denoted by the (tick) symbol in the corresponding cell or by the name of the type argument affected by the length modifier. .  Here, valid combinations are marked with a type name; arguments matched with the conversion specification will be interpreted as that type.  For example, an argument matched with the specifier %hd will be interpreted as a short, so short appears in the cell where d and h intersect.  The last column denotes the expected type of arguments matched with the original specifier character(s).  Throughout the table, valid and meaningful combinations are marked by the (tick) symbol (save for the length modifier columns, as described above).  Valid combinations that have no effect are denoted by labeled N/E.  Using a combination of a conversion specification, flag character, and length modifier denoted marked by the (error) symbol or a , using a specification not listed represented in the table, or using an argument of argument of an unexpected type , may result in undefined behavior. See undefined behaviors 153, 155,   157, 158, 161, and 162 in Annex J of the C Standard. 

Conversion
Specifier
Character

' XSI

-
+
SPACE


#


0

 


h


hh


l


ll


j


z


t


L

Argument
Type

d, i

(tick)

(tick)

(error)

(tick)

 

short

signed char

long

long long

intmax_t

size_t

ptrdiff_t

(error)

Signed integer

o

(error)

(tick)

(tick)

(tick)

 

unsigned short

unsigned char

unsigned long

unsigned long long

intmaxuintmax_t

size_t

ptrdiff_t

(error)

Unsigned integer

u

(tick)

(tick)

(error)

(tick)

 

unsigned short

unsigned  char

unsigned long

unsigned long long

intmaxuintmax_t

size_t

ptrdiff_t

(error)

Unsigned integer

x

(error)

(tick)

(tick)

(tick)

 

short

char

long

long long

intmax_t

size_t

ptrdiff_t

(error)

Unsigned integer

, X

(error)

(tick)

(tick)

(tick)

 

unsigned short

unsigned char

unsigned long

unsigned long long

intmaxuintmax_t

size_t

ptrdiff_t

(error)

Unsigned integer

f, F

(tick)

(tick)

(tick)

(tick)

 

(error)

(error)

N/E

N/E

(error)

(error)

(error)

long double

double or long double

e, E

(error)

(tick)

(tick)

(tick)

 

(error)

(error)

N/E

N/E

(error)

(error)

(error)

long double

double or long double

g, G

(tick)

(tick)

(tick)

(tick)

 

(error)

(error)

N/E

N/E

(error)

(error)

(error)

long double

double or long double

a, A

(tick)

(tick)

(tick)

(tick)

 

(error)

(error)

N/E

N/E

(error)

(error)

(error)

long double

double or long double

c

(error)

(tick)

(error)

(error)

 

(error)

(error)

wint_t

(error)

(error)

(error)

(error)

(error)

int or wint_t

s

(error)

(tick)

(error)

(error)

 

(error)

(error)

NTWS

(error)

(error)

(error)

(error)

(error)

NTBS or NTWS

p

(error)

(tick)

(error)

(error)

 

(error)

(error)

(error)

(error)

(error)

(error)

(error)

(error)

void*

n

(error)

(tick)

(error)

(error)

 

short*

char*

long*

long long*

intmax_t*

size_t*

ptrdiff_t*

(error)

Pointer to integer

C XSI

(error)

(tick)

(error)

(error)

 

(error)

(error)

(error)

(error)

(error)

(error)

(error)

(error)

wint_t

S XSI

(error)

(tick)

(error)

(error)

 

(error)

(error)

(error)

(error)

(error)

(error)

(error)

(error)

NTWS

%

(error)

(tick)

(error)

(error)

 

(error)

(error)

(error)

(error)

(error)

(error)

(error)

(error)

None

...

Mismatches between arguments and conversion specifications may result in undefined behavior.  Many compilers can diagnose type mismatches in formatted output function invocations.  In the following noncompliant code example, the error_type argument to printf() is incorrectly matched with the %s specifier (should be %d), and the error_msg argument is incorrectly matched with the %d specifier (should be %s).  One possible result of this invocation is that printf() will interpret the error_type argument as a pointer, and try to read a string from the address that error_type contains (likely this will result in an access violation):

Code Block
bgColor#ffcccc
langc
const char *error_msg = "Resource not available to user.";
int error_type = 3;
/* ... */
printf("Error (type %s): %d\n", error_type, error_msg);

...