Lui*_*a13 19 c printf compilation sizeof
我一直收到编译警告,但我不知道如何解决它:
'%d' expects argument of type 'int', but argument 2 has type 'long unsigned int' [
Run Code Online (Sandbox Code Playgroud)
程序运行正常,但我仍然收到编译警告:
/* Sizeof.c--Program to tell byte size of the C variable */
#include <stdio.h>
int main(void) {
printf("\nA Char is %d bytes", sizeof( char ));
printf("\nAn int is %d bytes", sizeof( int ));
printf("\nA short is %d bytes", sizeof( short ));
printf("\nA long is %d bytes", sizeof( long ));
printf("\nA long long is %d bytes\n", sizeof( long long ));
printf("\nAn unsigned Char is %d bytes", sizeof( unsigned char ));
printf("\nAn unsigned int is %d bytes", sizeof( unsigned int));
printf("\nAn unsigned short is %d bytes", sizeof( unsigned short ));
printf("\nAn unsigned long is %d bytes", sizeof( unsigned long ));
printf("\nAn unsigned long long is %d bytes\n",
sizeof( unsigned long long ));
printf("\nfloat is %d bytes", sizeof( float ));
printf("\nA double is %d bytes\n", sizeof( double ));
printf("\nA long double is %d bytes\n", sizeof( long double ));
return 0;
}
Run Code Online (Sandbox Code Playgroud)
Sha*_*our 26
sizeof返回size_t您需要使用%zu格式字符串而不是%d.所述类型的无符号整数的size_t可以变化(取决于平台),也有可能长UNSIGNED INT无处不在,其在C99草案标准部覆盖6.5.3.4 sizeof运算符段4:
结果的值是实现定义的,其类型(无符号整数类型)是size_t,在(和其他头文件)中定义.
另请注意,使用错误的格式说明符printf是未定义的行为,这在7.19.6.1 fprintf函数一节中有所介绍,该函数也涉及printf格式说明符:
如果转换规范无效,则行为未定义.248)如果任何参数不是相应转换规范的正确类型,则行为未定义.
更新
Visual Studio 不支持z格式说明符:
不支持hh,j,z和t长度前缀.
在这种情况下,正确的格式说明符将是%Iu.