C中sizeof()返回值的正确格式说明符

时间:2014-12-04 14:03:36

标签: c string types string-formatting

我有以下代码:

#include<stdio.h>

int main()
{
    printf("The 'int' datatype is \t\t %lu bytes\n", sizeof(int));
    printf("The 'unsigned int' data type is\t %lu bytes\n", sizeof(unsigned int));
    printf("The 'short int' data type is\t %lu bytes\n", sizeof(short int));
    printf("The 'long int' data type is\t %lu bytes\n", sizeof(long int));
    printf("The 'long long int' data type is %lu bytes\n", sizeof(long long int));
    printf("The 'float' data type is\t %lu bytes\n", sizeof(float));
    printf("The 'char' data type is\t\t %lu bytes\n", sizeof(char));
}

哪个输出:

The 'int' datatype is        4 bytes
The 'unsigned int' data type is  4 bytes
The 'short int' data type is     2 bytes
The 'long int' data type is  8 bytes
The 'long long int' data type is 8 bytes
The 'float' data type is     4 bytes
The 'char' data type is      1 bytes

但这就是问题,编译器要求我使用%lu(long unsigned int)而不是%d(int),正如我所期望的那样。毕竟,我们这里只讨论单位数字,不是吗?那么为什么在使用%d而不是%lu时会出现以下错误?是否与我在64位系统(Ubuntu 14.10)上有关?

helloworld.c: In function ‘main’:
helloworld.c:5:5: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘long unsigned int’ [-Wformat=]
     printf("The 'int' datatype is \t\t %d bytes\n", sizeof(int));
     ^
helloworld.c:6:5: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘long unsigned int’ [-Wformat=]
     printf("The 'unsigned int' data type is\t %d bytes\n", sizeof(unsigned int));
     ^
helloworld.c:7:5: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘long unsigned int’ [-Wformat=]
     printf("The 'short int' data type is\t %d bytes\n", sizeof(short int));
     ^
helloworld.c:8:5: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘long unsigned int’ [-Wformat=]
     printf("The 'long int' data type is\t %d bytes\n", sizeof(long int));
     ^
helloworld.c:9:5: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘long unsigned int’ [-Wformat=]
     printf("The 'long long int' data type is %d bytes\n", sizeof(long long int));
     ^
helloworld.c:10:5: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘long unsigned int’ [-Wformat=]
     printf("The 'float' data type is\t %d bytes\n", sizeof(float));
     ^
helloworld.c:11:5: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘long unsigned int’ [-Wformat=]
     printf("The 'char' data type is\t\t %d bytes\n", sizeof(char));
     ^
Compilation finished successfully.

3 个答案:

答案 0 :(得分:4)

您正在尝试打印sizeof operator的返回值,该值通常为size_t类型。

在您的情况下,size_t似乎是typedef long unsigned int,因此它要求使用兼容的格式说明符%lu。这里返回的值无关紧要,问题在于类型不匹配。

注意:要拥有可移植代码,在基于C99和转发标准的编译器上使用%zu是安全的。

答案 1 :(得分:3)

从技术上讲,由于格式和数据类型不匹配,您有未定义的行为

您应该使用%zu作为与sizeof相关联的类型(size_t)。 例如:

 printf("The 'int' datatype is \t\t %zu bytes\n", sizeof(int));

如果您打算同时针对32位和64位平台,这一点尤其重要。

参考:http://en.cppreference.com/w/c/io/fprintf

答案 2 :(得分:3)

在C中,sizeof表达式的类型为size_t

要使用的printf说明符为%zu。例如:

printf("The 'int' datatype is \t\t %zu bytes\n", sizeof(int));

自1999年版C标准以来,已经可以使用它。

相关问题