Difference C and C ++ in sizeof ('x')

Possible duplicate:
Why are characters characters characters C characters instead of characters?

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

C ++ usually tends to be nothing more than a superset of C, so why do the two results diverge?

Edit Another refinement. This seems like a deliberate step by the standards committee, and I suggest that resizing the x would not have been done without good reason. I am interested in what is the reason.

+7
c ++ c
source share
3 answers

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

Edit : character literal type changes from int to char

Justification . This is necessary to improve the correspondence of argument types with an overloaded function. For example:

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

Preferably, this call corresponds to the second version of the function, and not the first

(Appendix C describes the incompatibility between C and C ++)

+16
source share

C ++ is not a superset of C. In particular, if you use the "current" versions, the compiler in C ++ 0x mode will overwhelm the C99 code.

+7
source share

Because C, 'x' is actually int, and in C ++ a char.

C ++ is trying to tighten up strong typing, which was slightly weaker in C.

+5
source share

All Articles