c語言裡的char大小到底是4還是1?

時間 2021-05-06 01:34:19

1樓:

to store any member of the basicexecution character set. If a member of the basic execution character set is stored in a char object, its value is guaranteed to be positive. If any other character is stored in a char object, the resulting value is implementation-dened but shall be within the range of values that can be represented in that type.

所以,sizeof的結果其實告訴我們的是:多少個char,而不是多少個byte。但是,如果結合sizeof後半段關於array和struct都用了bytes,所以,唯一合理的解釋就是:

乙個char就是乙個byte。(我個人估計標準委員會那幫傢伙寫暈頭了?)

然後,sizeof('a')一定是sizeof(int)

6.4.4.4 Character constants

An integer character constant is a sequence of one or more multibyte characters enclosed in single-quotes, as in 'x'.

Aninteger characterconstant has typeint.

接著,再解釋一下sizeof(*s+0)。

這其實是發生了算數提公升:

6.3.1 Arithmetic operands

The following may be used in an expression wherever an int or unsigned int may be used:

— An object or expression with aninteger typewhose integer conversion rank isless

than the rank of int and unsigned int.也就是,小於int的整形(char也是),在運算中會提公升為int運算,那結果自然也是int了。

最後,再解釋一下byte和bit的關係:

3.61 byte

addressable unit of data storage large enough to hold any member of the basic character set of the execution environment

2 NOTE 1 It is possible to express the address of each individual byte of an object uniquely.

3 NOTE 2 A byte is composed of a contiguous sequence of bits,the number of which is implementation-dened. The least signicant bit is called the low-order bit; the most signicant bit is called the high-order bit.但是這個實現相關也不是天馬行空的,至少規定了下限:

large enough to hold any member of the basic character set

而在5.2.1的定義,basic character set有95個:

26*2的大小寫字母,10個數字、29個符號、4個特殊字元,那也就是起碼要7個bit。再結合6.2.

5中char的定義,必須要求是正數,那就要再加乙個符號位:

If a member of the basic execution character set is stored in a char object, its value isguaranteed to be positive那也就是說,1個byte起碼要8個bit。也就是說,理論上可能會出現1byte=9bit的系統,但是不可能出現1byte=7bit的系統。

2樓:

char*s

="hello"

;printf

("sizeof char is %lu, sizeof char* is %lu, sizeof 'a' is %lu\n"

,sizeof

(char

),sizeof

(char*),

sizeof

('a'

));//char 字元大小 1byte char* 指標 64位的機子為 8byte 『a』為int 4byte

printf

("sizeof s is %lu\n"

,sizeof(*

s+0));

//*s為陣列第乙個,'h' +0 轉化為 int 4byteprintf

("sizeof s is %lu\n"

,sizeof(*

s));

//*s 陣列第乙個元素 』h『 1byte

異性發小到底是種怎樣的存在?

小奶片 我有倆是從上幼兒園到初三畢業都乙個班那種十一年的感情上了高中就沒怎麼聯絡了 其實當時初中還乙個班的時候沒覺得有啥就生活裡很平常的角色大概也是習慣了到了畢業才發現原來他們陪伴我那麼多年啊?上了高中之後總是免不了懷舊見不了面就只能寫下來想給他們看見字如面。多麼美好的乙個詞啊。開學時候互相問問在哪...

c語言如何判斷乙個宣告到底是函式還是指標還是陣列還是什麼東西?

yuantj 從裡到外一步步看。設要求的型別為 T。第一步 找到識別符號名 T f第二步 注意到識別符號的左側有 符號,所以這是個指標 設被指型別為 F,則變成了 F f 第三步 識別符號的右側有 3 說明這是個含有 3 個指標的陣列 F f 3 第四步 外圍有函式指標的宣告,證明 F 是乙個函式型...

c語言中的引用到底是什麼原理呢,為什麼能改變原來的值

已登出 C語言沒有引用,引用是C 裡的東西。C 裡的引用就是 物件的別名 注意這裡的 物件 不是指乙個類例項化後的物件,而是指 記憶體中的資料 也就是說,像下面的內建型別變數,也是乙個 物件 inta 0 定義乙個int物件,其位於記憶體中的某個位置,我們通過識別符號a可以訪問它 除了通過名字a來訪...