c - How printf works in case of type mismatch with type specifier? -


int main() { printf("%c", "\n");  return 0; } 

here according type specifier character required. passing const char *. hoping give me warning message in code blocks gnu gcc compiler not giving me warning , printing $ character.

why not giving type of warning?

you see code works %d, %x, %u format specifiers.

why works without warnings ?

because don't have warnings enabled in codeblocks.

go settings -> compiler , check  enable common compiler warnings [-wall] 

and get:

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

why works ?

with %c, $ output in codeblocks, x output in visual studio . so, sounds undefined behavior.

wrong format specifiers in scanf (or) printf

anyways if want first char way this:

#include <stdio.h>  int main() { printf("%c", *"hello\n"); // not asked in question still :)  return 0; } 

it prints h dereferencing const pointer.


Comments