0

Why does this code produces different o/p in C and C++?

int i = sizeof('a');
haccks
  • 104,019
  • 25
  • 176
  • 264
debonair
  • 2,505
  • 4
  • 33
  • 73

3 Answers3

3

In C, the type of a character constant like 'a' is actually an int, with size of 4 (or some other implementation-dependent value). In C++, the type is char, with size of 1. This is one of many small differences between the two languages.

user376507
  • 2,004
  • 1
  • 19
  • 31
2

Character literals in C are ints, whereas in C++ they are chars. In any case sizeof(char) is always 1 by definition.

Arkku
  • 41,011
  • 10
  • 62
  • 84
1

This is because C and C++ define character literals differently. In C, character literals are treated as int while in C++ they are treated as char.

Joel
  • 4,732
  • 9
  • 39
  • 54
haccks
  • 104,019
  • 25
  • 176
  • 264