在此以下的代码:sizeof运算符在C中如何工作?
#include<stdio.h>
int main(void)
{
printf("%d",sizeof(int));
return 0;
}
当上的gcc编译(Ubuntu的4.8.4-2ubuntu1〜14.04.3)4.8.4编译它给出警告:
格式 '%d' 预计参数类型'int',但参数2的类型为 'long unsigned int'[-Wformat =] printf(“%d”,sizeof(int));
为什么我得到这个警告? sizeof的返回类型是'long unsigned int'吗?
当我用'%ld'取代'%d'时,警告消失了。
'sizeof'是运营商,而不是一个函数。 – Chirality
这个问题在这里得到了解答:http://stackoverflow.com/questions/24797843/why-is-sizeofint-less-than-1 – MayurK
为了避免这个警告,你必须把值转换为int。 这就像当你定义你自己的sizeof()运算符时,你需要一个像这样的类型, #define my-sizeof(type)(int *)(&type + 1) - (int *)(&type) – lazyborg