1
int main()
{
printf("%c", "\n");

return 0;
}

Here according to type specifier a character is required. But we are passing it const char *. I was hoping that it would give me a warning message in code blocks GNU GCC compiler but it is not giving me any warning and printing the $ character.

why it is not giving any type of warning?

OldSchool
  • 2,123
  • 4
  • 23
  • 45
  • 5
    Did you enable compiler warnings at all? – Kerrek SB Jul 13 '15 at 10:14
  • 1
    I think your question is badly named; you're asking why the compiler is not emitting a warning, but the title suggests that you want to know what happens if `printf` receives an argument that doesn't match its specifier. – user4520 Jul 13 '15 at 10:19
  • Possible duplicate of [What can happen if printf is called with a wrong format string?](http://stackoverflow.com/questions/14504148/what-can-happen-if-printf-is-called-with-a-wrong-format-string) – phuclv May 06 '17 at 09:37

3 Answers3

6

You need to enable that warning.

Compiling your code as test.cpp and adding #include <stdio.h> for printf with the correct warning flag under gcc 4.7.2:

$ gcc -c -Wformat test.cpp
test.cpp: In function 'int main()':
test.cpp:5:18: warning: format '%c' expects argument of type 'int', but argument 2 has type 'const char*' [-Wformat]
Andy Brown
  • 11,766
  • 2
  • 42
  • 61
3

With printf() if there is a type mismatch then it leads to undefined behavior.

The format specifier should match with the type which you want to print.

Further the number of arguments should match with the number of format specifiers violating which will also leads to undefined behavior.

Just add -Wall while compiling your code you will get the below error:

warning: format %c expects type int, but argument 2 has type char *

Gopi
  • 19,784
  • 4
  • 24
  • 36
  • 1
    I think the op is aware of this. He asked why the compiler gives no warnings. – Jabberwocky Jul 13 '15 at 10:18
  • @MichaelWalz Yeah the question was quite misleading as to whether OP knows how printf works or not so added both the details now – Gopi Jul 13 '15 at 10:27
1

You could see that the code also works with %d, %x, %u format specifiers.

Why it works without any warnings ?

Because you don't have warnings enabled in your CodeBlocks.

Go to settings -> compiler and check

Enable All Common Compiler Warnings [-Wall]

And now you get:

In function 'int main()':
warning: format '%c' expects argument of type 'int', but argument 2 has type 'const char*' [-Wformat=]|

Why it even works ?

With %c, $ is the output in CodeBlocks, X is the output in Visual Studio . So, that sounds like undefined behavior.

Wrong format specifiers in scanf (or) printf

Anyways if you want the first char this way only you could do this:

#include <stdio.h>

int main()
{
printf("%c", *"Hello\n"); // Not asked in Question but still :)

return 0;
}

It prints H by dereferencing the const pointer.

Community
  • 1
  • 1
adrian008
  • 395
  • 6
  • 18
  • `$ is the output in CodeBlocks, X is the output in Visual Studio .` I don't think the compiler matters here, it could change between builds with the same compiler if you have ASLR enabled ;) – user4520 Jul 13 '15 at 10:48
  • @szczurcio Yes, it can change but still. Anyways undefined behavior anywhere right ? – adrian008 Jul 13 '15 at 10:49