什么是无符号数据类型?
我见过这种unsigned
“无类型”类型使用过几次,但从未见过对此的解释。 我想有一个相应的 signed
类型。 这是一个例子:
static unsigned long next = 1;
/* RAND_MAX assumed to be 32767 */
int myrand(void) {
next = next * 1103515245 + 12345;
return((unsigned)(next/65536) % 32768);
}
void mysrand(unsigned seed) {
next = seed;
}
到目前为止我收集到的内容:
- 在我的系统上,sizeof(unsigned) = 4
(暗示 32 位无符号整数)
- 它可以用作将另一种类型转换为无符号版本的简写:
signed long int i = -42;
printf("%u\n", (unsigned)i);
这是 ANSI C,还是只是一个编译器扩展?
I've seen this unsigned
"typeless" type used a couple of times, but never seen an explanation for it. I suppose there's a corresponding signed
type. Here's an example:
static unsigned long next = 1;
/* RAND_MAX assumed to be 32767 */
int myrand(void) {
next = next * 1103515245 + 12345;
return((unsigned)(next/65536) % 32768);
}
void mysrand(unsigned seed) {
next = seed;
}
What I have gathered so far:
- on my system, sizeof(unsigned) = 4
(hints at a 32-bit unsigned int)
- it might be used as a shorthand for casting another type to the unsigned version:
signed long int i = -42;
printf("%u\n", (unsigned)i);
Is this ANSI C, or just a compiler extension?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
unsigned
实际上是unsigned int
的简写,在标准 C 中如此定义。unsigned
really is a shorthand forunsigned int
, and so defined in standard C.unsigned
表示unsigned int
。signed
表示signed int
。 仅使用unsigned
是在 C 中声明unsigned int
的一种懒惰方式。是的,这是 ANSI。unsigned
meansunsigned int
.signed
meanssigned int
. Using justunsigned
is a lazy way of declaring anunsigned int
in C. Yes this is ANSI.历史上,在 C 中,如果省略了数据类型“int”,则假定为“int”。 所以“unsigned”是“unsigned int”的简写。 长期以来,这一直被认为是不好的做法,但仍然有相当多的代码使用它。
Historically in C, if you omitted a datatype "int" was assumed. So "unsigned" is a shorthand for "unsigned int". This has been considered bad practice for a long time, but there is still a fair amount of code out there that uses it.
在 C 语言中,
unsigned
是unsigned int
的缩写。long
也是如此,它是long int
的快捷方式,并且也可以声明一个
unsigned long
(它将是一个 <代码>无符号长整型)。这是 ANSI 标准中的
in C,
unsigned
is a shortcut forunsigned int
.You have the same for
long
that is a shortcut forlong int
And it is also possible to declare a
unsigned long
(it will be aunsigned long int
).This is in the ANSI standard
在 C 和 C++ 中,
包含 n 位的无符号整数可以具有 0 到 0 之间的值
(2^n-1) ,这是 2^n 个不同的值。
无符号整数要么是正数,要么是零。
有符号整数使用 2 的补码存储在计算机中。
In C and C++
An unsigned integer containing n bits can have a value between 0 and
(2^n-1) , which is 2^n different values.
An unsigned integer is either positive or zero.
Signed integers are stored in a computer using 2's complement.
根据 C17 6.7.2 §2:
因此,对于
unsigned int
,我们可以写unsigned
或unsigned int
,或者如果我们感觉疯狂,int未签名
。 后者因为标准愚蠢到允许“......可能以任何顺序发生,可能混合”。 这是该语言的一个已知缺陷。正确的 C 代码使用
unsigned int
。According to C17 6.7.2 §2:
So in case of
unsigned int
we can either writeunsigned
orunsigned int
, or if we are feeling crazy,int unsigned
. The latter since the standard is stupid enough to allow "...may occur in any order, possibly intermixed". This is a known flaw of the language.Proper C code uses
unsigned int
.带来我的另一个问题的答案。
来自 C 规范,第 6.7 节。 2:
表示当未指定类型时,
unsigned
应默认为unsigned int
。 因此编写unsigned a
与unsigned int a
相同。Bringing my answer from another question.
From the C specification, section 6.7.2:
Meaning that
unsigned
, when not specified the type, shall default tounsigned int
. So writingunsigned a
is the same asunsigned int a
.