9

Possible Duplicate:
Why are C character literals ints instead of chars?

Why when using C does sizeof('x') return 4 but sizeof('x') in C++ returns 1?

C++ normally strives to be nothing more than a superset of C so why do the two results diverge?

Edit Just some further clarification. This seems like a deliberate move on the part of the standards committee and I assume changing the size of 'x' would not have been done without a good reason. I am interested in what the reason is.

Community
  • 1
  • 1
doron
  • 27,972
  • 12
  • 65
  • 103
  • 2
    More or less a duplicate of [Different sizeof results](http://stackoverflow.com/questions/49046/different-sizeof-results), [Why are C character literals ints instead of chars?](http://stackoverflow.com/questions/433895/) and [Understanding sizeof(char) in 32 bit C compilers](http://stackoverflow.com/questions/3451266/) and perhaps one or two others. Found with http://stackoverflow.com/search?q=[c]+[c%2B%2B]+sizeof+character+literal. – dmckee --- ex-moderator kitten Sep 08 '10 at 23:06
  • none of the duplicates explain why. – doron Sep 08 '10 at 23:13
  • @deus: It's just a design decision. The "c" way means the compiler has to make fewer decisions about type promotion ('cause "promotion" happens every time) which may have played a roll: the machines of the early seventies were slow and had limited memory, so simple compilers were a must. – dmckee --- ex-moderator kitten Sep 08 '10 at 23:16
  • See the answers from Malx and Johannes Schaub in [Why are C character literals ints instead of chars](http://stackoverflow.com/questions/433895/). – Matthew Slattery Sep 08 '10 at 23:17
  • "C++ normally strives to be nothing more than a superset of C" Where did you read that? Completely false. C++, in the beginning, had to be close to C so programmers could try to adopt it. But it's certainly wrong to say they *strive* to make it a superset. They're different languages, that's it. – GManNickG Sep 08 '10 at 23:18

3 Answers3

18

To quote the C++ standard ISO 14882:2003, annex C.1.1 clause 2.13.2

Change: Type of character literal is changed from int to char

Rationale: This is needed for improved overloaded function argument type matching. For example:

int function( int i );
int function( char c );
function( ’x’ );

It is preferable that this call match the second version of function rather than the first

(annex C describes the incompatibilities between C and C++)

Cubbi
  • 46,567
  • 13
  • 103
  • 169
  • More or less what I thought but nice to see in black and white. – doron Sep 08 '10 at 23:17
  • Of course, they *could* have just introduced a "`char` literal" syntax - eg `C'x'` is of type `char`, in the same way that `L'x'` is of type `wchar_t`. – caf Sep 09 '10 at 02:46
  • Note, a multicharacter literal eg `'abcd'` *is* still of type `int`, with implementation-defined value. – Potatoswatter Sep 09 '10 at 06:13
7

C++ is not a superset of C. Particularly if you use the "current" versions - a compiler in C++0x mode will choke on C99 code.

kenm
  • 23,127
  • 2
  • 43
  • 62
  • But that is because the (then) new C99 standard was not incorporated into C++ – doron Sep 08 '10 at 23:12
  • @deus-ex C99 was out for years before C++0x started. If they wanted to make C a subset, they would have included the changes. In fact, they did include some of them. `#include ` refers to C99's `stdint.h`. – kenm Sep 08 '10 at 23:19
  • C and C++ have been diverging slightly with recent versions of standards. I wouldn't expect C99 to be rolled into C++0x or vice versa, with respect to the common feature sets. –  Sep 08 '10 at 23:20
5

Because is C, 'x' is actually an int, while in C++ it's a char.

C++ tries is tighten up strong typing that was a bit lax in C.

James Curran
  • 101,701
  • 37
  • 181
  • 258
  • Intriguing behavior... I'm not much of a C guy but this surprises me. So when you call `printf("%c", 'x');`, 4 bytes are pushed onto the stack for the varargs, but in C++ it would only be 1 byte? How could that work, considering the CRT doesn't know what language is calling it? – tenfour Sep 08 '10 at 23:07
  • 4
    @tenfour, nope - 4 bytes are pushed in C++ too. varargs calls have lots of type promotion rules. – Carl Norum Sep 08 '10 at 23:08
  • 1
    @tenfour: In C++ variadic argument always go through integral promotions, meaning that `char` and `short` types are always promoted to `[unsigned] int` before being passed to a variadic function. The same thing happens in C. – AnT stands with Russia Sep 08 '10 at 23:12
  • 1
    learn something every day :) Now I wonder why a char literal is an `int` in C? – tenfour Sep 08 '10 at 23:15
  • @tenfour, see http://stackoverflow.com/questions/433895/why-are-c-character-literals-ints-instead-of-chars – Carl Norum Sep 08 '10 at 23:44