C语言 int32、int、int32_t、int8和int8_t的区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14515874/
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 05:08:56  来源:igfitidea点击:

Difference between int32, int, int32_t, int8 and int8_t

cintdeclaration

提问by linuxfreak

I came across the data type int32_tin a C program recently. I know that it stores 32 bits, but don't intand int32do the same?

int32_t最近在 C 程序中遇到了数据类型。我知道,它存储32位,但不要intint32这样做?

Also, I want to use charin a program. Can I use int8_tinstead? What is the difference?

另外,我想char在程序中使用。我可以用int8_t吗?有什么不同?

To summarize: what is the difference between int32, int, int32_t, int8 and int8_t in C?

总结一下:C 中的 int32、int、int32_t、int8 和 int8_t 有什么区别?

回答by Jerry Coffin

Between int32and int32_t, (and likewise between int8and int8_t) the difference is pretty simple: the C standard defines int8_tand int32_t, but does not define anything named int8or int32-- the latter (if they exist at all) is probably from some other header or library (most likely predates the addition of int8_tand int32_tin C99).

int32andint32_t之间(以及在int8and之间int8_t)的区别非常简单:C 标准定义了int8_tand int32_t,但没有定义任何命名的int8int32- 后者(如果它们存在的话)可能来自其他一些头文件或库(最有可能在 C99 中添加int8_t和之前int32_t)。

Plain intis quite a bit different from the others. Where int8_tand int32_teach have a specified size, intcan be any size >= 16 bits. At different times, both 16 bits and 32 bits have been reasonably common (and for a 64-bit implementation, it should probably be 64 bits).

平原int与其他人有很大不同。其中int8_tint32_t每个都有指定的大小,int可以是任意大小 >= 16 位。在不同的时期,16 位和 32 位都相当常见(对于 64 位实现,它可能应该是 64 位)。

On the other hand, intis guaranteed to be present in every implementation of C, where int8_tand int32_tare not. It's probably open to question whether this matters to you though. If you use C on small embedded systems and/or older compilers, it may be a problem. If you use it primarily with a modern compiler on desktop/server machines, it probably won't be.

另一方面,int保证出现在 C 的每个实现中,其中int8_tint32_t不是。不过,这对您是否重要,这可能是值得商榷的。如果您在小型嵌入式系统和/或较旧的编译器上使用 C,则可能会出现问题。如果您主要将它与台式机/服务器机器上的现代编译器一起使用,则可能不会。

Oops -- missed the part about char. You'd use int8_tinstead of char if (and only if) you want an integer type guaranteed to be exactly 8 bits in size. If you want to store characters, you probably want to use charinstead. Its size can vary (in terms of number of bits) but it's guaranteed to be exactly one byte. One slight oddity though: there's no guarantee about whether a plain charis signed or unsigned (and many compilers can make it either one, depending on a compile-time flag). If you need to ensure its being either signed or unsigned, you need to specify that explicitly.

哎呀 - 错过了关于的部分charint8_t如果(且仅当)您希望整数类型保证大小正好为 8 位时,您将使用char 代替。如果你想存储字符,你可能想用它char来代替。它的大小可能会有所不同(就位数而言),但保证正好是一个字节。但是有一点奇怪:无法保证普通char是有符号的还是无符号的(并且许多编译器可以将其设为任一,这取决于编译时标志)。如果您需要确保其已签名或未签名,则需要明确指定。

回答by Superman

The _t data types are typedef types in the stdint.h header, while int is an in built fundamental data type. This make the _t available only if stdint.h exists. int on the other hand is guaranteed to exist.

_t 数据类型是 stdint.h 标头中的 typedef 类型,而 int 是内置的基本数据类型。这使得 _t 仅在 stdint.h 存在时可用。另一方面,int 保证存在。

回答by Naumann

Always keep in mind that 'size' is variable if not explicitly specified so if you declare

永远记住,如果没有明确指定,'size' 是可变的,所以如果你声明

 int i = 10;

On some systems it may result in 16-bit integer by compiler and on some others it may result in 32-bit integer (or 64-bit integer on newer systems).

在某些系统上,编译器可能会生成 16 位整数,而在其他一些系统上,它可能会生成 32 位整数(或在较新的系统上生成 64 位整数)。

In embedded environments this may end up in weird results (especially while handling memory mapped I/O or may be consider a simple array situation), so it is highly recommended to specify fixed size variables. In legacy systems you may come across

在嵌入式环境中,这可能会导致奇怪的结果(尤其是在处理内存映射 I/O 或可能被认为是简单的数组情况时),因此强烈建议指定固定大小的变量。在遗留系统中,您可能会遇到

 typedef short INT16;
 typedef int INT32;
 typedef long INT64; 

Starting from C99, the designers added stdint.h header file that essentially leverages similar typedefs.

从 C99 开始,设计者添加了 stdint.h 头文件,该文件基本上利用了类似的 typedef。

On a windows based system, you may see entries in stdin.h header file as

在基于 Windows 的系统上,您可能会在 stdin.h 头文件中看到条目为

 typedef signed char       int8_t;
 typedef signed short      int16_t;
 typedef signed int        int32_t;
 typedef unsigned char     uint8_t;

There is quite more to that like minimum width integer or exact width integer types, I think it is not a bad thing to explore stdint.h for a better understanding.

还有更多的东西,比如最小宽度整数或精确宽度整数类型,我认为探索 stdint.h 以获得更好的理解并不是一件坏事。