DOCUMENT:Q68389 18-DEC-2001 [visualc] TITLE :INFO: sizeof(char Expression) Same as sizeof(int) PRODUCT :Microsoft C Compiler PROD/VER::1.0,1.5,2.0,4.0,5.0,6.0 OPER/SYS: KEYWORDS:kbcode kbCompiler kbVC100 kbVC150 kbVC200 kbVC400 kbVC500 kbVC600 ====================================================================== ------------------------------------------------------------------------------- The information in this article applies to: - Microsoft C/C++ for MS-DOS - Microsoft Visual C++ for Windows, 16-bit edition, versions 1.0, 1.5 - Microsoft Visual C++, 32-bit Editions, versions 1.0, 2.0, 4.0, 5.0, 6.0 ------------------------------------------------------------------------------- SUMMARY ======= In Microsoft C/C++, expressions involving variables of type "char" are promoted to type "int". MORE INFORMATION ================ This is ANSI-specified behavior. Below is Section 3.3.7 from the ANSI specifications, which details the semantics of the shift operator: Semantics The integral promotions are performed on each of the operands. The type of the result is that of the promoted left operand. If the value of the right operand is negative or is greater than or equal to the width in bits of the promoted left operand, the behavior is undefined. This means that chars are promoted to integers by default. If you really want a char result, you must cast the final result. The ANSI-specified semantics of all operators specify promotion from char to int, so the size of any char expression will be the sizeof int. This was also the case for Kernighan and Ritchie (K & R) C. The sizes of the int and long expressions stay the same because no promotion takes place. Note that if int is the same size as long rather than short in this implementation, the sizeof both a short expression and a char expression will be 4, as will be the sizeof both an int and a long expression. Sample Code ----------- /* Compile options needed: none */ #include void main(void) { short si; long li; char sc; unsigned char uc; printf("Signed char width: %d\n", sizeof((char)(sc<<1))); // 1 byte printf("Signed char width: %d\n", sizeof(sc<<1)); // 2 bytes printf("Unsigned char width: %d\n", sizeof((unsigned char)uc<<1)); // 2 bytes printf("Unsigned char width: %d\n", sizeof(uc<<1)); // 2 bytes printf("Short width: %d\n", sizeof(si<<1)); // 2 bytes printf("Long width: %d\n", sizeof(li<<1)); // 4 bytes } Additional query words: 8.00 8.00c 9.00 ====================================================================== Keywords : kbcode kbCompiler kbVC100 kbVC150 kbVC200 kbVC400 kbVC500 kbVC600 Technology : kbVCsearch kbVC400 kbAudDeveloper kbZNotKeyword8 kbvc150 kbvc100 kbZNotKeyword3 kbVC500 kbVC600 kbVC200 kbVC32bitSearch kbVC16bitSearch kbVC500Search Version : :1.0,1.5,2.0,4.0,5.0,6.0 Issue type : kbinfo ============================================================================= THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY. Copyright Microsoft Corporation 2001.