-1

Somewhat oddly, C treats character constants as type int rather than type char. For example, on an ASCII system with a 32-bit int and an 8-bit char, the code

char grade = 'B';

represents 'B' as the numerical value 66 stored in a 32-bit unit, but grade winds up with 66 stored in an 8-bit unit. Please explain this lines.

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
Riya
  • 25
  • 3

1 Answers1

1

grade is of type char, and it's initialized with an expression of type int. That's perfectly ordinary and legal, and the int value is implicitly converted to type char. A value of any arithmetic type can be implicitly converted to any other arithmetic type.

Such implicit conversions are why, 99% of the time, the fact that character constants are of type int is not a problem; such constants are converted as appropriate depending on the context.

(Note that in C++ character constants are of type char. Remember that C and C++ are two different languages.)

Petr Skocik
  • 58,047
  • 6
  • 95
  • 142
Keith Thompson
  • 254,901
  • 44
  • 429
  • 631