欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

詳談signed 關(guān)鍵字

 更新時(shí)間:2015年01月20日 17:25:09   投稿:hebedich  
c++中關(guān)鍵字有幾十個(gè),其中類型修飾關(guān)鍵字有l(wèi)ong, short, singed, unsigned。今天我們就來談一下經(jīng)常被大家忽視的signed關(guān)鍵字

我們都知道且經(jīng)常用到 unsigned 關(guān)鍵字,但有沒有想過,與此對(duì)應(yīng)的 signed 關(guān)鍵字有啥用?

復(fù)制代碼 代碼如下:

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

復(fù)制代碼 代碼如下:

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

復(fù)制代碼 代碼如下:

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ù)情況下都是累贅,但對(duì)于上述所言的兩種情況,即在 char 與 bit-fields 的使用過程中,還是有比較隱晦的作用的。

給自己提個(gè)醒,總是好的。

相關(guān)文章

  • C語言高效實(shí)現(xiàn)向量循環(huán)移位

    C語言高效實(shí)現(xiàn)向量循環(huán)移位

    這篇文章主要為大家詳細(xì)介紹了C語言高效實(shí)現(xiàn)向量循環(huán)移位,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-03-03
  • QT中線程池QThreadPool類概念和使用方法詳解

    QT中線程池QThreadPool類概念和使用方法詳解

    這篇文章主要為大家介紹了QT中線程池QThreadPool類概念和使用方法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-09-09
  • 淺析C++?atomic?和?memory?ordering

    淺析C++?atomic?和?memory?ordering

    這篇文章主要介紹了C++?atomic?和?memory?ordering的相關(guān)知識(shí),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-04-04
  • 關(guān)于C語言strlen與sizeof區(qū)別詳情

    關(guān)于C語言strlen與sizeof區(qū)別詳情

    對(duì)于 strlen 和 sizeof,相信不少程序員會(huì)混淆其功能。雖然從表面上看它們都可以求字符串的長度,但二者卻存在著許多不同之處及本質(zhì)區(qū)別,今天得這篇文章我們就來學(xué)習(xí)C語言strlen與sizeof區(qū)別的相關(guān)資料,需要的朋友可以參考一下
    2021-10-10
  • C語言數(shù)據(jù)結(jié)構(gòu)不掛科指南之隊(duì)列詳解

    C語言數(shù)據(jù)結(jié)構(gòu)不掛科指南之隊(duì)列詳解

    這篇博客主要介紹一下隊(duì)列的概念,并且采用 C 語言,編寫兩種存儲(chǔ)實(shí)現(xiàn)方式:順序存儲(chǔ)和鏈?zhǔn)酱鎯?chǔ),當(dāng)然還有常規(guī)的隊(duì)列基本操作的實(shí)現(xiàn)算法
    2022-09-09
  • C語言三子棋一步步實(shí)現(xiàn)詳程

    C語言三子棋一步步實(shí)現(xiàn)詳程

    三子棋是一種民間傳統(tǒng)游戲,又叫九宮棋、圈圈叉叉、一條龍、井字棋等。將正方形對(duì)角線連起來,相對(duì)兩邊依次擺上三個(gè)雙方棋子,只要將自己的三個(gè)棋子走成一條線,對(duì)方就算輸了,想用c語言做出這個(gè)游戲,事實(shí)上也是比較簡單的,下面通過c語言進(jìn)行對(duì)五子棋的分析
    2022-02-02
  • 實(shí)現(xiàn)去除c語言注釋的小工具

    實(shí)現(xiàn)去除c語言注釋的小工具

    這篇文章主要介紹了實(shí)現(xiàn)去除c語言注釋的小工具,說是C語言,但其實(shí)所有C語系的都可以,比如Java,需要的朋友可以參考下
    2014-02-02
  • 基于C語言實(shí)現(xiàn)簡單的掃雷游戲

    基于C語言實(shí)現(xiàn)簡單的掃雷游戲

    這篇文章主要為大家詳細(xì)介紹了基于C語言實(shí)現(xiàn)簡單的掃雷游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-10-10
  • C++使用fdk-aac實(shí)現(xiàn)將音頻PCM編碼成aac

    C++使用fdk-aac實(shí)現(xiàn)將音頻PCM編碼成aac

    mp4的音頻流通常是aac編碼,我們做音視頻采集的時(shí)候就需要將,采集的音頻PCM編碼成aac,本文就來為大家介紹一下C++如何使用fdk-aac實(shí)現(xiàn)將音頻PCM編碼成aac吧
    2023-11-11
  • 基于Qt實(shí)現(xiàn)電子木魚小游戲

    基于Qt實(shí)現(xiàn)電子木魚小游戲

    今年最火爆的解壓小游戲電子木魚,現(xiàn)在許多軟件都上架了這個(gè)小程序。我在網(wǎng)上看了一下基本上都是用py和Java寫的,所以我用QT重新寫了一下,作為小白練手項(xiàng)目非常適合,快跟隨小編一起學(xué)習(xí)一下吧
    2023-01-01

最新評(píng)論