简体   繁体   English

即使条件不成立,也退出了C语言中的while循环

[英]While loop in C exited even when the condition is not true

I'm new to C decided to learn it myself because of certain job requirements (knew some java before this). 我是C语言的新手,由于某些工作要求,我决定自己学习它(在此之前了解一些Java)。 I hope I don't get downvoted for this question because I've searched everywhere about while loops in C, the usage of scanf() and variable assignments to see if I've missed anything but nothing explains why this code below runs even when the check on the variable status for the while loop is not true anymore. 我希望我不会对此问题感到沮丧,因为我到处搜索了C语言中的while循环, scanf()和变量赋值的用法,以查看是否遗漏了任何内容,但没有任何内容可以解释为什么即使在while循环的变量status检查不再成立 This program was taken from a text book but it doesn't explain why it works either it's just an example. 该程序取自一本教科书,但仅说明一个例子,没有说明为什么会起作用。

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

int main(void) {
    long num; 
    long sum = 0L; 
    int status;

    printf("Please enter an integer to be summed ");

    printf("(q to quit): ");

    status = scanf("%ld", &num);

    while (status == 1) {
        sum = sum + num; 
        printf("Please enter next integer (q to quit): ");
        status = scanf("%ld", &num);
    }

    printf("Those integers sum to %ld.\n", sum);

    return 0;   
}

When I enter a number on the console (to be read as input by scanf ) and from my understanding that number is clearly assigned to the variable status . 当我在控制台上输入一个数字(由scanf读取为输入)时,根据我的理解,该数字显然分配给了变量status The while loop however is checking each iteration if status == 1 so why when I enter a number to be read by scanf after the while loop starts and that gets assigned to status the loop keeps going? 但是while循环会检查status == 1是否为status == 1每个迭代,所以为什么当我输入一个要在while循环开始scanf读取的数字并将其分配给status ,循环继续进行? The status variable is clearly not 1 anymore at this point. 此时, status变量显然不再为1 However it does stop when I enter q as suggested by the printed sentence to enter q to quit. 然而,当我进入它停止q由印刷句子的建议进入q退出。

I've considered that it's perhaps checking if the value you entered is an integer or long to begin with, and if it's neither of those (eg if the input is a char ) break the loop. 我认为这可能是在检查您输入的值是一个integer还是long的开头,以及是否都不是(例如,如果输入是char )打破循环。 But the problem is I cannot find a confirmation or explanation for this anywhere, of why isn't the check for the while loop checking if status is that exact integer 1 . 但是问题是我在任何地方都找不到对此的确认或解释,为什么不检查while循环以检查status是否为正整数1

Remove the ampersand from 从中删除&符号

printf("Those integers sum to %1d.\n", &sum);

it should be 它应该是

printf("Those integers sum to %ld.\n", sum);

Edit As others pointed out, the one in %1d is a typo, and it causes your scanf to read a one-digit int value. 编辑正如其他人指出的那样, %1d中的一个是拼写错误,它使您的scanf读取一位int值。 Replace it with %ld ('ell' for 'long'). 将其替换为%ld (“ ell”表示“ long”)。 Similary in printf the 'l' modifier tells the function what size of int it got to print. printf类似,'l'修饰符告诉函数它要打印什么int大小。

But the problem is I cannot find a confirmation or explanation for this anywhere, of why isn't the check for the while loop checking if status is that exact integer "1". 但是问题是我无法在任何地方找到对此的确认或解释,为什么不检查while循环以检查状态是否为该整数“ 1”。

It is. 它是。 And status is the return value from scanf , which is the number of fields read. statusscanf的返回值,即读取的字段数。 That will be one if and only if one valid field was read. 当且仅当读取一个有效字段时,该字段才为1。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM