关于 C 整数类型和 printf() 说明符的奇怪问题
好吧,我知道这个问题可能有些奇怪,但我仍然想揭开它的神秘面纱。
1.) C 中的 int
类型可以存储 -2147483648 到 2147483647 范围内的数字。
2.) 如果我们在其前面附加一个 unsigned
,则该范围将变成 0 到 2147483647。
3.) 问题是,当下面的代码时,为什么我们还要费心使用关键字 unsigned
实际上可以起作用。
代码:
#include <stdio.h>
int main(void)
{
int num = 2147483650;
printf("%u\n" , num);
return 0;
}
4.)如您所见,如果我使用 %u
说明符,我仍然可以将整数打印为无符号类型,并且它将向我打印值 2147483650
。
5.)即使我创建另一个值为 50
的整数类型并用 num
求和,虽然它溢出了,但我仍然可以通过使用打印出正确的总和值%u
说明符。那么为什么 unsigned
关键字仍然是必要的?
感谢您花时间阅读我的问题。
Alright i know this question might some weird , but still i wanted to demystify it.
1.)an int
type in C can stores number in the range of -2147483648 to 2147483647.
2.)If we append an unsigned
it front of it , the range would become 0 to 2147483647.
3.)The thing is , why do we even bother to use the keyword unsigned
when the code below could actually works.
The Code:
#include <stdio.h>
int main(void)
{
int num = 2147483650;
printf("%u\n" , num);
return 0;
}
4.)As you see , i can still print out the integer as unsigned type if I use the %u
specifier and it will print me the value 2147483650
.
5.)Even if I create another integer type with value 50
and sum it up with num
, although it's overflow but yet I still can print out the correct sum value by using %u
specifier.So why unsigned
keyword is still a necessity??
Thanks for spending time reading my question.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
不,这仅在某些平台上成立(其中
int
是 32 位,2 的补码)。否,在这种情况下,范围将为 0 到 4294967295。
该代码表现出未定义的行为。< /p>
参见 3。
参见 2.和 3。
No, this is true only on certain platforms (where an
int
is 32-bit, 2's-complement).No, in that case the range would be 0 to 4294967295.
That code exhibits undefined behaviour.
See 3.
See 2. and 3.
仅考虑问题 3“为什么我们要费心使用 unsigned”,请考虑以下程序片段:
我们使用“unsigned”,因为
unsigned int
的行为与int
不同。除了printf
的工作原理之外,还有更多有趣的行为。Considering only Q3, "Why do we bother to use unsigned", consider this program fragment:
We use "unsigned" because
unsigned int
behaves differently fromint
. There are more interesting behaviors than just howprintf
works.好吧,首先,分配超出范围的数字的结果是实现定义的 - 它不必提供使用
%u
格式说明符打印时“起作用”的值。但要真正回答您的问题,请考虑对您的代码进行以下修改:(如果 2147483650 超出您平台上的
int
范围,但在unsigned
范围内,那么您使用unsigned
类型只能得到正确答案 1073741825)。Well, firstly the result of assigning an out-of-range number is implementation-defined - it doesn't have to give the value that will "work" when printed with the
%u
format specifier. But to really answer your question, consider this modification to your code:(If 2147483650 is out of range of
int
on your platform, but within the range ofunsigned
, then you will only get the correct answer of 1073741825 using theunsigned
type).错了。范围是 INT_MIN(在 2 个补码系统中通常是 -INT_MAX+1)到 INT_MAX; INT_MAX 和 INT_MIN 取决于编译器、体系结构等。
错误。范围是 0 到 UINT_MAX,通常是 INT_MAX*2 + 1
无符号整数在溢出和语义方面具有不同的行为。在 2 补码中,有一个未定义的有符号整数值(即,如果仅设置了最高位,其余为零),这在某种程度上具有双重含义。无符号整数可以使用全部位模式。
这是一个练习:在 32 位计算机上比较 printf("%d %u", 0xffffffff, 0xffffffff);
因为无符号整数的行为与有符号整数不同。
Wrong. The range is INT_MIN (which in 2 completment systems usually is -INT_MAX+1) to INT_MAX; INT_MAX and INT_MIN depends on the compiler, architecture, etc.
Wrong. The range is 0 to UINT_MAX which is usually INT_MAX*2 + 1
Unsigned integers have a different behaviour regarding overflow and semantics. In 2 complement there's one value undefined for signed integers (that is if only the uppermost bit is set, the rest zero), that has somewhat a double meaning. Unsigned integers can make use of the full range of bit patterns.
Here's an exercise: On a 32 bit machine compare the output of printf("%d %u", 0xffffffff, 0xffffffff);
Because unsigned integers behave differently than signed ones.