2014-12-04 81 views
2

的sizeof()的返回值我有以下代碼:正確的格式說明在C

#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

'sizeof'產生一個'size_t'類型的數字。它的結果是否適合'int'並不重要 - 它是這樣定義的。不要使用'%lu',因爲它不可移植 - 'size_t'的(always-)正確格式說明符是'%zu'。 – 2014-12-04 14:06:10

+0

可能重複[如何打印大小\ _t變量portably?](http://stackoverflow.com/questions/2524611/how-to-print-size-t-variable-portably) – 2014-12-04 14:06:40

回答

4

你試圖打印的sizeof operator的返回值,這通常是size_t類型。

它的出現,你的情況,size_tlong unsigned int一個typedef,所以它要求它是兼容的格式說明%lu使用。這裏返回的值並不重要,你的問題是類型不匹配。

注意:要獲得便攜式代碼,在基於C99和轉發標準的編譯器上使用%zu是安全的。

+0

謝謝,我不知道在基礎教程中講述的是比通常講解更多的說明符。我以前從未見過%z。還有更多嗎? – Totem 2014-12-04 14:23:30

+0

@圖騰歡迎。肯定有。嘗試探索,你會驚訝。 :-) – 2014-12-04 14:28:45

+0

不適用於g ++ 4.7.2:'錯誤:ISO C++不支持'z'gnu_printf長度修飾符[-Werror = format]'在http://stackoverflow.com/找到了答案。 questions/2524611/how-to-print-size-t-variable-portably - use'#ifdef' ... – 2015-09-08 14:24:54

2

從技術上講,您有未定義的行爲由於格式和數據類型不匹配。

您應該使用%zusizeof關聯的類型(這是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

在C sizeof表達式的類型是size_t

要使用的printf說明符是%zu。例如:

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

它已經可以使用這個自1999年版本C標準的。