我可以假设long int的大小始终为4个字节吗? [英] Can I assume the size of long int is always 4 bytes?

查看:108
本文介绍了我可以假设long int的大小始终为4个字节吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

long int(据我所知是long的同义词)是否总是4个字节是真的吗?

Is it always true that long int (which as far as I understand is a synonym for long) is 4 bytes?

我可以依靠吗?如果不是这样,那么对于基于POSIX的操作系统是否正确?

Can I rely on that? If not, could it be true for a POSIX based OS?

推荐答案

除了char之外,标准没有说明任何整数类型的确切大小.通常,long在32位系统上为32位,在64位系统上为64位.

The standards say nothing regarding the exact size of any integer types aside from char. Typically, long is 32-bit on 32-bit systems and 64-bit on 64-bit systems.

该标准确实指定了最小大小.摘自 C标准的第5.2.4.2.1节a>:

The standard does however specify a minimum size. From section 5.2.4.2.1 of the C Standard:

1 以下给出的值应替换为常量表达式 适用于#if预处理指令.而且, 除了CHAR_BITMB_LEN_MAX,以下内容应为 替换为与类型相同的表达式 表达式是转换后的相应类型的对象 根据整数促销. 其实现定义 值的大小应等于或大于(绝对值)至 显示的那些符号相同.

1 The values given below shall be replaced by constant expressions suitable for use in #if preprocessing directives. Moreover, except for CHAR_BIT and MB_LEN_MAX, the following shall be replaced by expressions that have the same type as would an expression that is an object of the corresponding type converted according to the integer promotions. Their implementation-defined values shall be equal or greater in magnitude (absolute value) to those shown, with the same sign.

...

  • 类型long int

LONG_MIN -2147483647//-(2 ^ 31−1)

LONG_MIN -2147483647 // −(2^31−1)

long int类型的对象的最大值

LONG_MAX +2147483647//2 ^ 31−1

LONG_MAX +2147483647 // 2^31−1

这表示long int 必须至少应为32位,但可以更大.在CHAR_BIT为8的机器上,这给出的最小字节大小为4. CHAR_BIT等于16,long int可能是2个字节长.

This says that a long int must be a minimum of 32 bits, but may be larger. On a machine where CHAR_BIT is 8, this gives a minimum byte size of 4. However on machine with e.g. CHAR_BIT equal to 16, a long int could be 2 bytes long.

这是一个真实的例子.对于以下代码:

Here's a real-world example. For the following code:

#include <stdio.h>

int main ()
{
    printf("sizeof(long) = %zu\n", sizeof(long));
    return 0;
}

在Debian 7 i686上的输出:

Output on Debian 7 i686:

sizeof(long)= 4

sizeof(long) = 4

在CentOS 7 x64上的输出:

Output on CentOS 7 x64:

sizeof(long)= 8

sizeof(long) = 8

因此,您不能对大小做任何假设.如果需要特定大小的类型,可以使用stdint.h中定义的类型.它定义了以下类型:

So no, you can't make any assumptions on size. If you need a type of a specific size, you can use the types defined in stdint.h. It defines the following types:

  • int8_t:签名的8位
  • uint8_t:无符号8位
  • int16_t:带符号的16位
  • uint16_t:无符号16位
  • int32_t:带符号的32位
  • uint32_t:无符号32位
  • int64_t:已签名的64位
  • uint64_t:无符号64位
  • int8_t: signed 8-bit
  • uint8_t: unsigned 8-bit
  • int16_t: signed 16-bit
  • uint16_t: unsigned 16-bit
  • int32_t: signed 32-bit
  • uint32_t: unsigned 32-bit
  • int64_t: signed 64-bit
  • uint64_t: unsigned 64-bit

stdint.h标头在标准的7.20节中进行了说明,确切的宽度类型在7.20.1.1节中进行了说明.该标准指出,这些typedef是可选的,但它们存在于大多数实现中.

The stdint.h header is described in section 7.20 of the standard, with exact width types in section 7.20.1.1. The standard states that these typedefs are optional, but they exist on most implementations.

这篇关于我可以假设long int的大小始终为4个字节吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆