为什么当数据类型为unsigned

为什么当数据类型为unsigned

本文介绍了为什么当数据类型为unsigned int无限循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面code运行perfectly.Gives正确的输出,但我改变变量的符号从符号到无符号的时刻程序运行到一个无限循环。
该计划是要找到整数的阶乘。
任何变量的值没有得到任何地方负
我知道的unsigned int型的模块化行为。

 #包括LT&;&stdio.h中GT;INT主要(无效)
{
    int类型的[200],我,索引,数字,接下来,计数,温度,测试,X;    scanf函数(%d个,&安培;试验);    而(test--)
    {
        scanf函数(%d个,&安培;号码);
        一个[0] = 1;
        数= 1; // 1位
        对于(下一= 2;接下来< =号;接下来++)
        {
            索引= 0;温度= 0;
            对于(i = 0; I<计数; ++ I)
            {
                X = A [索引] *接下来+温度;
                一个[索引] = X%10;
                TEMP = X / 10;
                ++指数;
            }
            而(温度!= 0)
            {
                一个[统计++] =气温%10;
                TEMP =温度/ 10;
            }
        }
        为(ⅰ=计数-1; I> = 0; - 一)
        的printf(%d个,一个[我]);
        的printf(\\ n);
    }
    返回0;
}


解决方案

这是 unsigned int类型永远是负的,所以 I> = 0 持有真正所有的时间,这意味着,像

循环

 无符号整型值;值= 10;
而(value--> = 0){}

是的有效的一个inifinte循环,以及你的为(I =计数 - 1; I> = 0; --i)

编译器警告做这个,所以如果您编译code传递正确的参数传给编译器,它应该告诉你,这个状况会总是如此。

您应该注意的是,虽然> = 使得在这方面没有任何区别,

 无符号整型值;值= 10;
而(value--!= 0){}

做的工作,为而(value--大于0)也做,因为 0 ,但它不能是< 0

还有,没有 unsigned int类型整数溢出,因此该循环将是无限的,而不会导致不确定的行为,的有当您添加 1 来的最高值,如果你减去一个从最小值会发生什么情况的说明,那么我相信你的的会发生什么,从相同的答案。

The below code runs perfectly.Gives the correct output but, the moment I change the sign of the variables from signed to unsigned the program runs into an infinite loop.The program is to find the factorial of integers.The value of any variable doesn't get negative anywhereI am aware of the modular behavior of the unsigned int.

#include<stdio.h>

int main(void)
{
    int a[200], i,index, number, next, count, temp, test, x;

    scanf(" %d", &test);

    while(test--)
    {
        scanf(" %d", &number);
        a[0]=1;
        count=1;    //1 digit
        for(next=2;next<=number;++next)
        {
            index=0;temp=0;
            for(i=0;i<count;++i)
            {
                x=a[index]*next+temp;
                a[index]=x%10;
                temp=x/10;
                ++index;
            }
            while(temp!=0)
            {
                a[count++]=temp%10;
                temp=temp/10;
            }
        }
        for(i=count-1;i>=0;--i)
        printf("%d",a[i]);
        printf("\n");
    }
    return 0;
}
解决方案

An unsigned int can never be negative, so i >= 0 holds true all the time, that means that a loop like

unsigned int value;

value = 10;
while (value-- >= 0) {}

is effectively an inifinte loop, as well as your for (i = count - 1 ; i >= 0 ; --i) is.

Compilers do warn about this, so if you compile your code passing the right flags to the compiler, it should tell you that the condition will be always true.

You should note that while the >= makes no difference in this respect,

unsigned int value;

value = 10;
while (value-- != 0) {}

does work, as while (value-- > 0) also does, because value can be 0, but it can't be < 0.

There is also, no unsigned int integer overflow, so the loop will be infinite without causing undefined behavior, this answer has an explanation of what happens when you add 1 to the maximum value, if you subtract one from the minimum value, then I am sure you can "guess" what would happen from the same answer.

这篇关于为什么当数据类型为unsigned int无限循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 00:34