Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/63.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C scanf()函数错误?_C_Integer_Scanf - Fatal编程技术网

C scanf()函数错误?

C scanf()函数错误?,c,integer,scanf,C,Integer,Scanf,我很难弄明白为什么会发生这种情况。当我在linux终端上编译和运行我的代码时,scanf()函数似乎工作得很好,因为它接受输入,但是由于某种原因,它实际上没有将该值存储到指定的变量中,而是将其设为1。 我已经看过了我的代码,无法看到和发布,但我确实缺乏C语言的知识,所以非常感谢您的帮助 int main() { int a, b, c, d, e, f; printf("Please enter the value of a: "); a = scanf("%d", &

我很难弄明白为什么会发生这种情况。当我在linux终端上编译和运行我的代码时,
scanf()
函数似乎工作得很好,因为它接受输入,但是由于某种原因,它实际上没有将该值存储到指定的变量中,而是将其设为1。 我已经看过了我的代码,无法看到和发布,但我确实缺乏C语言的知识,所以非常感谢您的帮助

int main() {

    int a, b, c, d, e, f;
    printf("Please enter the value of a: ");
    a = scanf("%d", &d);
    printf("a = %d\n", a);

    printf("Please enter the value of b: ");
    b = scanf("%d", &e);
    printf("b = %d\n", b);

    printf("Please enter the value of c: ");
    c = scanf("%d", &e);

    printf("C = %d\n", c);
    c = c + b;
    printf("C = %d\n", c);

    if (a > 1) {
        printf("B = %d\n", b);
        printf("C = %d\n", c);
    } else {
        printf("A is smaller or equal to 1 \n");
    }

    if (b > 3 || c > 3) {
        printf("A = %d\n", a);
    } else {
        printf("B is less than or equal to 3\n");
        printf("B = %d\n", b);
        printf("C = %d\n", c);
    }
}

非常感谢您的帮助。

我认为这里的混淆源于存储扫描值的变量以及从
scanf()返回的内容

int main() {

    int a, b, c, d, e, f;
    printf("Please enter the value of a: ");
    a = scanf("%d", &d);
    printf("a = %d\n", a);

    printf("Please enter the value of b: ");
    b = scanf("%d", &e);
    printf("b = %d\n", b);

    printf("Please enter the value of c: ");
    c = scanf("%d", &e);

    printf("C = %d\n", c);
    c = c + b;
    printf("C = %d\n", c);

    if (a > 1) {
        printf("B = %d\n", b);
        printf("C = %d\n", c);
    } else {
        printf("A is smaller or equal to 1 \n");
    }

    if (b > 3 || c > 3) {
        printf("A = %d\n", a);
    } else {
        printf("B is less than or equal to 3\n");
        printf("B = %d\n", b);
        printf("C = %d\n", c);
    }
}
  • 传递给
    scanf()
    的第二个参数是存储扫描值的地址
  • scanf()
    的返回值是扫描的值数(或少数错误代码之一)
了解了这一点后,再看一看这段代码:

int a,b,c,d,e,f;
printf("PLease enter the value of a: ");
a = scanf("%d", &d);
printf("a = %d\n",a);
第三行扫描一个值并将其存储在变量
d
(因此
scanf()
调用末尾的
&d
)。它还存储扫描到变量
a
中的值的数量。然后,它打印出
a
——此时,它是1,因为前面的
scanf()
调用只找到一个值


一个更好的模式是扫描您正在询问的变量(因此,在本例中,将
&A
传递给
scanf()
),然后检查返回值是否存在任何错误,然后再继续执行程序。

我认为这里的混淆源于存储扫描值的变量,以及从
scanf()
返回的内容

  • 传递给
    scanf()
    的第二个参数是存储扫描值的地址
  • scanf()
    的返回值是扫描的值数(或少数错误代码之一)
了解了这一点后,再看一看这段代码:

int a,b,c,d,e,f;
printf("PLease enter the value of a: ");
a = scanf("%d", &d);
printf("a = %d\n",a);
第三行扫描一个值并将其存储在变量
d
(因此
scanf()
调用末尾的
&d
)。它还存储扫描到变量
a
中的值的数量。然后,它打印出
a
——此时,它是1,因为前面的
scanf()
调用只找到一个值


更好的模式是扫描您正在询问的变量(因此,在本例中,将
&A
传递给
scanf()
),然后检查返回值是否存在任何错误,然后再继续执行程序。

您的意思是只需去掉&d,使代码看起来像这样:scanf(%d),&A);printf(“a=%d\n”,a);?如果是这样,那么我如何消除这个错误:示例3.c:6:8:警告:忽略“scanf”的返回值,用属性warn\u unused\u result[-Wunused result]scanf(“%d”,&a)声明;因为这是我首先做的。同样,感谢您的贡献,我至少知道为什么值总是1,因为我只输入1个值。这样的更改将使键入的值进入变量
a
。警告告诉您没有检查扫描了多少值;例如,
scanf()
如果扫描一个整数但键入“asdf”,则可能返回0。您可以执行类似于
int scanned=scanf(“%d”和&a)的操作;如果(扫描<1)返回-1scanf()
不成功,请提前退出程序。非常感谢,我对Objective C的知识相当有限,并且来自java,但这已经很好地解释了,它现在可以工作了:)那么您的意思是去掉&d,使代码看起来像这样:scanf(“%d”,&a);printf(“a=%d\n”,a);?如果是这样,那么我如何消除这个错误:示例3.c:6:8:警告:忽略“scanf”的返回值,用属性warn\u unused\u result[-Wunused result]scanf(“%d”,&a)声明;因为这是我首先做的。同样,感谢您的贡献,我至少知道为什么值总是1,因为我只输入1个值。这样的更改将使键入的值进入变量
a
。警告告诉您没有检查扫描了多少值;例如,
scanf()
如果扫描一个整数但键入“asdf”,则可能返回0。您可以执行类似于
int scanned=scanf(“%d”和&a)的操作;如果(扫描<1)返回-1
如果
scanf()
不成功,请提前退出程序。非常感谢,我对Objective C的知识相当有限,并且来自java,但这已经很好地解释过了,它现在可以工作了:)