c - Passing too few arguments to printf for the format specifier — is it undefined behavior? -


as know, having few format specifiers arguments considered undefined behavior (and having few arguments format specifiers undefined behaviour). apply to:

printf("%1$d %1$d", 5); 

ps use of "%1$d" format specifier an extension printf family of functions added posix.

the n$ notation not part of standard c, part of posix. posix specification printf() supports n$ notation refer arguments.

conversions can applied nth argument after format in argument list, rather next unused argument. in case, conversion specifier character % (see below) replaced sequence "%n$", n decimal integer in range [1,{nl_argmax}], giving position of argument in argument list. feature provides definition of format strings select arguments in order appropriate specific languages (see examples section).

the format can contain either numbered argument conversion specifications (that is, "%n$" , "*m$"), or unnumbered argument conversion specifications (that is, % , *), not both. exception %% can mixed "%n$" form. results of mixing numbered , unnumbered argument specifications in format string undefined. when numbered argument specifications used, specifying nth argument requires leading arguments, first (n-1)th, specified in format string.

in format strings containing "%n$" form of conversion specification, numbered arguments in argument list can referenced format string many times required.

it requires provide argument each n$, , format string refers every argument 1..n. doesn't have use different n$ each time.

the code shown fine on posix systems. since uses posix-only feature, won't portable non-posix systems don't have necessary support extension.


Comments