Q67079: Error C2141 When Initializing enum Constant to -32768
Article: Q67079
Product(s): See article
Version(s): 5.10 6.00 6.00a | 5.10 6.00 6.00a
Operating System(s): MS-DOS | OS/2
Keyword(s): ENDUSER | S_QUICKC buglist5.10 buglist6.00 buglist6.00a | mspl13_c
Last Modified: 4-DEC-1990
Although the range of values for an enum constant should be the same
as that for signed integer constants, the Microsoft C and QuickC
compilers do not allow a value of -32768 to be used as an initializer
for an enum constant.
-32767 (Ox8000) is the largest negative number that will fit into a
16-bit signed integer. The compilers will allow this value to be used
as an integer constant. If you try to use this value to initialize an
enum constant, the compilers will generate the following error
message:
error C2141: value out of range for enum constant
The sample program below demonstrates this limitation. Uncommenting
the "Hex8000" line and compiling will result in the C2141 error above.
Microsoft has confirmed this to be a problem in C versions 5.10, 6.00,
and 6.00a and QuickC versions 2.00, 2.01, 2.50, and 2.51 (buglist2.00,
buglist2.01, buglist2.50, and buglist2.51). We are researching this
problem and will post new information here as it becomes available.
Sample Code
-----------
#include <stdio.h>
int MaxNegative = 1 << 15; // -32768 : this works for int
enum {
Hex4000 = 1 << 14, // 16384 : this works for enum
Hex7FFF = (1 << 15) - 1, // 32767 : this is ok for enum
// Hex8000 = 1 << 15 // -32768 : this fails for enum
};
void main(void)
{
printf("\nHex4000 = %d\n", Hex4000);
printf("Hex7FFF = %d\n", Hex7FFF);
printf("MaxNegative = %d\n", MaxNegative);
}
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 1986-2002.