Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

#include <stdio.h>
int main(void)
{
    if (sizeof(int) > -1)
        printf("True");
    else
        printf("False");
}

It prints False. Why doesn't sizeof() return a value in the if?

question from:https://stackoverflow.com/questions/17293749/sizeof-operator-in-if-statement

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
269 views
Welcome To Ask or Share your Answers For Others

1 Answer

  1. sizeof is not a function, it's an operator. The parentheses are not part of the operator's name.
  2. It's failing because the value generated has the unsigned type size_t, which causes "the usual arithmetic conversions" in which -1 is converted to unsigned, in which case it's a very large number.

Basically you're comparing 4 > 0xffffffffu, or something close to that at least. See this question for details.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...