什么是无符号数据类型?

发布于 2024-07-29 02:49:40 字数 589 浏览 2 评论 0原文

我见过这种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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

蘑菇王子 2024-08-05 02:49:40

unsigned 实际上是 unsigned int 的简写,在标准 C 中如此定义。

unsigned really is a shorthand for unsigned int, and so defined in standard C.

傲鸠 2024-08-05 02:49:40

unsigned 表示unsigned intsigned 表示signed int。 仅使用 unsigned 是在 C 中声明 unsigned int 的一种懒惰方式。是的,这是 ANSI。

unsigned means unsigned int. signed means signed int. Using just unsigned is a lazy way of declaring an unsigned int in C. Yes this is ANSI.

(り薆情海 2024-08-05 02:49:40

历史上,在 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.

酒儿 2024-08-05 02:49:40

在 C 语言中,unsignedunsigned int 的缩写。

long 也是如此,它是 long int 的快捷方式

,并且也可以声明一个 unsigned long (它将是一个 <代码>无符号长整型)。

这是 ANSI 标准中的

in C, unsigned is a shortcut for unsigned int.

You have the same for long that is a shortcut for long int

And it is also possible to declare a unsigned long (it will be a unsigned long int).

This is in the ANSI standard

恰似旧人归 2024-08-05 02:49:40

在 C 和 C++ 中,

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

包含 n 位的无符号整数可以具有 0 到 0 之间的值
(2^n-1) ,这是 2^n 个不同的值。

无符号整数要么是正数,要么是零。

有符号整数使用 2 的补码存储在计算机中。

In C and 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.

An unsigned integer is either positive or zero.

Signed integers are stored in a computer using 2's complement.

挽心 2024-08-05 02:49:40

根据 C17 6.7.2 §2:

每个类型说明符列表应为以下多重集之一(当每项有多个多重集时,以逗号分隔); 类型说明符可以以任何顺序出现,可能与其他声明说明符混合

—无效
— 字符
— 有符号的字符
— 无符号字符
— 短整型、带符号短整型、短整型或带符号短整型
— 无符号短整型,或无符号短整型
— int、有符号或有符号 int
— 无符号或无符号整数
— long、signed long、long int 或signed long int
— unsigned long 或 unsigned long int
— long long、signed long long、long long int 或signed long long int
— unsigned long long,或 unsigned long long int
— 浮动
— 双
— 长双
— _布尔
— float _Complex
— 双_Complex
— 长双精度_Complex
— 原子类型说明符
— 结构或联合说明符
— 枚举说明符
— typedef 名称

因此,对于 unsigned int ,我们可以写 unsignedunsigned int,或者如果我们感觉疯狂,int未签名。 后者因为标准愚蠢到允许“......可能以任何顺序发生,可能混合”。 这是该语言的一个已知缺陷。

正确的 C 代码使用 unsigned int

According to 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
— unsigned short, or unsigned short int
— int, signed, or signed int
— unsigned, or unsigned int
— long, signed long, long int, or signed long int
— unsigned long, or unsigned long int
— long long, signed long long, long long int, or signed long long int
— unsigned long long, or unsigned long long int
— float
— double
— long double
— _Bool
— float _Complex
— 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. 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.

似狗非友 2024-08-05 02:49:40

带来我的另一个问题的答案

来自 C 规范,第 6.7 节。 2:

—无符号或无符号整数

表示当未指定类型时,unsigned 应默认为 unsigned int。 因此编写unsigned aunsigned int a相同。

Bringing my answer from another question.

From the C specification, section 6.7.2:

— unsigned, or unsigned int

Meaning that unsigned, when not specified the type, shall default to unsigned int. So writing unsigned a is the same as unsigned int a.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文