0

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

int i = sizeof('a');
haccks
  • 100,941
  • 24
  • 163
  • 252
debonair
  • 2,395
  • 4
  • 25
  • 60

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
  • 1,794
  • 1
  • 16
  • 29
2

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

Arkku
  • 39,548
  • 10
  • 58
  • 80
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,661
  • 9
  • 36
  • 52
haccks
  • 100,941
  • 24
  • 163
  • 252