Correct format specifier for return value of sizeof() in C

核能气质少年 提交于 2019-11-26 17:12:00

问题


I have the following code:

#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));
}

Which outputs:

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

But that's just the thing, the compiler requires that I use %lu(long unsigned int) rather than %d(int), as I would have expected. After all, we are just talking about single digit numbers here, aren't we? So why do I get the following error when using %d instead of %lu? Has it something to do with me being on a 64bit system(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.

回答1:


You're trying to print the return value of sizeof operator, which is usually of type size_t.

It appears, in your case, size_t is a typedef of long unsigned int, so it demands it's compatible format specifier %lu to be used. The returned value here does not matter, your problem is with the type mismatch.

Note: To have a portable code, it's safe to use %zu, on compilers based on C99 and forward standards.




回答2:


In C the type of a sizeof expression is size_t.

The printf specifier to use is %zu. Example:

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

It has been possible to use this since the 1999 version of the C standard.




回答3:


Technically, you have undefined behaviour due to mismatched format and data types.

You should use %zu for the type associated with sizeof (which is size_t). For example:

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

This is particularly important if you intend to target both 32 and 64 bit platforms.

Reference: http://en.cppreference.com/w/c/io/fprintf



来源:https://stackoverflow.com/questions/27296011/correct-format-specifier-for-return-value-of-sizeof-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!