char c;
c = '2';
printf("%d\n",c);
So this question is from my quiz, it is asking what is the display. The answer is 50, i tried by using program, but why it is 50? not 2 or anything?
The apostrophes mean "character literal", i.e. '2'
is not the integer 2, but instead the character 2, i.e. the glyph used to represent the single digit 2.
You print this value using %d
in printf()
, which means "signed integer", so you get the integer value of the character, often called the "code point" (or, classically, it's "ASCII value").
I think your code is not 100% clean, since char
might be unsigned
, you should cast to (int)
in the call to be clear, since int
is what %d
expects.