Search code examples
cformatargumentsc99specifier

Format specifier without argument


Consider the following code sample:

#define STRING_LITERAL  "%u, %u"
const char string_const[ ] = "%u, %u";

snprintf(dest_buff, sizeof(dest_buff), STRING_LITERAL, arg1, arg2, arg3);

My compiler then issues a Warning: the format string ends before this argument

Now if I change the instruction to:

snprintf(dest_buff, sizeof(dest_buff), string_const, arg1, arg2, arg3);

Then no warning is issued by the compiler.

My question is: does such a behavior comply with the C99 standard?


Solution

  • The warning is generated by the compiler which was able to determine that you passed incorrect arguments to the call, and didn't in the second.

    The standard defines that passing incorrect arguments and/or using incorrect flags results in undefined behavior.

    The warning is not required by the Standard, and it is merely additional help to the programmer.