-4

I declared a character as below in C:

char x="A";
printf("%c", x);

Why does this print a question mark kind of symbol and if I change format specifier to %d, it prints 4?

Why does this not raise an error and why are these outputs such?

When I replace "A" with 'A' it works as desired and prints A with %c and 65 with %d.

3 Answers3

6

Why does this print a question mark kind of symbol?

What did you expect it to print?

You started with the string literal "A". As you may know, a string in C is internally represented as an array of characters. So you had an array of characters, which you attempted to assign to a single character. What did you expect would happen? Did you expect it to automatically take the first character from the array,. or something (and throw away the rest)? Well, that's not what happens. (It's not clear what should happen; there's probably no reasonable thing that could happen.)

But in fact, I lied: you didn't attempt to take an array of characters, and attempt to assign it to a single character. Most of the time, including here, when you try to use the value of an array, what you get is a pointer to the array's first element. So you actually had a pointer to some characters, which you attempted to assign to a single character. But again, what did you expect would happen? Did you expect it to automatically take the pointed-to character? Well, that's not what happens, either. (And, again, it's not clear what should happen.)

It sounds like what happened is that you got one byte of the pointer value, which happened to have the value 4, and which printed out on your display as a question mark.

Why does this not raise an error?

Well, it looks like you're handicapping yourself by using a very old compiler. A modern C compiler would definitely warn about the type mismatch (pointer versus char) in your erroneous initialization.

But then the other reason is that C is an old-school language, that may not match your expectations.

As a general rule, C compilers don't work very hard to protect you from yourself. They assume that you know what you are doing. This can simultaneously be very liberating and rather terrifying.

C can be a very powerful tool -- but you simply have to learn it first, and you have to learn what to be careful of. If you want to be able to just blunder around trying stuff, if you expect the language to try to figure out what you meant, and to hold you by the hand when you're confused or imprecise, and warn you about everything that might be wrong, then you are going to find C to be an eternally frustrating language to use, because that's just not the way it works.

See also this question about whether it's a good idea to enable a C compiler's non-default warnings.

Steve Summit
  • 45,437
  • 7
  • 70
  • 103
1

In C++ (and C), double quotes " " are used to represent string literals, not characters. Only single quotes ' ' are valid for declaring the char data type like this:

char c = 'a';

So, when you try to print this, it just outputs garbage.


Concerning your declaration, it is the first time that I see two types declared on the same variable, this should definitely throw an error.
liakoyras
  • 1,101
  • 12
  • 27
  • 1
    https://learn.microsoft.com/en-us/cpp/c-language/c-string-literals?view=vs-2019 A "string literal" is a sequence of characters from the source character set enclosed in double quotation marks (" "). – liakoyras Sep 09 '19 at 16:59
0

I don´t know how you test your code, because the line char int throws an error. If you add only one type your output will be a zero character. This behavior occurs because of the last character of a string in C which is a \0 ([null termination])(https://en.wikipedia.org/wiki/Null-terminated_string). So the compiler replaces your A with A\0 if you use "". When you use '' the compiler doesn´t add the null termination and interpret the character as a single value.

Kampi
  • 1,798
  • 18
  • 26