BUG: Incorrect Code Generated for Long Constant

Last reviewed: July 22, 1997
Article ID: Q111767
7.00 | 1.00 1.50 MS-DOS | WINDOWS kbtool kbbuglist

The information in this article applies to:

  • The Microsoft C/C++ Compiler (CL.EXE), included with:

        - Microsoft C/C++ for MS-DOS, version 7.0
        - Microsoft Visual C++ for Windows, versions 1.0 and 1.5
    

SYMPTOMS

Incorrect code is generated when using a long constant in a conditional expression in a for or a while statement.

If attempting to compile using Microsoft Visual C++ versions 1.0 or 1.5, the following warning may be generated under warning level 2 or higher

   warning C4309: '*' : truncation of constant value

where '*' is the relational operator used in the condition.

There is no warning or error message with version 7.0 of the C/C++ compiler; however, the problem still occurs.

CAUSE

The compiler generates incorrect code only if the following conditions are met:

  • The fast compiler is used. Under debug mode, this is the default.
  • The code is compiled as a C++ module.
  • A long constant is used in a relational expression in a for or a while statement.
  • The long constant has a value greater than the range of integer values (greater than +32767 or less than -32768).

RESOLUTION

To avoid the problem, you can do any of the following:

  • Compile without using the fast compiler (/f-).
  • Because the error occurs only with a signed long constant, either use a variable or an unsigned long constant.

STATUS

Microsoft has confirmed this to be a problem in C/C++ version 7.0 and Visual C++ for Windows, versions 1.0 and 1.5. We are researching this problem and will post new information here in the Microsoft Knowledge Base as it becomes available.

This is not a problem in the 32-bit compilers.

MORE INFORMATION

The following sample code will produce a warning at the line indicated when compiled with Visual C++ version 1.0 or 1.5 under warning level 2 or above. No warning will be generated with version 7.0. In either case, the body of the for loop will not be executed.

Sample Code

/* Compile options needed: /f and </W3 or /W4>
* * Compile this sample code as a .CPP file
*/

#include <stdio.h>

const long bignum = 131072L;

void main()
{
     for (long l = 0L; l < bignum; l++)
     {
          printf("%ld\n",l);     // This line never executes.
     }     // warning C4309: '<' : truncation of constant value
}


Additional reference words: 1.00 1.50 7.00 8.00 8.00c
KBCategory: kbtool kbbuglist
KBSubcategory: CodeGen
Keywords : kb16bitonly


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.

Last reviewed: July 22, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.