简体   繁体   English

什么是无符号数据类型?

[英]what is the unsigned datatype?

I've seen this unsigned "typeless" type used a couple of times, but never seen an explanation for it.我见过这种unsigned的“无类型”类型使用过几次,但从未见过对此的解释。 I suppose there's a corresponding signed type.我想有一个相应的signed类型。 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) - 在我的系统上, sizeof(unsigned) = 4 (暗示 32 位无符号整数)
- 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?这是 ANSI C,还是只是一个编译器扩展?

unsigned really is a shorthand for unsigned int , and so defined in standard C. unsigned实际上是unsigned int的简写,因此在标准 C 中定义。

unsigned means unsigned int . unsigned表示unsigned int signed means signed int . signed表示signed int Using just unsigned is a lazy way of declaring an unsigned int in C. Yes this is ANSI.仅使用unsigned是在 C 中声明unsigned int的一种惰性方式。是的,这是 ANSI。

Historically in C, if you omitted a datatype "int" was assumed.从历史上看,在 C 中,如果您省略了数据类型,则假定为“int”。 So "unsigned" is a shorthand for "unsigned int".所以“unsigned”是“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.很长一段时间以来,这一直被认为是不好的做法,但仍然有相当多的代码在使用它。

in C, unsigned is a shortcut for unsigned int .在 C 中, unsignedunsigned int的快捷方式。

You have the same for long that is a shortcut for long int你有同样的longlong int的快捷方式

And it is also possible to declare a unsigned long (it will be a unsigned long int ).也可以声明一个unsigned long (它将是一个unsigned long int )。

This is in the ANSI standard这是在 ANSI 标准中

In C and C++在 C 和 C++ 中

unsigned = unsigned int (Integer type)
signed   = signed int (Integer type)

An unsigned integer containing n bits can have a value between 0 and (2^n-1), which is 2^n different values.包含 n 位的无符号整数可以具有 0 和 (2^n-1) 之间的值,即 2^n 个不同的值。

An unsigned integer is either positive or zero.无符号整数为正数或零。

Signed integers are stored in a computer using 2's complement.有符号整数使用 2 的补码存储在计算机中。

According to C17 6.7.2 §2:根据 C17 6.7.2 §2:

Each list of type specifiers shall be one of the following multisets (delimited by commas, when there is more than one multiset per item);每个类型说明符列表应为以下多重集之一(当每个项目有多个多重集时,以逗号分隔); the type specifiers may occur in any order, possibly intermixed with the other declaration specifiers类型说明符可以以任何顺序出现,可能与其他声明说明符混合

— void - 空白
— char — 字符
— signed char — 签名字符
— unsigned char - 无符号字符
— short, signed short, short int, or signed short int — short、signed short、short int 或 signed short int
— unsigned short, or unsigned short int — unsigned short 或 unsigned short int
— int, signed, or signed int — int、signed 或 signed int
— unsigned, or unsigned int — 无符号或无符号整数
— long, signed long, long int, or signed long int — long、signed long、long int 或 signed long int
— unsigned long, or unsigned long int — unsigned long 或 unsigned long int
— long long, signed long long, long long int, or signed long long int — long long、signed long long、long long int 或 signed long long int
— unsigned long long, or unsigned long long int — unsigned long long 或 unsigned long long int
— float - 漂浮
— double - 双倍的
— long double — 长双
— _Bool — _布尔
— float _Complex — 浮动 _Complex
— double _Complex — 双 _Complex
— long double _Complex — long double _Complex
— atomic type specifier — 原子类型说明符
— struct or union specifier — 结构或联合说明符
— enum specifier — 枚举说明符
— typedef name — 类型定义名称

So in case of unsigned int we can either write unsigned or unsigned int , or if we are feeling crazy, int unsigned .所以在unsigned int的情况下,我们可以写unsignedunsigned int ,或者如果我们觉得疯狂, 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 代码使用unsigned int

Bringing my answer from another question .从另一个问题中得出我的答案

From the C specification , section 6.7.2:来自C 规范,第 6.7.2 节:

— unsigned, or unsigned int — 无符号或无符号整数

Meaning that unsigned , when not specified the type, shall default to unsigned int .这意味着unsigned在未指定类型时应默认为unsigned int So writing unsigned a is the same as unsigned int a .所以写unsigned aunsigned int a是一样的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM