“签名”交易的实际用途是什么?关键词? [英] What is the actual use of "signed" keyword?
问题描述
我知道无符号整数只能是正数(和0),并且值可以是普通int的两倍。
I know that unsigned integers are only positive numbers (and 0), and can have double the value compared to a normal int. Are there any difference between
int variable = 12;
并且:
signed int variable = 12;
何时以及为什么要使用带符号的关键字?
When and why should you use the signed keyword?
推荐答案
只有一个实例可能需要使用签名
关键字。 带符号的字符
始终与普通类型不同。 char
可以是带符号的也可以是无符号的类型,具体取决于
There is only one instance where you might want to use the signed
keyword. signed char
is always a different type from "plain" char
, which may be a signed or an unsigned type depending on the implementation.
C ++ 14 3.9.1 / 1说:
C++14 3.9.1/1 says:
由实现定义的是
char
对象是否可以包含负值。可以将字符显式声明为unsigned
或signed
。普通char
,签名字符
和未签名字符
独特的类型[...]
It is implementation-defined whether a
char
object can hold negative values. Characters can be explicitly declaredunsigned
orsigned
. Plainchar
,signed char
, andunsigned char
are three distinct types [...]
在其他情况下,签名
是多余的。
In other contexts signed
is redundant.
在C ++ 14之前(以及在C中),还有第二种情况:位域。例如, int x:2;
(在类的声明中)是否与 unsigned int x相同,这是实现定义的: 2;
或与 signed int x:2
相同。
Prior to C++14, (and in C), there was a second instance: bit-fields. It was implementation-defined whether, for example, int x:2;
(in the declaration of a class) is the same as unsigned int x:2;
or the same as signed int x:2
.
C ++ 11 9.6 / 3说:
C++11 9.6/3 said:
这是实现-定义是否为普通(既不明确签名也不签名)
char
,short
,int
,long
或long long
位字段已签名或未签名。
It is implementation-defined whether a plain (neither explicitly signed nor unsigned)
char
,short
,int
,long
, orlong long
bit-field is signed or unsigned.
但是,自C ++ 14起,此内容已更改,因此 int x:2;
始终表示 signed int
。 链接到讨论
However, since C++14 this has been changed so that int x:2;
always means signed int
. Link to discussion
这篇关于“签名”交易的实际用途是什么?关键词?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!