C++ 不同整数类型的区别

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

Difference between different integer types

c++c

提问by user1507133

I was wondering what is the difference between uint32_tand uint32, and when I looked in the header files it had this:

我想知道uint32_tand之间有什么区别uint32,当我查看头文件时,它有这个:

types.h:

    /** @brief 32-bit unsigned integer. */
    typedef unsigned int uint32;
stdint.h:

    typedef unsigned   uint32_t;

This only leads to more questions: What is the difference between

这只会引出更多问题:两者之间有什么区别?

unsigned varName;

and

unsigned int varName;

?

?

I am using MinGW.

我在用 MinGW.

采纳答案by Kerrek SB

unsignedand unsigned intare synonymous, much like unsigned short [int]and unsigned long [int].

unsignedandunsigned int是同义词,很像unsigned short [int]and unsigned long [int]

uint32_tis a type that's (optionally) defined by the C standard. uint32is just a name you made up, although it happens to be defined as the same thing.

uint32_t是由 C 标准(可选)定义的类型。uint32只是你编造的一个名字,尽管它恰好被定义为同一个东西。

回答by RiaD

There is no difference.

没有区别。

unsigned int = uint32 = uint32_t = unsignedin your case and unsigned int = unsignedalways

unsigned int = uint32 = uint32_t = unsigned在你的情况下,unsigned int = unsigned总是

回答by Mat

There is absolutely no difference between unsignedand unsigned int.

unsigned和之间绝对没有区别unsigned int

Whether that type is a good match for uint32_tis implementation-dependant though; an intcould be "shorter" than 32 bits.

不过,该类型是否适合匹配uint32_t取决于实现;anint可能比 32 位“短”。

回答by Russell Borogove

unsignedand unsigned intare synonymous for historical reasons; they both mean "unsigned integer of the most natural size for the CPU architecture/platform", which is often (but by no means always) 32 bits on modern platforms.

unsigned并且unsigned int由于历史原因是同义词;它们都表示“CPU 架构/平台最自然大小的无符号整数”,在现代平台上通常(但绝不总是)32 位。

<stdint.h>is a standard header in C99 that is supposed to give type definitions for integers of particular sizes, with the uint32_tnaming convention.

<stdint.h>是 C99 中的标准头文件,它应该为具有uint32_t命名约定的特定大小的整数提供类型定义。

The <types.h>that you're looking at appears to be non-standard and presumably belongs to some framework your project is using. Its uint32typedef is compatible with uint32_t. Whether you should use one or the other in your code is a question for your manager.

<types.h>说你看似乎是不规范的,想必是属于一些框架您的项目使用。它的uint32typedef 与uint32_t. 您是否应该在代码中使用其中一种是您的经理的问题。