What returns sizeof (int) in this case?

#include<stdio.h>
#include<conio.h>

void main()
{   
    if(sizeof(int)>=-2)    
        printf("True");
    else
        printf("False");
}

When I try to compile this piece of code using Turbo C ++, it returns False instead of True. But when I tried to print the int value, the program will return 2.

How is this possible? but sizeof(int)returns 2 and yes 2> = - 2.

+4
source share
2 answers

sizeof(int)replaced by a type std::size_tthat is unsigned for most implementations .

Comparison of the signed with the unsigned leads to a strange result due to the fact that the signed position is being promoted without sign.

You can get a reasonable result as shown below

if(static_cast<int>(sizeof(int)) >= -2)

If you work with a compiler C

if((int)sizeof(int) >= -2)

-Wall, , , , / . ( )

+16

sizeof size_t (typedef unsigned int size_t). unsigned int .

+2

All Articles