为什么我的程序在 scanf("%i", x); 之后退出,其中 x 是 int x;

问题描述 投票:0回答:3

我的代码在第二个分数后停止接受输入并结束执行。我想不出为什么会这样做 下面的代码接受我的第一个和第二个分数输入,跳过第三个,然后结束执行而不进一步。

下面的 C 代码在第二个之后停止接受输入并退出。它接受第一个和第二个输入,跳过第三个输入,然后退出。

如何解决此错误?以下是我提供的代码。

#include <stdio.h>
int main(void)
{
    int scores[3];
    int x;
    int y;
    int z;
    printf("first score is:\n");
    scanf("%i", x);
    scores[0] = x;
    printf("second score is:\n");
    scanf("%i", y);
    scores[1] = y;
    printf("third score is:\n");
    scanf("%i", z);
    scores[2] = z;
    printf("average is %f\n", (scores[0]+scores[1]+scores[2])/(float)3);
}
arrays c scanf
3个回答
2
投票

您需要将变量的地址传递给

scanf
才能使其正常工作。由于指针是整数类型,因此传递整数而不是指针可以“工作”,但会导致未定义的行为。带有警告的编译应该会告诉你这一点。如果我用
-Wall -Wextra
编译你的代码,我会看到几个,包括有关传递给
scanf
的参数类型的警告。

如果我进一步添加

-Werror
,那么警告将被视为错误,我必须在我的代码编译成可以运行的代码之前修复此问题。 建议:修复第一个错误/警告first,然后重新编译。

test.c:9:17: warning: format specifies type 'int *' but the argument has type 'int' [-Wformat]
    scanf("%i", x);
           ~~   ^
test.c:12:17: warning: format specifies type 'int *' but the argument has type 'int' [-Wformat]
    scanf("%i", y);
           ~~   ^
test.c:15:17: warning: format specifies type 'int *' but the argument has type 'int' [-Wformat]
    scanf("%i", z);
           ~~   ^
test.c:9:17: warning: variable 'x' is uninitialized when used here [-Wuninitialized]
    scanf("%i", x);
                ^
test.c:5:10: note: initialize the variable 'x' to silence this warning
    int x;
         ^
          = 0
test.c:12:17: warning: variable 'y' is uninitialized when used here [-Wuninitialized]
    scanf("%i", y);
                ^
test.c:6:10: note: initialize the variable 'y' to silence this warning
    int y;
         ^
          = 0
test.c:15:17: warning: variable 'z' is uninitialized when used here [-Wuninitialized]
    scanf("%i", z);
                ^
test.c:7:10: note: initialize the variable 'z' to silence this warning
    int z;
         ^
          = 0
6 warnings generated.

更正:

#include <stdio.h>

int main(void)
{
    int scores[3];
    int x;
    int y;
    int z;
    printf("first score is:\n");
    scanf("%i", &x);
    scores[0] = x;
    printf("second score is:\n");
    scanf("%i", &y);
    scores[1] = y;
    printf("third score is:\n");
    scanf("%i", &z);
    scores[2] = z;
    printf("average is %f\n", (scores[0]+scores[1]+scores[2])/(float)3);
}

当然,

x
y
z
都是多余的。

#include <stdio.h>

int main(void)
{
    int scores[3];

    printf("first score is:\n");
    scanf("%i", &scores[0]);

    printf("second score is:\n");
    scanf("%i", &scores[1]);

    printf("third score is:\n");
    scanf("%i", &scores[2]);

    printf("average is %f\n", (scores[0]+scores[1]+scores[2])/(float)3);
}

您应该每次检查

scanf
返回
1
,表明它已成功读取一个值。如果没有,您应该采取一些措施,否则您正在使用初始化的数据,这意味着您的程序可以打印anything

此外,当您处理数组时,重复代码来读取多个值是一种浪费。使用循环。

#include <stdio.h>
#include <stdlib.h>

#define ARR_SIZE 3

int main(void)
{
    int scores[ARR_SIZE];

    for (size_t i = 0; i < ARR_SIZE; i++) {
        printf("Score %d is:\n", i);
        if (scanf("%d", &scores[i]) != 1) {
            printf("Invalid input\n");
            exit(EXIT_FAILURE);
        }
    }

    int sum = 0;

    for (size_t i = 0; i < ARR_SIZE; i++) {
        sum += scores[i];
    }

    printf("Average is %f\n", sum/(float)ARR_SIZE);
}

1
投票

令我惊讶的是该程序甚至在编译时没有出现警告或错误。

scanf
将指向
int
的指针作为第二个参数。所以它应该是:

scanf("%i", &x);

y 和 z 也类似。


0
投票

代码中的问题在于

scanf
函数。
scanf
函数需要存储输入值的变量地址,但您直接传递值。

这是更正后的代码:

#include <stdio.h>

int main(void)
{
    int scores[3];
    int x, y, z;

    printf("first score is:\n");
    scanf("%i", &x);
    scores[0] = x;

    printf("second score is:\n");
    scanf("%i", &y);
    scores[1] = y;

    printf("third score is:\n");
    scanf("%i", &z);
    scores[2] = z;

    printf("average is %f\n", (scores[0] + scores[1] + scores[2]) / (float)3);

    return 0;
}

在此更正后的代码中,关键的更改是在

scanf
调用中,其中
&x
&y
&z
用于将
x
y
z
的地址传递给
scanf
,允许它将用户输入存储在这些变量中。

我希望这能解决您的问题。如需任何进一步的帮助或疑问,您可以在下面发表评论。

© www.soinside.com 2019 - 2024. All rights reserved.