最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

詳談signed 關(guān)鍵字

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

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

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

int i = 0;
signed int i = 0;

這倆有區(qū)別嗎?沒區(qū)別,看起來,signed 完全是個累贅。

真的是這樣嗎?

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

給自己提個醒,總是好的。

相關(guān)文章

  • C++ 中std::vector<T>的幾種清除方式

    C++ 中std::vector<T>的幾種清除方式

    std::vector<T>?可以通過多種方式清除(刪除所有元素),本文主要介紹了C++ 中std::vector<T>的幾種清除方式,具有一定的參考價值,感興趣的可以了解一下
    2025-04-04
  • C++實(shí)現(xiàn)百度坐標(biāo)(BD09)及GCJ02與WGS84之間的轉(zhuǎn)換

    C++實(shí)現(xiàn)百度坐標(biāo)(BD09)及GCJ02與WGS84之間的轉(zhuǎn)換

    這篇文章主要為大家詳細(xì)介紹了C++實(shí)現(xiàn)百度坐標(biāo)(BD09)及GCJ02與WGS84之間的轉(zhuǎn)換的方法,文中的示例代碼講解詳細(xì),希望對大家有所幫助
    2023-03-03
  • C語言編寫實(shí)現(xiàn)學(xué)生管理系統(tǒng)

    C語言編寫實(shí)現(xiàn)學(xué)生管理系統(tǒng)

    這篇文章主要為大家詳細(xì)介紹了C語言編寫實(shí)現(xiàn)學(xué)生管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-07-07
  • C++學(xué)習(xí)之命名空間詳解

    C++學(xué)習(xí)之命名空間詳解

    C++中,命名空間(namespace)是一個重要的概念。命名空間可以為函數(shù)、變量、類等定義作用域,避免與其他定義的名稱發(fā)生沖突。下面我們就來了解一下如何使用C++命名空間,以及一些常見的操作吧
    2023-04-04
  • C++中Operator類型強(qiáng)制轉(zhuǎn)換成員函數(shù)解析

    C++中Operator類型強(qiáng)制轉(zhuǎn)換成員函數(shù)解析

    轉(zhuǎn)換函數(shù)定義了由<類型說明符1>到<類型說明符2>之間的映射關(guān)系。可見,轉(zhuǎn)換函數(shù)是用來將一種類型的數(shù)據(jù)轉(zhuǎn)換成為另一種類型
    2013-09-09
  • C語言音樂播放器實(shí)例代碼

    C語言音樂播放器實(shí)例代碼

    文章給大家分享了用C語言音樂播放器的實(shí)例代碼,對此有需要的朋友參考學(xué)習(xí)下。
    2018-07-07
  • QT實(shí)現(xiàn)貪吃蛇游戲代碼詳解

    QT實(shí)現(xiàn)貪吃蛇游戲代碼詳解

    本文主要為大家詳細(xì)介紹了在QT中實(shí)現(xiàn)貪吃蛇游戲的詳細(xì)教程,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-11-11
  • C++ opencv ffmpeg圖片序列化實(shí)現(xiàn)代碼解析

    C++ opencv ffmpeg圖片序列化實(shí)現(xiàn)代碼解析

    這篇文章主要介紹了C++ opencv ffmpeg圖片序列化實(shí)現(xiàn)代碼解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-08-08
  • 基于Qt實(shí)現(xiàn)的自定義樹結(jié)構(gòu)容器

    基于Qt實(shí)現(xiàn)的自定義樹結(jié)構(gòu)容器

    在Qt框架中,盡管其提供了許多強(qiáng)大的容器類,但缺少一個通用的、靈活的樹結(jié)構(gòu)容器,所以本文將設(shè)計并實(shí)現(xiàn)一個可復(fù)用的自定義樹結(jié)構(gòu)容器,需要的可以參考下
    2024-12-12
  • 詳解C++ new-handler機(jī)制

    詳解C++ new-handler機(jī)制

    這篇文章主要介紹了C++ new-handler機(jī)制的相關(guān)資料,幫助大家更好的理解和使用c++,感興趣的朋友可以了解下
    2020-11-11

最新評論

登封市| 外汇| 双柏县| 潞城市| 儋州市| 巍山| 高州市| 宜兰县| 辽阳市| 博罗县| 勐海县| 新营市| 青海省| 霍城县| 金昌市| 正蓝旗| 彭阳县| 萨迦县| 当阳市| 建始县| 桐城市| 临猗县| 长顺县| 惠州市| 黄浦区| 海原县| 奉新县| 大城县| 饶平县| 闸北区| 白沙| 大余县| 曲周县| 枝江市| 清河县| 罗定市| 新巴尔虎右旗| 浦城县| 重庆市| 玛沁县| 南皮县|