2

So I have been working on a question and this think stumbled upon me. When I declare a variable outside the main function, the program works correctly, that is it reaches the else case of "Friendship is magic" but if the variable is declared inside it returns Chris instead of Friendship statement.

int mis, chr;
int main() {
    int a, n, m; 
    cin >> a;
    for (int i = 0; i < a; i++) {
       //code here
    }
    if(mis > chr) 
        cout << "Mishka";
    else if(chr > mis)
        cout << "Chris";
    else
        cout << "Friendship is magic!^^";
}

The input that I am using makes the value of chr and mis equal so it should be evaluating to the else statement but instead it just stops at else if.

Kaneki
  • 99
  • 11
  • 2
    Variables declared at global scope initialise differently than variables initialised at local scape. I recommend always initialising your variables, by `int mis = 0, chr = 0` so it's clear. Also do you modify `mis` or `chr` during the `code here` loop? – Tas Apr 29 '20 at 04:15
  • I second Tas. Also, this might be an obvious suggestion, but have you tried outputting the values of both variables just before the `if` statements, to check what's going on? – CrazyChucky Apr 29 '20 at 04:20
  • @Tas Yes, I did modify the values of mis and chr at the code here part. – Kaneki Apr 29 '20 at 04:23
  • @CrazyChucky I did check the values of both the variables and they were correct – Kaneki Apr 29 '20 at 04:23
  • 2
    FYI: [SO: Uninitialized variable behaviour in C++](https://stackoverflow.com/q/30172416/7478597) – Scheff's Cat Apr 29 '20 at 04:33
  • 1
    probably there is a bug in the code that you didn't post. It would be better to copy and paste your real code (and the input you provide) – M.M Apr 29 '20 at 04:34

2 Answers2

5

"With great power (provided by C++) there must also come great responsibility"

And

"With uninitialized variables comes the undefined behavior"

Variables that are declared at global scope are being initialized by the compiler. However, the variables defined inside any function (i.e, having automatic storage) may contain garbage values (that could be different in each invocation of the program). I recommend always initializing your variables to some value.

int main() 
{
    int mis = 0, chr = 0;
    // ...

    return 0;
}

Let's come to your program now:

When I declare a variable outside the main function, the program works correctly, that is it reaches the else case of "Friendship is magic"

It is happening because the variables (on which your if ladder dependent) are being initialized to 0. Since, both variables have same value (0), the else part of the if statement is being executed.

but if the variable is declared inside it returns Chris instead of Friendship statement.

It's a perfect example of undefined behavior. If they are defined inside your function, they will be holding some garbage value and that might not be equal. Hence, what you are observing is an undefined behavior and you might get different results in different machines or even sometimes in a same machine.

abhiarora
  • 9,743
  • 5
  • 32
  • 57
  • This was what I originally thought too, but they said that their truncated code does set the values, and that they checked them by outputting them just before the conditionals and they were correct. Still a good idea to initialize everything upon declaration, though. – CrazyChucky Apr 29 '20 at 19:41
  • Don't forget to upvote and accept if it has solved your issue. – abhiarora Apr 30 '20 at 02:36
1

I tried your code and it works the same for me in both cases. So, I would say it changes with editor to editor and always initialize the variable before using it. Otherwise, we might face the same problem as you face. As it initializes the variables with garbage values.

shaw_7
  • 29
  • 6
  • 2
    The editor (you wrote the program in) has probably the least effect onto the Undefined Behavior of uninitialized local variables. It's Undefined Behavior regardless whether you wrote this code in Notepad++, VisualStudio, or with `cat` in the shell. ;-) – Scheff's Cat Apr 29 '20 at 05:06