在32位计算机中,以下C代码的输出是什么?

时间:2019-09-04 13:30:43

标签: c

 #include<stdio.h>
    double i;
    int main()
    {
    (int)(float)(char) i;
    printf("%d",sizeof(i));
    return 0;
    }

它显示为8。有人可以解释一下为什么显示为8吗? Typecasting是否对变量i ...产生任何影响,以便输出可能为4?

1 个答案:

答案 0 :(得分:1)

(int)(float)(char) i;不是i的定义。它只是在毫无价值地使用该值。

#include <stdio.h>
double i;
int main(void) {
    i; // use i for nothing
    (int)i; // convert the value of i to integer, than use that value for nothing
    (int)(float)i; // convert to float, then to int, then use for nothing
    (int)(float)(char)i; // convert char, then to float, then to int, then use for nothing
    printf("sizeof i is %d\n", (int)sizeof i);
    char i; // define a new i (and hide the previous one) of type char
    printf("sizeof i is %d\n", (int)sizeof i);
}