[previous] 1322 [next] In C, sizeof('c') == sizeof(int), but in C++, sizeof('c') == sizeof(char) == 1.
C++ compatibility REFERENCE - ISO:C90-6.1.3.4 Character Constants - Semantics

The sizeof operator has been applied to a character constant.

The result of this operation will be different in C and C++. In the C language a character constant is of type int and the result will (probably) be 2, 4 or 8 (bytes). In C++, its type is char and the result will always be 1 (byte).

For example:


/*PRQA S 702,3123,3199,3227,3408,3447,3470,3625++*/

extern unsigned long ux;
extern unsigned long uy;

extern void foo(char pc)
{
    ux = sizeof(pc);

    uy = sizeof('c');                           /* Message 1322 */
}

QA·C Source Code Analyser 8.1.2
© 2013 Programming Research.
www.programmingresearch.com
Personality Groups | Glossary | Message Index Contents