C 传递给函数isn';t设置为我分配给它的值

C 传递给函数isn';t设置为我分配给它的值,c,memory,variable-assignment,C,Memory,Variable Assignment,为了从给定数字的左侧返回第I个数字,我创建了以下函数: int ith(unsigned long x, int i) { int count=0; unsigned long y = x; do { y /= 10; count++; } while (y > 0); if (i > count) return -1; count -= i; while (count

为了从给定数字的左侧返回第I个数字,我创建了以下函数:

int ith(unsigned long x, int i)
{
    int count=0;
    unsigned long y = x;

    do
    {
        y /= 10;
        count++;
    } while (y > 0);
    if (i > count)
        return -1;
    count -= i;
    while (count > 0)
    {
        x /= 10;
        count--;
    }
    return x % 10;

}
但是,当函数的输入为2,2时。花了很多时间才把这个功能翻译成芬兰语; 因此,我查看调试器,发现
count==54353453
count
从未收到我分配给他的
0

编辑 下面是我的代码的其余部分,一些人认为这可能是这个错误的根源

int main()
{
    int x, i,result;
    do
    {
        printf("Enter a number and the digit you want to retrieve.\nEnter a negative number to exit\n");
        scanf("%ul %d", &x, &i);
        if (x<0)
        {
            printf("Operation Aborted.. Exiting....\n");
            break;
        }
        else
        {
            result = ith(x, i);
            if (result == -1)
            {
                printf("Error: There are no such amount of digits to retrieve from that location\n");
                continue;
            }
            printf("The %dth digit of the number %ul is %d\n", i, x, result);
        }

    } while (x >= 0);
}
intmain()
{
int x,i,结果;
做
{
printf(“输入一个数字和要检索的数字。\n输入一个负数以退出\n”);
扫描频率(“%ul%d”、&x和&i);
如果(x=0);
}
这一行

scanf("%ul %d", &x, &i);
x
中扫描时使用了错误的说明符

由于定义了
x
int
,因此必须

scanf("%d %d", &x, &i);
或者将
x
定义为
unsigned long
,则必须

scanf("%lu %d", &x, &i);

同样的问题也在这里:

printf("The %dth digit of the number %ul is %d\n", i, x, result);

无法复制可能代码中的其他地方存在UB问题。
x
的键入不正确:
int
/
无符号long
/
long
%ul
在任何情况下都是错误的。请注意,查看
ith
的原型,可能是另一种情况:
x
的键入错误(应该是
未签名的long
)谢谢。我的问题是:A.我没有声明X为Nessery.B.我在scanf中写了%ul而不是%lufunction@basddsasda:编译器应该警告您后一种情况。