11

制作 long 64 位并保留 long long 直到 128 位数字成为现实不是更有意义吗?

4

6 回答 6

12

Yes, it does make sense, but Microsoft had their own reasons for defining "long" as 32-bits.

As far as I know, of all the mainstream systems right now, Windows is the only OS where "long" is 32-bits. On Unix and Linux, it's 64-bit.

All compilers for Windows will compile "long" to 32-bits on Windows to maintain compatibility with Microsoft.

For this reason, I avoid using "int" and "long". Occasionally I'll use "int" for error codes and booleans (in C), but I never use them for any code that is dependent on the size of the type.

于 2011-09-02T05:25:28.060 回答
5

The c standard have NOT specified the bit-length of primitive data type, but only the least bit-length of them. So compilers can have options on the bit-length of primitive data types. On deciding the bit-length of each primitive data type, the compiler designer should consider the several factors, including the computer architecture.

here is some references: http://en.wikipedia.org/wiki/C_syntax#Primitive_data_types

于 2011-09-02T05:35:54.887 回答
2

由于历史原因。很长一段时间(双关语),“int”表示 16 位;因此“长”为 32 位。当然,时代变了。因此“长长”:)

PS:

GCC(和其他)目前支持 128 位整数作为“(u)int128_t”。

PP:

以下是关于为什么 GCC 的人们做出他们所做决定的讨论:

http://www.x86-64.org/pipermail/discuss/2005-August/006412.html

于 2011-09-02T05:24:13.180 回答
0

自从第一个用于通用可重编程微型计算机的 C 编译器出现以来,代码通常需要使用恰好保持 8、16 或 32 位的类型,但直到 1999 年标准才明确提供程序指定的任何方式。另一方面,几乎所有 8 位、16 位和 32 位微型计算机的编译器都将“char”定义为 8 位,将“short”定义为 16 位,将“long”定义为 32 位。它们之间的唯一区别是“int”是 16 位还是 32 位。

虽然 32 位或更大的 CPU 可以将“int”用作 32 位类型,而将“long”用作 64 位类型,但有大量代码预计“long”将是 32 位。虽然 C 标准在 1999 年添加了“固定大小”类型,但标准中还有其他地方仍然使用“int”和“long”,例如“printf”。虽然 C99 添加了宏来为固定大小的整数类型提供正确的格式说明符,但有大量代码预计“%ld”是 int32_t 的有效格式说明符,因为它几乎适用于任何 8 位、16 位或 32 位平台。

出于对可追溯到几十年前的现有代码库的尊重,将“long”设为 32 位或 64 位是否更有意义,以避免需要更冗长的“long long”或“int64_t”来识别64 位类型可能是一个判断调用,但考虑到新代码可能应该在实际情况下支持使用指定大小的类型,我不确定我是否看到制作“长”64 位的显着优势,除非“int”也是 64 位(这将对现有代码产生更大的问题)。

于 2016-05-09T20:52:41.270 回答
0

d 32位微机定义“char”为8位,“short”为16位,“long”为32位。它们之间的唯一区别是“int”是 16 位还是 32 位。

虽然 32 位或更大的 CPU 可以将“int”用作 32 位类型,而将“long”用作 64 位类型,但有大量代码预计“long”将是 32 位。虽然 C 标准在 1999 年添加了“固定大小”类型,但标准中还有其他地方仍然使用“int”和“long”,例如“printf”。虽然 C99 添加了宏来提供

于 2019-02-20T12:36:57.867 回答
-2

C99 N1256标准草案

大小longlong long是实现定义的,我们所知道的是:

  • 最小尺寸保证
  • 类型之间的相对大小

5.2.4.2.1 整数类型<limits.h>的大小给出了最小大小:

1 [...] 它们的实现定义值的大小(绝对值)应等于或大于所示的 [...]

  • UCHAR_MAX 255 // 2 8 - 1
  • USHRT_MAX 65535 // 2 16 - 1
  • UINT_MAX 65535 // 2 16 - 1
  • ULONG_MAX 4294967295 // 2 32 - 1
  • ULLONG_MAX 18446744073709551615 // 2 64 - 1

6.2.5 类型然后说:

8 对于任何两个具有相同符号和不同整数转换等级的整数类型(见 6.3.1.1),具有较小整数转换等级的类型的值范围是另一个类型的值的子范围。

6.3.1.1 布尔值、字符和整数确定相对转换等级:

1 每个整数类型都有一个整数转换等级,定义如下:

  • long long int 的rank 大于long int 的rank,long int 的rank 大于int 的rank,short int 的rank 大于signed char 的rank。
  • 任何无符号整数类型的等级应等于相应的有符号整数类型的等级,如果有的话。
  • 对于所有整数类型 T1、T2 和 T3,如果 T1 的秩大于 T2 且 T2 的秩大于 T3,则 T1 的秩大于 T3
于 2016-05-09T19:39:29.577 回答