PRB: Sizeof Constant Character Returns Different Values (150380)



The information in this article applies to:

  • Microsoft Visual C++, 32-bit Professional Edition 4.0
  • Microsoft Visual C++, 32-bit Professional Edition 4.1
  • Microsoft Visual C++, 32-bit Learning Edition 4.0

This article was previously published under Q150380

SYMPTOMS

The sizeof('x') or sizeof(L'x') constants do not return expected results. In C, the return value is 4 for the sizeof character constant and 2 for the sizeof multicharacter constant. In C++, the return value is 1 for the sizeof character constant and 2 for the sizeof multicharacter constant.

CAUSE

The specification on this point is different for C from what it is for C++. In C, a character constant is of type int. In C++, a character constant is of type char. Multicharacter constants are implementation dependent.

RESOLUTION

When using C, do not use sizeof to determine the width of a character constant. Use sizeof("") to give the width of one character in both C and C++.

STATUS

This behavior is by design.

REFERENCES

For more information, see "The Annotated C++ Reference Manual" by Margaret Ellis and Bjarne Stroustrup, Section 2.5.2.

Modification Type:MajorLast Reviewed:10/24/2003
Keywords:kbCompiler kbCRT kbprb KB150380