程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> 關於C++ >> 詳談signed 症結字

詳談signed 症結字

編輯:關於C++

詳談signed 症結字。本站提示廣大學習愛好者:(詳談signed 症結字)文章只能為提供參考,不一定能成為您想要的結果。以下是詳談signed 症結字正文


我們都曉得且常常用到 unsigned 症結字,但有無想過,與此對應的 signed 症結字有啥用?


int i = 0;
signed int i = 0;

這倆有差別嗎?沒差別,看起來,signed 完整是個包袱。

真的是如許嗎?

我查閱了 C++11 的尺度文檔(草稿N3690),發明一些眉目:

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.

尺度劃定的很清晰,char, signed char 和 unsigned char 是三種分歧的類型。 char 會依據詳細完成場景,而決議究竟是 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 (位域) 也存在異樣的成績。(位域的概念能夠也有點偏,常常寫比擬底層的接口或協定童鞋應當熟習,可參考這裡)

結論

在 C/C++ 中,signed 症結字絕年夜多半情形下都是包袱,但關於上述所言的兩種情形,即在 char 與 bit-fields 的應用進程中,照樣有比擬隱晦的感化的。

給本身提個醒,老是好的。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved