0

Why does this program stop working if I leave out the reference to userNumber in the scanf function?

#include <stdio.h>

int main()
{
    int userNumber;

    printf("Enter a number: ");
    scanf("%d", &userNumber);

    while (userNumber != 10)
    {
        printf("\nWrong number. Try again\n");
        printf("\nEnter a number: ");
        scanf("%d", &userNumber);
    }
    return 0;
}
mch
  • 9,424
  • 2
  • 28
  • 42
Njgardner90
  • 61
  • 1
  • 7
  • *where* do you leave *what* out? –  Jun 23 '17 at 13:06
  • If you mean you just write `scanf("%d");` -- this is **undefined behavior**. The language requires a parameter to receive the result for each conversion specifier. –  Jun 23 '17 at 13:08
  • 1
    Sidenote: C does not support references. You pass pointers to `scanf`. – too honest for this site Jun 23 '17 at 13:09
  • So you ask why your program behaves undefined when you invoke undefined behaviour? I don't see a problem, apparently your code does the expected. What about the documentation is not clear? – too honest for this site Jun 23 '17 at 13:10
  • Trying to make some sense of this question: Please tell us what you want to **achieve** by leaving out `&userNumber`. –  Jun 23 '17 at 13:14

1 Answers1

2

scanf("%d") requires to pass a pointer as its second argument, thus you really need to write:

int userNumber;
scanf("%d", &userNumber);

If you take out the reference, then input is not read correctly, and userNumber is actually left uninitialized, which causes Undefined Behavior, which probably explains the behavior you witness.

You should have seen a warning, if you had removed the reference from the first scanf:

Georgioss-MacBook-Pro:~ gsamaras$ gcc main.c 
main.c:8:17: warning: format specifies type 'int *' but the argument has type
      'int' [-Wformat]
    scanf("%d", userNumber);
           ~~   ^~~~~~~~~~
1 warning generated.
gsamaras
  • 71,951
  • 46
  • 188
  • 305
  • uhm. Not the *prototype*. The prototype doesn't tell you anything in case of `scanf()`. –  Jun 23 '17 at 13:08
  • @FelixPalmen yeah you are right, updated, do you like it now? – gsamaras Jun 23 '17 at 13:10
  • Not my downvote, as it's good enogh for such a strange question ... but to be precise, there's no *second argument* required at all. The requirement is that you have to pass a pointer for each conversion in the format string that isn't suppressed with `*`. (so, with `%d` as the format string, the second argument *is* required) –  Jun 23 '17 at 13:12
  • @FelixPalmen right, edited again, thanks for helping improve my answer. – gsamaras Jun 23 '17 at 13:13
  • Thank you for your responses. Am following a tutorial to learn c and decided to extend on it a bit by adding a loop. I haven't really covered pointers yet, so don't quite understand what is going on here? – Njgardner90 Jun 24 '17 at 16:30
  • Well, you should study more then and read that chapter, because now it's too broad to explain @Njgardner90. BTW do not forget to *accept* an answer! – gsamaras Jun 24 '17 at 16:32
  • My apologies. I'm new to the whole setup here. Thanks again – Njgardner90 Jun 24 '17 at 17:33