cout<< -2147483648; g ++中的奇怪输出 [英] cout << -2147483648 ; strange output in g++
问题描述
对于这段代码,我得到以下输出,我无法理解。$ / $
(2 ^ 31 = 2147483648)
cout<< -2147483648<< endl;
cout<< numeric_limits< int> :: min()<<'',''<<
numeric_limits< int> :: max()<<我得到的输出是:
2147483648
-2147483648,2147483647
有这个编译时警告:这个十进制常数只是无符号
$ C $ b ISO C90
我很惊讶为什么负数打印为正数。
你能解释一下这个行为的原因吗?
谢谢
suresh
Hi,
For this code snippet, I get the following output, which I am unable
to understand.
(2^31 = 2147483648)
cout<< -2147483648 << endl;
cout << numeric_limits<int>::min() <<'','' <<
numeric_limits<int>::max()<< endl;
The outputs I get are:
2147483648
-2147483648,2147483647
with this compile time warning: this decimal constant is unsigned only
in ISO C90
I am surprised why the negative number is printed as positive.
Could you please explain why this behaviour?
thanks
suresh
推荐答案
suresh写道:
suresh wrote:
对于这段代码,我得到以下输出,我无法
了解。
(2 ^ 31 = 2147483648)
cout<< -2147483648<< endl;
cout<< numeric_limits< int> :: min()<<'',''<<
numeric_limits< int> :: max()<<我得到的输出是:
2147483648
-2147483648,2147483647
有这个编译时警告:这个十进制常数只是无符号
$ C $ b ISO C90
我很惊讶为什么负数打印为正数。
你能解释一下这个行为的原因吗?
For this code snippet, I get the following output, which I am unable
to understand.
(2^31 = 2147483648)
cout<< -2147483648 << endl;
cout << numeric_limits<int>::min() <<'','' <<
numeric_limits<int>::max()<< endl;
The outputs I get are:
2147483648
-2147483648,2147483647
with this compile time warning: this decimal constant is unsigned only
in ISO C90
I am surprised why the negative number is printed as positive.
Could you please explain why this behaviour?
文字''2147483648''不适合int,因此它是'/ b $ b'提升为''long int''。如果您的系统'''long''具有相同的大小
为''int''(例如在Windows上),则行为未定义。
>
尝试将后缀''U'添加到文字中。
V
-
请在通过电子邮件回复时删除资本''A'
我没有回复最热门的回复,请不要问
The literal ''2147483648'' does not fit in an int, therefore it is
promoted to ''long int''. If your system''s ''long'' has the same size
as ''int'' (like on Windows, for example), the behaviour is undefined.
Try adding the suffix ''U'' to the literal.
V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask
suresh写道:
suresh wrote:
对于这段代码,我得到以下输出,我是无法理解。
了解。
(2 ^ 31 = 2147483648)
cout<< -2147483648<< endl;
cout<< numeric_limits< int> :: min()<<'',''<<
numeric_limits< int> :: max()<<我得到的输出是:
2147483648
-2147483648,2147483647
有这个编译时警告:这个十进制常数只是无符号
$ C $ b ISO C90
我很惊讶为什么负数打印为正数。
你能解释一下这个行为的原因吗?
Hi,
For this code snippet, I get the following output, which I am unable
to understand.
(2^31 = 2147483648)
cout<< -2147483648 << endl;
cout << numeric_limits<int>::min() <<'','' <<
numeric_limits<int>::max()<< endl;
The outputs I get are:
2147483648
-2147483648,2147483647
with this compile time warning: this decimal constant is unsigned only
in ISO C90
I am surprised why the negative number is printed as positive.
Could you please explain why this behaviour?
使用2'补码的32位有符号数的范围是-2147483647到
2147483648. -2147483648不适合一个带符号的32位int。在32位
位int。
基本上,你试图表示的数字不适合int
变量。我相信这给你一个不确定的价值。
-
Jim Langston
ta ******* @ rocketmail.com
HI Victor,
>
你能更精细一点吗?我的
linux机器中int的最大值是2147483648.那么为什么你说文字
'2147483648''不适合int?
long与我的机器上的int大小相同。
suresh
HI Victor,
Could you be little bit more elaborative? The max value of int in my
linux machine is 2147483648. Then why did you say that The literal
''2147483648'' does not fit in an int?
long has the same size as int on my machine too.
suresh
文字''2147483648''不适合int,因此它被提升为''long int''。
。如果您的系统'''long''具有相同的大小
为''int''(例如在Windows上),则行为未定义。
>
尝试将后缀''U'添加到文字中。
V
-
请在通过电子邮件回复时删除资本''A'
我没有回复最热门的回复,请不要问
The literal ''2147483648'' does not fit in an int, therefore it is
promoted to ''long int''. If your system''s ''long'' has the same size
as ''int'' (like on Windows, for example), the behaviour is undefined.
Try adding the suffix ''U'' to the literal.
V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask
这篇关于cout<< -2147483648; g ++中的奇怪输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!