的sizeof()运算符在if语句 [英] sizeof() operator in if-statement

查看:140
本文介绍了的sizeof()运算符在if语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 的#include<&stdio.h中GT;
     主要()
     {
         如果(的sizeof(int)的-1个)
             的printf(真);
         其他
             的printf(假);
     }

它打印。为什么不和sizeof()的返回值如果


解决方案

  1. 的sizeof 不是一个函数,这是一个运营商。括号不是经营者的名称的一部分。

  2. 它的失败,因为生成的值具有无符号的类型为size_t ,这将导致通常的算术转换,其中 1 转换为无,在这种情况下,这是一个非常大的数字。

基本上你比较 4&GT; 0xffffffffu 或接近的东西,至少。 <一href=\"http://stackoverflow.com/questions/2280663/in-a-c-ex$p$pssion-where-unsigned-int-and-signed-int-are-$p$psent-which-type-will\">See这个问题的详情。

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

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

解决方案

  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.

这篇关于的sizeof()运算符在if语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆