使用 zd列印資料型別的大小

2021-10-02 05:28:07 字數 888 閱讀 4221

c99和c11提供%zd轉換說明匹配sizeof的返回型別:

printf

("%zd"

,sizeof

(long

long))

;

如果使用%d

會得到以下報錯

warning: format specifies type

'int' but the argument has type

'unsigned long'

[-wformat]

printf(

"%d",sizeof(long long))

; ~~ ^~~~~~~~~~~~~~~~~

%lu1 warning generated.

可以看到,sizeof返回的型別是unsigned long而%d是int型別的說明符

%zd同樣適用於strlen()函式

#include

#include

intmain()

如果使用%d,同樣會報錯:

warning: format specifies type

'int' but the argument has type

'unsigned long'

[-wformat]

printf(

"%d",strlen(

"fdfsfsdsfsd"));

~~ ^~~~~~~~~~~~~~~~~~~~~

%lu1 warning generated.

注意如果系統不支援%zd,可使用%u或者%lu代替%zd。

C 資料型別的大小

一 c 1.幾條規則 1 char型別一般是8bit,但ansi c裡沒有硬性規定其長度,某些嵌入式編譯器可能是16bit 2 short和long型別的長度不相同 3 int型別通常同具體機器的物理字長相同 4 short通常是16bits,int通常是16bits or 32bits每種編譯器可...

C 資料型別大小

iostream void main 輸出結果 size of bool 1 bytes size of char 1 bytes size of unsigned char 1 bytes size of wchar t 2 bytes size of short 2 bytes size of ...

使用指標列印陣列的內容

寫乙個函式列印一維整型arr陣列的內容,不使用陣列下標,使用指標 思路 陣列名代表陣列的首元素位址,我們用int 指標接收陣列首元素位址也就是arr,在for迴圈中 迴圈範圍為 陣列長度 1 讓指標進行 操作並進行解引用,這樣得到陣列的每個位置元素,實現和arr索引同樣的效果。1.實現 includ...