-3

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=]                                                                                                         
jamesdlin
  • 81,374
  • 13
  • 159
  • 204
  • 1
    The program has undefined behavior. – Vlad from Moscow Sep 19 '19 at 15:14
  • 1
    Do not ignore warning. This is most likely undefined behavior: you do not provide a valid conversion character. 0x20 is the hexadecimal value of space. – AugustinLopez Sep 19 '19 at 15:14
  • If you provide invalid format specifiers, why would you expect to get any specific result? – Gerhardh Sep 19 '19 at 15:16
  • Given the `#include` directive without a filename I would expect a few more warnings. – Gerhardh Sep 19 '19 at 15:19
  • @ryyker no, no zero is implied, and that output does not happen on all printf implementations (afaik it only happens on just _one_ printf implementation -- the glibc's). –  Sep 19 '19 at 15:20
  • 1
    Quite similar. Possible duplicate of [printf() with no arguments in C compiles fine. how?](https://stackoverflow.com/questions/28194227/printf-with-no-arguments-in-c-compiles-fine-how) – Achal Sep 19 '19 at 15:23
  • it is not duplicate. How can you say this sir. – Rohit Pratap Sep 19 '19 at 15:27
  • What did you expect it to do? – Steve Summit Sep 19 '19 at 15:36
  • @mosvy - Unfortunately, the original text ( `Output: %.0 %.0 %.0 main.c:13:15: warning: unknown conversion type` ) to which I was referring, and was also part of OP's question has been edited out. In any case, you are picking nits here with _"no zero is implied"_... The idea of a zero just appearing without a cause is ridiculous, regardless of compiler implementation, if it showed up, who/what do you suppose placed the zero there? The compiler that was used to generate that error string placed the zero there, thereby _implying_ the zero. – ryyker Sep 19 '19 at 15:52
  • @ryyker a) that hasn't been edited out b) what printf does has _nothing_ to do with the compiler implementation, and c) yes, a 0 or any other byte could just appear there with no particular cause -- it could be garbage left on the stack by previous code (eg. the the dynamic linker), or unitialized memory purposely "poisoned" with random stuff in order to catch bugs and prevent exploits. In any case, the compiler __did not place any zero there__. It's just an artifact of the printf implementation in the GNU C library –  Sep 19 '19 at 16:54
  • an artifact, not a bug, since it's undefined behavior, and they're free to do what they please with it (including printing "MAYONNAISE!" for each unparsable `%` spec). –  Sep 19 '19 at 16:55
  • thank you @mosvy sir for clarification. – Rohit Pratap Sep 19 '19 at 17:38

1 Answers1

3

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.

jamesdlin
  • 81,374
  • 13
  • 159
  • 204
  • Every time I run this code, I am getting the same output. – Rohit Pratap Sep 19 '19 at 15:33
  • 1
    @RohitPratap Undefined does not mean random. Undefined means *anything* can happen. Absolutely anything. Even if you were sure it had to make sense, it's allowed to not make sense. Even if you were sure it had to not make sense, it's allowed to be predictable. – Steve Summit Sep 19 '19 at 15:35
  • 2
    @RohitPratap always compile with `cc -Werror=format`. The fact that the compiler treats this as an informative warning instead of failing hard as on a syntax error is just unfortunate. –  Sep 19 '19 at 15:36
  • @mosvy ok sir, I would. – Rohit Pratap Sep 19 '19 at 15:38