无效的主要(){如果(的sizeof(int)的-1个)的printf(QUOT;真&QUOT);其他的printf(QUOT;假"); ;

无效的主要(){如果(的sizeof(int)的-1个)的printf(QUOT;真&QUOT);其他的printf(QUOT;假"); ;

问题描述:

void main()
{
    if(sizeof(int) > -1)
        printf("true");
    else
        printf("false");    
}

我所期望的输出是真实的,但它是假的。任何人都可以请解释我要输出的原因。

I expected the output to be true but it is false. Can anybody please explain me the reason for the output.

的sizeof(INT)的类型为为size_t ,它是一个无符号整型。所以在EX pression 如果(sizeof的(INT)-1个) 1 转换一个无符号整数,这是非常大的。

sizeof(int) is of type size_t, which is an unsigned integer type. So in the expression if(sizeof(int) > -1), -1 is converted to an unsigned integer, which is very big.

BTW,使用 INT主而不是非标准无效的主要

BTW, use int main instead of the non-standard void main.