詳談signed 關(guān)鍵字
我們都知道且經(jīng)常用到 unsigned 關(guān)鍵字,但有沒有想過,與此對應(yīng)的 signed 關(guān)鍵字有啥用?
int i = 0;
signed int i = 0;
這倆有區(qū)別嗎?沒區(qū)別,看起來,signed 完全是個(gè)累贅。
真的是這樣嗎?
我查閱了 C++11 的標(biāo)準(zhǔn)文檔(草稿N3690),發(fā)現(xiàn)一些端倪:
3.9.1 Fundamental types
Objects declared as characters(char) shall be large enough to store any member of the implementation's basic character set. If a character from this set is stored in a character object, the integral value of that character object is equal to the value of the single character literal form of that character. It is implementation-defined whether a char object can hold negative values. Characters can be explicitly declared unsigned or signed. Plain char, signed char, and unsigned char are three distinct types, collectively called narrow character types. A char,a signed char,and an unsigned char occupy the same amount of storage and have the same alignment requirements(3.11); that is,they have the same object representation. For narrow character types, all bits of the object representation participate in the value representation. For unsigned narrow character types, all possible bit patterns of the value representation represent numbers. These requirements do not hold for other types. In any particular implementation, a plain char object can take on either the same values as a signed char or an unsigned char; which one is implementation-defined.
標(biāo)準(zhǔn)規(guī)定的很清楚,char, signed char 和 unsigned char 是三種不同的類型。 char 會(huì)根據(jù)具體實(shí)現(xiàn)場景,而決定到底是 signed 還是 unsigned.
再看看 C11 的標(biāo)準(zhǔn)文檔(ISO/IEC 9899:201x)呢?
6.7.2 Type specifiers
Each of the comma-separated multisets designates the same type, except that for bit-fields, it is implementation-defined whether the specifier int designates the same type as signed int or the same type as unsigned int.
看來,bit-fields (位域) 也存在同樣的問題。(位域的概念可能也有點(diǎn)偏,經(jīng)常寫比較底層的接口或協(xié)議童鞋應(yīng)該熟悉,可參考這里)
結(jié)論
在 C/C++ 中,signed 關(guān)鍵字絕大多數(shù)情況下都是累贅,但對于上述所言的兩種情況,即在 char 與 bit-fields 的使用過程中,還是有比較隱晦的作用的。
給自己提個(gè)醒,總是好的。
上一篇:C語言函數(shù)語法詳解
欄 目:C語言
本文標(biāo)題:詳談signed 關(guān)鍵字
本文地址:http://mengdiqiu.com.cn/a1/Cyuyan/3161.html
您可能感興趣的文章
- 01-10深入理解C++中常見的關(guān)鍵字含義
- 01-10淺談C/C++中的static與extern關(guān)鍵字的使用詳解
- 01-10探討C語言中關(guān)鍵字volatile的含義
- 01-10C++關(guān)鍵字typename的深入理解
- 01-10C語言關(guān)鍵字auto與register的深入理解
- 01-10求32位機(jī)器上unsigned int的最大值及int的最大值的解決方法
- 01-10C語言關(guān)鍵字大全(共32個(gè))
- 01-10深入解析unsigned int 和 int
- 01-10關(guān)于C/C++中static關(guān)鍵字的作用總結(jié)
- 01-10C++ Explicit關(guān)鍵字詳細(xì)解析


閱讀排行
本欄相關(guān)
- 04-02c語言函數(shù)調(diào)用后清空內(nèi)存 c語言調(diào)用
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言的正則匹配函數(shù) c語言正則表達(dá)
- 04-02c語言用函數(shù)寫分段 用c語言表示分段
- 04-02c語言中對數(shù)函數(shù)的表達(dá)式 c語言中對
- 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排
- 04-02c語言沒有round函數(shù) round c語言
- 04-02c語言分段函數(shù)怎么求 用c語言求分段
- 04-02C語言中怎么打出三角函數(shù) c語言中怎
- 04-02c語言調(diào)用函數(shù)求fibo C語言調(diào)用函數(shù)求
隨機(jī)閱讀
- 08-05織夢dedecms什么時(shí)候用欄目交叉功能?
- 01-10C#中split用法實(shí)例總結(jié)
- 01-10使用C語言求解撲克牌的順子及n個(gè)骰子
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 01-11ajax實(shí)現(xiàn)頁面的局部加載
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 01-10delphi制作wav文件的方法
- 04-02jquery與jsp,用jquery
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改