The sizeof operator used in a comparison does not generate C4018 (117201)



The information in this article applies to:

  • Microsoft C for MS-DOS
  • Microsoft Visual C++ for Windows, 16-bit edition 1.0
  • Microsoft Visual C++ for Windows, 16-bit edition 1.5
  • Microsoft Visual C++, 32-bit Editions 1.0
  • Microsoft Visual C++, 32-bit Editions 2.0
  • Microsoft Visual C++, 32-bit Editions 4.0
  • Microsoft Visual C++, 32-bit Editions 4.1
  • Microsoft Visual C++, 32-bit Editions 4.2
  • Microsoft Visual C++, 32-bit Editions 5.0
  • Microsoft Visual C++, 32-bit Editions 6.0
  • Microsoft Visual C++ .NET (2002)
  • Microsoft Visual C++ .NET (2003)

This article was previously published under Q117201

SYMPTOMS

According to ANSI specifications, the sizeof operator returns the unsigned integer size of a particular data type or variable.

When the sizeof operator is used in a comparison with an integer variable, the compiler should, but does not, generate the following warning:
warning C4018: 'op' : signed/unsigned mismatch

STATUS

Microsoft has confirmed that this is a bug in the Microsoft products that are listed in the "Applies to" section.

MORE INFORMATION

The following sample code can be used to demonstrate the problem. The C4018 warning is produced for the statement in function3, but not for the statements in function1 and function2.

Sample code


/* Compile options needed: /W4
*/ 

int i;
unsigned u;

int function1(void)
{
   return(i > sizeof(i));
}

int function2(void)
{
   return(i > (unsigned) sizeof(i));
}

int function3(void)
{
   u = sizeof(i);
   return(i > u);
}
				

Modification Type:MinorLast Reviewed:7/5/2005
Keywords:kbBug kbCompiler KB117201