C语言 uint 和 unsigned int 之间的区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5678049/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 08:24:35  来源:igfitidea点击:

Difference between uint and unsigned int?

cgccuint

提问by the_candyman

Is there any difference between uintand unsigned int? I'm looking in the site, but all question refers to C# or C++. I'd like to have an answer concerning the C language.

有什么区别uintunsigned int?我正在查看网站,但所有问题都涉及 C# 或 C++。我想得到一个关于 C 语言的答案。

If it is relevant, note that I'm using GCC under Linux.

如果相关,请注意我在 Linux 下使用 GCC。

回答by Erik

uintisn't a standard type - unsigned intis.

uint不是标准类型 -unsigned int是。

回答by taskinoor

Some systems may define uint as a typedef.

某些系统可能将 uint 定义为 typedef。

typedef unsigned int uint;

For these systems they are same. But uint is not a standard type, so every system may not support it and thus it is not portable.

对于这些系统,它们是相同的。但是 uint 不是标准类型,因此每个系统可能都不支持它,因此它不可移植。

回答by Yauhen Yakimovich

I am extending a bit answers by Erik, Teoman Soygul and taskinoor

我正在扩展 Erik、Teoman Soygul 和 taskinoor 的一些答案

uintis not a standard.

uint不是标准。

Hence using your own shorthand like this is discouraged:

因此,不鼓励像这样使用自己的速记:

typedef unsigned int uint;

typedef unsigned int uint;

If you look for platform specificity instead (e.g. you need to specify the number of bits your int occupy), including stdint.h:

如果您寻找平台特异性(例如,您需要指定 int 占用的位数),包括stdint.h

#include <stdint.h>

will expose the following standard categories of integers:

将公开以下标准类别的整数:

  • Integer types having certain exact widths

  • Integer types having at least certain specified widths

  • Fastest integer types having at least certain specified widths

  • Integer types wide enough to hold pointers to objects

  • Integer types having greatest width

  • 具有特定精确宽度的整数类型

  • 至少具有某些指定宽度的整数类型

  • 至少具有某些指定宽度的最快整数类型

  • 整数类型足够宽以容纳指向对象的指针

  • 具有最大宽度的整数类型

For instance,

例如,

Exact-width integer types

The typedef name int N _t designates a signed integer type with width N, no padding bits, and a two's-complement representation. Thus, int8_t denotes a signed integer type with a width of exactly 8 bits.

The typedef name uint N _t designates an unsigned integer type with width N. Thus, uint24_t denotes an unsigned integer type with a width of exactly 24 bits.

精确宽度整数类型

typedef 名称 int N _t 指定宽度为 N、无填充位和二进制补码表示的有符号整数类型。因此, int8_t 表示宽度正好为 8 位的有符号整数类型。

typedef 名称 uint N _t 指定宽度为 N 的无符号整数类型。因此, uint24_t 表示宽度正好为 24 位的无符号整数类型。

defines

定义

int8_t
int16_t
int32_t
uint8_t
uint16_t
uint32_t

回答by Trevor Hickey

All of the answers here fail to mention the real reason for uint.
It's obviously a typedefof unsigned int, but that doesn't explain its usefulness.

这里的所有答案都没有提到uint.
它显然是 a typedefof unsigned int,但这并不能解释它的用处。

The real question is,

真正的问题是,

Why would someone want to typedef a fundamental type to an abbreviated version?

为什么有人要将基本类型定义为缩写版本?

To save on typing?
No, they did it out of necessity.

为了节省打字?
不,他们这样做是出于必要。

Consider the C language; a language that does not have templates.
How would you go about stamping out your own vector that can hold any type?

考虑 C 语言;一种没有模板的语言。
您将如何消除自己的可以容纳任何类型的向量?

You could do something with void pointers,
but a closer emulation of templates would have you resorting to macros.

你可以用 void 指针做一些事情,
但是更接近的模板模拟会让你诉诸宏。

So you would define your template vector:

所以你会定义你的模板向量:

#define define_vector(type) \
  typedef struct vector_##type { \
    impl \
  };

Declare your types:

声明你的类型:

define_vector(int)
define_vector(float)
define_vector(unsigned int)

And upon generation, realize that the types ought to be a single token:

并且在生成时,意识到类型应该是单个标记:

typedef struct vector_int { impl };
typedef struct vector_float { impl };
typedef struct vector_unsigned int { impl };

回答by Teoman Soygul

The unsigned intis a built in (standard) type so if you want your project to be cross-platform, always use unsigned intas it is guarantied to be supported by all compilers (hence being the standard).

unsigned int是一种内置(标准)类型,因此如果您希望您的项目是跨平台的,请始终使用unsigned int它,因为它保证所有编译器都支持(因此是标准)。