ID Number: Q68475
5.10 6.00 6.00a 6.00ax 7.00 | 5.10 6.00 6.00a
MS-DOS | OS/2
Summary:
SYMPTOMS
In Microsoft C versions 5.1, 6.0, 6.0a, 6.0ax, and C/C++ version 7.0
the compiler error:
C2118: negative subscript
may be caused by an integer expression in the subscript of an
array. If the expression evaluates to greater than 32768, its value
becomes negative, thus causing the aforementioned error.
RESOLUTION
To work around this situation, add an uppercase (or lowercase) "L"
to one of the terms of the expression. This will force the compiler
to use long math to evaluate the expression.
STATUS
This is correct and expected behavior for the Microsoft C Compiler
because the evaluation of integer expressions is done using integer
math. In this case, integer math produces an incorrect result
because the value of the expression is larger than an integer. By
using a long integer as one of the operands, you can force the
expression to be evaluated using long math, which does not overflow
in this case.
More Information:
For the sample code below, C/C++ version 7.0 will generate the following
additional warning message:
C4307: '*' : integral constant overflow; result truncated
Sample Code
-----------
/* Compile options needed: <none>
*/
#include <stdio.h>
char foo[1000 * 33];
If the above two-line program is compiled, the following error is
generated:
file.c(2) : error C2118: negative subscript
To eliminate the error, add "L" to one of the terms to indicate it is
a long constant. For example:
#include <stdio.h>
char foo[1000L * 33];
Additional reference words: 5.10 6.00 6.00a 6.00ax