在这里会发生什么?的sizeof(short_int_variable + char_variable) [英] What happens here? sizeof(short_int_variable + char_variable)

查看:189
本文介绍了在这里会发生什么?的sizeof(short_int_variable + char_variable)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include <stdio.h>
 int main()        
{

           short int i = 20;

            char c = 97;

            printf("%d, %d, %d\n", sizeof(i), sizeof(c), sizeof(c + i));
            return 0;
}

能否有人告诉我发生什么事时的sizeof(A + B)一短整​​型和b是char类型
    输出是:2,1,4

Could some one tell me what happens when sizeof(a+b) "a is short int type & b is char type" Output is : 2, 1, 4

推荐答案

href=\"http://publib.boulder.ibm.com/infocenter/cellcomp/v101v121/index.jsp?topic=/com.ibm.xlcpp101.cell.doc/language_ref/cplr066.html\"相对=nofollow>积分促销规则中,前pression类型 C + I INT ,所以这就是为什么你得到的等价的sizeof(INT)

Because of C's standard integral promotion rules, the type of the expression c + i is int, so that's why you're getting the equivalent of sizeof (int).

注意的sizeof 不是一个函数,命名类型的并解决precendence冲突的。您code:水灾这样写:

Note that sizeof is not a function, the parenthesis are only needed when naming a type and to resolve precendence conflicts. Your code coule be written:

printf("%d, %d, %d\n", sizeof i, sizeof c, sizeof (c + i));

的sizeof 最后使用的有,因为括号中的sizeof 绑定比 + ,称 sizeof的C + I 将被解析为(sizeof的C)+ I 这是不期望的结果。

The final use of sizeof has parentheses since sizeof binds tighter than +, saying sizeof c + i would be parsed as (sizeof c) + i which is not the desired result.

另外请注意,这是一个编译时构建的大部分时间,前pression实际上从来不评价。所有这一切发生的是,编译器pretends来评价它,找出前pression的类型,然后给你一个该类型的值的大小。实际值永远不需要存在,这有时是整齐

Also note that it's a compile-time construct most of the time, the expression is never actually evaluated. All that happens is that the compiler "pretends" to evaluate it, to figure out the type of the expression, and then gives you the size of a value of that type. The actual value never needs to exist, which is sometimes neat.

这篇关于在这里会发生什么?的sizeof(short_int_variable + char_variable)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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