C语言 在 C/C++ 中如何确保 int 是 4 个字节或 2 个字节

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

How to make sure a int is 4 bytes or 2 bytes in C/C++

c

提问by kkpattern

I want to know how to announce int to make sure it's 4 bytes or short in 2 bytes no matter on what platform. Does C99 have rules about this?

我想知道如何宣布 int 以确保无论在什么平台上它都是 4 个字节或 2 个字节。C99有这方面的规定吗?

回答by Ben

C99 doesn't say much about this, but you can check whether sizeof(int) == 4, or you can use fixed size types like uint32_t (32 bits unsigned integer). They are defined in stdint.h

C99 对此没有太多说明,但您可以检查是否sizeof(int) == 4,或者您可以使用固定大小的类型,例如 uint32_t(32 位无符号整数)。它们定义在stdint.h

回答by Arkku

If you are using C99 and require integer types of a given size, include stdint.h. It defines types such as uint32_tfor an unsigned integer of exactly 32-bits, and uint_fast32_tfor an unsigned integer of at least 32 bits and “fast” on the target machine by some definition of fast.

如果您使用 C99 并需要给定大小的整数类型,请包含stdint.h. 它定义了一些类型,例如uint32_t恰好 32 位uint_fast32_t的无符号整数,以及至少 32 位的无符号整数,并且通过某种快速定义在目标机器上“快速”。

Edit: Remember that you can also use bitfields to get a specific number of bits (though it may not give the best performance, especially with “strange” sizes, and most aspects are implementation-defined):

编辑:请记住,您还可以使用位域来获取特定数量的位(尽管它可能无法提供最佳性能,尤其是对于“奇怪”的大小,并且大多数方面都是实现定义的):

typedef struct {
    unsigned four_bytes:32;
    unsigned two_bytes:16;
    unsigned three_bits:3;
    unsigned five_bits:5;
} my_message_t;

Edit 2: Also remember that sizeofreturns the number of chars. It's theoretically possible (though very unlikely these days) that charis not 8 bits; the number of bits in a charis defined as CHAR_BITin limits.h.

编辑 2:还请记住,sizeof返回chars的数量。理论上可能(尽管现在不太可能)char不是 8 位;在一个比特的数量char被定义为CHAR_BITlimits.h

回答by jamesdlin

I assume you want something beyond just the obvious sizeof (int) == 4check. Likely you want some compile-time check.

我假设您想要的不仅仅是明显的sizeof (int) == 4检查。可能你想要一些编译时检查。

In C++, you could use BOOST_STATIC_ASSERT.

在 C++ 中,您可以使用BOOST_STATIC_ASSERT.

In C, you can make compile-time assertions by writing code that tries to create negatively-sized arrays on failure or that tries to create switchstatements with redefined cases. See this stackoverflow question for examples: Ways to ASSERT expressions at build time in C

在 C 中,您可以通过编写尝试在失败时创建负大小数组或尝试创建switch具有重新定义案例的语句的代码来进行编译时断言。有关示例,请参阅此 stackoverflow 问题:Ways to ASSERT expression at build time in C

回答by Edwin Buck

You can use sizeof(int), but you can never assume how large an int is. The C specification doesn't put any assumptions on the size of an int, except that it must be greater or equal to the size of a short (which must be greater or equal to the size of a char).

您可以使用 sizeof(int),但您永远无法假设 int 有多大。C 规范对 int 的大小没有任何假设,只是它必须大于或等于 short 的大小(必须大于或等于 char 的大小)。

Often the size of an int aligns to the underlying hardware. This means an int is typically the same as a word, where a word is the functional size of data fetched off the memory bus (or sometimes the CPU register width). It doesn't have to be the same as a word, but the earliest notes I have indicated it should be the preferred size for memory transfer (which is typically a word).

通常 int 的大小与底层硬件对齐。这意味着 int 通常与字相同,其中字是从内存总线(或有时是 CPU 寄存器宽度)获取的数据的功能大小。它不必与单词相同,但我指出的最早注释应该是内存传输的首选大小(通常是一个单词)。

In the past, there have been 18 bit ints (PDP-8) and 24 bit ints (PDP-15). There have been architectures with 36 bit word sizes (PDP-11) but I can't recall what their int size turned out to be.

过去,有 18 位整数 (PDP-8) 和 24 位整数 (PDP-15)。已经有 36 位字长 (PDP-11) 的架构,但我不记得它们的 int 大小是多少。

On Linux platforms, you can peek in

在 Linux 平台上,您可以查看

#include <sys/types.h>

to get the actual bit count for each type.

获取每种类型的实际位数。

回答by kkpattern

I found last night that visual studio 2008 doesn't support C99 well, and it doesn't support stdint.h. BUT they have their own types. here is a example:

我昨晚发现visual studio 2008 不能很好地支持C99,它也不支持stdint.h。但是他们有自己的类型。这是一个例子:

#ifdef _MSC_VER 
typedef __int8  int8_t;
typedef unsigned __int8 uint8_t;
typedef __int16 int16_t;
typedef unsigned __int16 uint16_t;
typedef __int32 int32_t; 
typedef unsigned __int32 uint32_t; 
typedef __int64 int64_t; 
typedef unsigned __int64 uint64_t; 
#else 
#include <stdint.h> 
#endif 

回答by Fred

Try the INT_MAXconstant in limits.h

尝试INT_MAXlimits.h中的常量

回答by Jared

Do you want to require it to be 4 bytes?

你想要求它是 4 个字节吗?

If you just want to see the size of int as it is compiled on each platform then you can just do sizeof(int).

如果您只想查看在每个平台上编译的 int 的大小,那么您只需执行sizeof(int).

回答by Karmic Coder

sizeof (int)will return the number of bytes an intoccupies in memory on the current system.

sizeof (int)将返回int当前系统内存占用的字节数。