1

I was doing some amateur practicing and ran into this error:

#include <stdio.h>

int main(){

int answer = 5;
int guess;

while (guess != answer) {
    printf("enter a number: ");
    scanf_s("%d", &guess);
}
printf("you guessed the correct number which was 5!");
    
return 0;
}

It's pretty simple while loop practice but, for some reason it won't work even though I'm doing the exact same thing as the teacher on this video is telling me to do.

I fixed it by just giving int guess a value, like int guess = 0.

But why does that code work in the video but not in my compiler?

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
lmgesus
  • 9
  • 4
  • 1
    Do not confuse 'warning' and 'error', please. Your code will work, yes. But only as long as you input valid strings on the console. If you input, for example 'Supercalifragilistig', scanf would fail and guess would be 'uninitialized', meaning that it would be a pseudorandom value. Your compiler is just trying to help you here. – Refugnic Eternium Oct 25 '21 at 07:41
  • What do you mean that it does not work? Looks like a valid code that works. You have a good room now to learn using a debugger also. Just put a breakpoint and see how the program goes on. – Ilian Zapryanov Oct 25 '21 at 07:43
  • 3
    If the code "works", then it is just accidentally. If your `guess` happens to hold `5` initially, you will not get asked to input a number. If your teacher did the same, it is still broken. – Gerhardh Oct 25 '21 at 07:48
  • 1
    @RefugnicEternium The code *may* work. As `guess` is uninitialized, then it *could* start of with the value of `5`, in which case the code *won't* work. – Adrian Mole Oct 25 '21 at 07:49
  • @Refugnic Eternium the code just out right refused to run in visual studio 2019. I *have* to write "int guess = 0" for the code to run at all. – lmgesus Oct 25 '21 at 07:52
  • 1
    @AdrianMole Please refer to my second sentence: 'Only as long as you input valid strings on the console'. So yes, it may work. Or it may not. – Refugnic Eternium Oct 25 '21 at 07:52
  • 2
    @RefugnicEternium But it may not even give you the chance to input anything. – Adrian Mole Oct 25 '21 at 07:53
  • 1
    @Adrian Mole Ah, I missed the condition overhead. Sorry, my bad. – Refugnic Eternium Oct 25 '21 at 07:54
  • 3
    The trouble with learning from online videos is that it's difficult to fire the teacher! That is just bad code. – Adrian Mole Oct 25 '21 at 07:57
  • @Ilian Zapryanov I mean the compiler just out right refuses to run the code. I essentially copied and pasted the code to my computer and it just won't run at all. I think the real question I'm trying to ask is, how is it possible that I can enter the same code in this computer and have it not run but when someone else enters the exact same code, it works just fine. FYI: I'm getting a warning **AND** an error. – lmgesus Oct 25 '21 at 08:03
  • Well, you should actually thank your compiler for having pointed out that mistake (yes, it's a bug, there are no excuses) unlike others ;). – Bob__ Oct 25 '21 at 08:18

2 Answers2

1

This is not an error, but a warning from your compiler. It is telling you, that you are accessing an undefined value, because you did not initialize your variable.

A local variable that is not explicitely given a value is called 'uninitialized', because it assumes the value of 'whatever has been in the allocated block of memory', which far more often than not results in a garbarge value and definitely not what you want.

The first issue is in the loop. Depending on the garbage value in 'guess', it may decide to not run your loop at all (because 'guess' just might hold the value '5' by chance).

Another problem is your use of scanf. If the input does not match your requirements (i.e. not a number), guess will remain unchanged. Please refer to the documentation on scanf for more details.

As a little anecdote I like to tell: I once had a bool (C++, should only be true or false) variable that was uninitialized under certain circumstances.

Then, down the road, it was checked against if (!variable) and, in the else condition it read else if (variable == true). It did not enter either branch, because, as the debugger revealed, the value of my variable was neither true nor false. It was 42. That's the kind of danger uninitialized variables hold. You'll never know what you get.

Refugnic Eternium
  • 4,089
  • 1
  • 15
  • 24
1

Using an uninitialized variable for anything (other than assigning it a value) invokes undefined behavior. Here you use it as an operand in a comparison. That means anything can happen, including making the impression of everything to be working as intended.

The compiler is not required to report an error or even warn you in these cases (although it may do so). The code is just wrong.

If this teacher chose to ignore this matter, you should consider to learn from someone else.

For further reading:

moooeeeep
  • 31,622
  • 22
  • 98
  • 187