1

I'm new to C decided to learn it myself because of certain job requirements (knew some java before this). 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. 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. 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? The status variable is clearly not 1 anymore at this point. However it does stop when I enter q as suggested by the printed sentence to enter q to quit.

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 (e.g. if the input is a char) break the loop. 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.

11
  • 2
    You have a typo: %1d --> %ld Commented Oct 11, 2016 at 6:21
  • 2
    You don't read a number into status. You read a number into num and then status is assigned the number of values read in the scanf call. As long as you input any valid number, status will be 1 Commented Oct 11, 2016 at 6:22
  • 1
    Why do you think the status variable isn't one anymore? You say it's "Clearly not 1 anymore", but why shouldn't it still be one? Commented Oct 11, 2016 at 6:22
  • How do you know ' when the check on the variable status for the while loop is not "true" anymore.'? Try printing the status value just after the scanf to make sure. Commented Oct 11, 2016 at 6:23
  • 1
    @Darkphoton: the value read and converted is stored into the variable whose address is passed as an argument to scanf. You probably never saw nor wrote code that stores the return value of scanf(). This function has many quirks, it is widely misunderstood. Read the specification carefully and use wisely. Commented Oct 11, 2016 at 6:43

2 Answers 2

4

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. Replace it with %ld ('ell' for 'long'). Similary in printf the 'l' modifier tells the function what size of int it got to print.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks I've changed a couple typos (the book is extremely old I kept reading the l's as 1's) and now the program sums properly.
3

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".

It is. And status is the return value from scanf, which is the number of fields read. That will be one if and only if one valid field was read.

3 Comments

So that's why, that makes a lot of sense now. I honestly could not find in any tutorial that this is comparing it to the number of fields read - up to this point when reading in a value with scanf I'm used to storing that as a variable and then using that to compare to something else. So why is status = scanf("%ld", &num) not assigning status to the long that is entered on console and in fact the return value?
@Darkphoton: scanf can read and convert multiple fields at a time. It takes extra arguments, one address for each converted field. The return value is the number of fields converted or EOF if the end of file was encountered before the first conversion. The name status used in the textbook is misleading, but in your case, it can receive 1 if a number was converted and stored into num, 0 if you type something that is not a number, such as a letter, or EOF at end file.
@chqrlie: Thanks again, I answered your comment in your other reply above. Yes I agree the variable names made it extra confusing so I looked at the assignments in the wrong manner. It's a lot clearer now thank you :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.