用C无符号整数 [英] unsigned integers in C
本文介绍了用C无符号整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我运行程序下面的输出像109876543210-1-2-3-4-5-6-78-9-10-11-12和S0上。为什么这样?什么是无符号整数的概念?
的main()
{
无符号整型我;
对于(I = 10; I> = 0;我 - )
的printf(%D,我);
}
解决方案
无符号整数总是非负的,大于或等于0。当你从0减去1你结束了一个MAX_INT无符号整型
因此,你的循环不会终止。
不过,你需要的,如果你想让它打印无符号的价值,而不是符号值在你的printf用%U而不是%d按钮。
While i am running the program below it outputs like 109876543210-1-2-3-4-5-6-78-9-10-11-12-and s0 on. Why so? What is the concept of unsigned integer?
main ()
{
unsigned int i;
for (i = 10; i >= 0; i--)
printf ("%d", i);
}
解决方案
Unsigned integers are always non-negative, that is greater than or equal to 0. When you subtract 1 from 0 in an unsigned integer type you end up with MAX_INT.
Therefore, your for loop will never terminate.
However, you need to use "%u" not "%d" in your printf if you want it to print the unsigned value rather than the signed value.
这篇关于用C无符号整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文