Why is the output of the code snippet printf("%. %. %. "); comes to be %.0 %.0 %.0?

115 views Asked by At

I know that period . symbol separates field width with precision. But, I am not getting how the zero is coming after %. Every time I am running it is showing the same output.

The code:

#include <stdio.h>
int main() { printf("%. %. %. "); return 0; }

Output: %.0 %.0 %.0

main.c:13:15: warning: unknown conversion type character 0x20 in format [-Wformat=]                                                                                                         
main.c:13:18: warning: unknown conversion type character 0x20 in format [-Wformat=]                                                                                                         
main.c:13:21: warning: unknown conversion type character 0x20 in format [-Wformat=]                                                                                                         
1

There are 1 answers

4
jamesdlin On

You didn't supply a valid conversion specifier after % (e.g. d, x, f, g, c, s, p, among others) so you're invoking undefined behavior. That means anything can happen.

From section 7.19.6.1/9 of the ISO C99 standard:

If a conversion specification is invalid, the behavior is undefined.239) If any argument is not the correct type for the corresponding conversion specification, the behavior is undefined.