在C ++中声明长文字时,是否需要长后缀和无符号后缀? [英] Are long-suffix and unsigned-suffix needed when declaring long literals in C++?
问题描述
我对编写C代码有一些古老的记忆,例如:
I have some ancient memories of writing C code like:
long value = 0;
在糟糕的旧Win16天内
并最终将value
初始化为一半:即,低16位是0
,高16位是内存中该位置的任意随机位.因此,我变得有条件写:
in the bad old Win16 days and ending up with value
being only half-initialized: i.e. the lower 16 bits were 0
and the upper 16 bits were whatever random bits were at that location in memory. As such, I became conditioned to write:
long value = 0L;
在当今的C99和/或C ++时代仍然需要吗?我知道ILP32指定int
和long
都是32位,但是假设我们使用的是LP64,其中int
是32位,而long
是64位.后缀是必需的,还是现代形式的C和C ++会将文字直接隐式地扩展到它们被分配给的变量的长度?
Is this still required in this day-and-age under C99 and/or C++? I know that ILP32 specifies that int
and long
are both 32-bit, but suppose we're using, say, LP64 where int
s are 32-bits and long
s are 64-bits. Are the suffixes required or will the modern forms of C and C++ implicitly extend literals to the length of the variable they're being assigned to?
无符号值如何? IE.这是必需的吗?
How about unsigned values? I.e. is this required?
unsigned long value = 0UL;
推荐答案
在您提供的示例中,它们不是必需的.但是,在某些默默的情况下可能需要使用它们.例如,以下内容可能会产生不同的值:
They are not required in the examples you gave. However, they may be needed in some somewhat recondite circumstances. For example, the following may produce different values:
sizeof(1)
sizeof(1L)
这篇关于在C ++中声明长文字时,是否需要长后缀和无符号后缀?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!