gpt4 book ai didi

int 和 long 可以是同一类型吗?

转载 作者:行者123 更新时间:2023-12-03 03:14:44 31 4
gpt4 key购买 nike

当然intlong不能保证是同一类型,但是在Windows等平台上以32位编译时位模式,它们恰好具有相同的大小,编译器是否允许将它们视为相同的类型?

我不确定如何明确测试编译器是否将两种类型视为相同,但我尝试使用 int *a; 进行启发式测试。 long *b = a; 并且 Microsoft 编译器毫无怨言地接受这一点,而 GCC 则给出警告。编译器是否行为不当,或者它们是否命名两种不同的类型或仅命名一种,是否依赖于实现?

最佳答案

标准(C2011标准的N1570草案)在6.2.5(4)中说:

There are five standard signed integer types, designated as signed char, short int, int, long int, and long long int. (These and other types may be designated in several additional ways, as described in 6.7.2.) There may also be implementation-defined extended signed integer types. The standard and extended signed integer types are collectively called signed integer types.

同一节第 14 段:

The type char, the signed and unsigned integer types, and the floating types are collectively called the basic types. The basic types are complete object types. Even if the implementation defines two or more basic types to have the same representation, they are nevertheless different types.

也就是说,intlong(指定long int 的另一种方式)是不同的类型。它们可能具有相同的表示、行为和对齐要求,但就语言而言,它们不是同一类型。

关于int 和 long 可以是同一类型吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14874558/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com