在C ++中是默认`int long int`的`int`? [英] Is `int` by default `signed long int` in C++?
问题描述
在C ++中 int
默认为 signed long int
?
它是平台和/或编译器依赖吗?如果是,如何?
以下是否确保重复? p>
signed short int
signed int
signed long int
signed long long int
unsigned short int
unsigned int
unsigned long int
unsigned long long int
的所有整数都是不同的,也就是说,你可以安全地重载所有的函数,并且不会产生任何冲突。然而,有时使用相同数量的位用于它们的表示。即使它们使用相同数量的位,有符号和无符号类型始终具有不同的范围。除了
char
,使用任何不带签名的整数类型相当于使用 signed
,即 signed int
和 int
是等价的。 是与 signed char
和不同的类型但 char
具有与 signed char
或 unsigned char $相同的表示和范围c $ c>。您可以使用 std :: numeric_limits< char> :: is_signed
来确定它使用的是哪个。
更有趣的方面。以下条件都是真的:
-
7< = std :: numeric_limits< signed char&
-
sizeof(char)== 1
- code> sizeof(char)== sizeof(signed char)
)
15< = std :: numeric_limits< short> :: digits
sizeof(char)< = sizeof(short)
short)< = sizeof(int)
31< = std :: numeric_limits< long& c $ c>
sizeof(int)< = sizeof(long)
63< = std :: numeric_limits< long long> :: digits
sizeof ; = sizeof(long long)
sizeof(X)== sizeof(signed X)
sizeof(signed X)== sizeof(unsigned X)
(其中X是 char
, short
, int
, long
和 long long
)。
$ b
这意味着所有整数类型的大小可以相同,只要此类型至少包含64位(显然 Cray X-MP 是这样的野兽)。在现代机器上通常 sizeof(int)== sizeof(long)
但有一些机器 sizeof(int) code>。 long
是32位还是64位取决于实际架构,两种类型目前都在。
Is int
by default signed long int
in C++?
Is it platform and/or compiler dependent? If so, how?
[EDIT]
Are any of the following guaranteed to be duplicate?
signed short int
signed int
signed long int
signed long long int
unsigned short int
unsigned int
unsigned long int
unsigned long long int
解决方案 All of the integer types are different, i.e. you can safely overload functions for all of them and you won't get any conflict. However, some times use the same number of bits for their representation. Even if they use the same number of bits signed and unsigned types always have a different range. Except for char
, using any integer type without signed
is equivalent to using it with signed
, i.e. signed int
and int
are equivalent. char
is a different type as signed char
and unsigned char
but char
has the same representation and range of either signed char
or unsigned char
. You can use std::numeric_limits<char>::is_signed
to find out which it uses.
On to the more interesting aspects. The following conditions are all true:
7 <= std::numeric_limits<signed char>::digits
sizeof(char) == 1
sizeof(char) == sizeof(signed char)
sizeof(char) == sizeof(unsigned char)
15 <= std::numeric_limits<short>::digits
sizeof(char) <= sizeof(short)
sizeof(short) <= sizeof(int)
31 <= std::numeric_limits<long>::digits
sizeof(int) <= sizeof(long)
63 <= std::numeric_limits<long long>::digits
sizeof(long) <= sizeof(long long)
sizeof(X) == sizeof(signed X)
sizeof(signed X) == sizeof(unsigned X)
(where "X" is one of char
, short
, int
, long
, and long long
).
This means that the size of all integer types can be the same as long as this types hold at least 64 bits (and apparently the Cray X-MP was such a beast). On contemporary machines typically sizeof(int) == sizeof(long)
but there are machines where sizeof(int) == sizeof(short)
. Whether long
is 32 or 64 bits depends on the actual architecture and both kinds are currently around.
这篇关于在C ++中是默认`int long int`的`int`?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!