国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C > 正文

詳談signed 關鍵字

2020-01-26 15:10:41
字體:
來源:轉載
供稿:網(wǎng)友

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

復制代碼 代碼如下:

int i = 0;
signed int i = 0;

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

真的是這樣嗎?

我查閱了 C++11 的標準文檔(草稿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.

標準規(guī)定的很清楚,char, signed char 和 unsigned char 是三種不同的類型。 char 會根據(jù)具體實現(xiàn)場景,而決定到底是 signed 還是 unsigned.

再看看 C11 的標準文檔(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 (位域) 也存在同樣的問題。(位域的概念可能也有點偏,經(jīng)常寫比較底層的接口或協(xié)議童鞋應該熟悉,可參考這里)

結論

在 C/C++ 中,signed 關鍵字絕大多數(shù)情況下都是累贅,但對于上述所言的兩種情況,即在 char 與 bit-fields 的使用過程中,還是有比較隱晦的作用的。

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

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表

圖片精選

主站蜘蛛池模板: 万载县| 攀枝花市| 汉沽区| 大新县| 金门县| 青阳县| 泰州市| 循化| 崇仁县| 广宁县| 南宁市| 灵宝市| 永胜县| 同心县| 五大连池市| 开平市| 保定市| 龙海市| 墨玉县| 山阴县| 天峻县| 中西区| 纳雍县| 周宁县| 新巴尔虎左旗| 教育| 榆树市| 区。| 延寿县| 宝鸡市| 苍南县| 红安县| 安康市| 彭州市| 开原市| 新安县| 南靖县| 于田县| 昌宁县| 宁武县| 建瓯市|