-5
#include <stdio.h>
int main()
{
printf("%c\n","\"\"\"\"\"");

}

This prints $ but not """""

Why "$" is printed when I try to run the C code above?

halfer
  • 19,824
  • 17
  • 99
  • 186
  • `"%c"` is for a *single* `char`. Mismatching format specifier and argument leads to [*undefined behavior*](https://en.wikipedia.org/wiki/Undefined_behavior). UB (Undefined Behavior) means there's no use in discussing behavior. – Some programmer dude Jul 14 '18 at 14:33
  • [What is the difference between %c and %s in C](https://stackoverflow.com/questions/33856654/what-is-the-difference-between-c-and-s-in-c/33856673) – user202729 Jul 14 '18 at 14:33

3 Answers3

1

You pass a string to printf, not a char. Try '"'

What happens in your code is that the string is passed to printf, but printf expects a char. printf now tries to interpret the address of this string as a char. In general, this is undefined behavior.

Paul Ogilvie
  • 25,048
  • 4
  • 23
  • 41
0

%c is the format specifier for a single character; there is probably some conversion going on. You want %s (for strings).

0

It actually prints some garbage, because your input is not a single character, is a sequence of characters. Try using %s instead of %c