简体   繁体   English

定义int时的sizeof((int)(float)(char)i))(int)(float)(char)i;

[英]sizeof((int)(float)(char)i)) when int is defined (int)(float)(char) i;

#include<stdio.h>
double i;

int main()
{
    (int)(float)(char) i;
    printf("%d", sizeof((int)(float)(char)i));
    return 0;
}

The above outputs 4 on a Micrsoft compiler. 上面在Micrsoft编译器上输出4。 Why? 为什么?

sizeof is the size, in bytes, of the variable. sizeof是变量的大小(以字节为单位)。 In this case, i is being cast to an int which is 4 bytes. 在这种情况下, i被强制转换为4字节的int

These are the sizes of types on MS C++: http://msdn.microsoft.com/en-us/library/cc953fe1(v=vs.71).aspx 这些是MS C ++上类型的大小: http : //msdn.microsoft.com/zh-cn/library/cc953fe1(v=vs.71).aspx

The last cast operation is to int, so u will get the sizeOf(int). 最后的强制转换操作是对int的操作,因此您将获得sizeOf(int)。 Integer size differes form compiler to another, some return 2-bytes and onther return 4-bytes. 整数大小与另一种形式的编译器不同,一些返回2字节,而另一些返回4字节。

sizeof something tells you how many bytes it takes up in memory. sizeof告诉您它在内存中占用了多少字节。 Your platform and toolchain clearly has 32-bit integers; 您的平台和工具链显然具有32位整数。 it's telling you that int , on your toolchain (compiler), takes up 4 bytes. 告诉您在工具链(编译器)上的int占用4个字节。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM