-2

So this piece of program compiles and runs fine. but i don't understand why it is printing out 41. Since the variables are not global. They are local to the function.

For the sake of clarification i am changing the variable names in two different method. the name of variable being same has nothing to do with the result.

#include<stdio.h>

    void init();
    void _print(); 

    int main(){
        init();
        _print(); 
    }

    void init(){
       int myVar;
       myVar = 41; 
    } 

    void _print(){
       int xyz;
       printf("%d",xyz); 
    }
metadata
  • 38
  • 8
  • I'd look a bit into head and stack memory allocations and how they work. I am not knowledgeable enough to give a clear answer but basically, since the second variable is initialised, it points to the first variable and reads the first variable's value. If you name the variables differently or change types you will still have consistent results ( no random stuff or "magic" here ). If you use valgrind to check the program, it will show you that there is an error while executing it. – Matthieu Raynaud de Fitte Nov 12 '17 at 03:14
  • 1
    i will do that @MatthieuRaynauddeFitte. Thanks – metadata Nov 12 '17 at 03:17

3 Answers3

3

Like you said, the variables are local to each function. 42 just happens to be the value in your memory that is assigned to myVar in your _print() function. It's garbage value.

bitnahian
  • 516
  • 5
  • 17
  • even when i change the names of the variable in two different methods. It still ends up printing 41. I don't understand why void init(){ int myVar; myVar = 41; } void _print(){ int Var; printf("%d",Var); } – metadata Nov 12 '17 at 02:58
  • Yes. It doesn't necessarily mean anything. – bitnahian Nov 12 '17 at 02:58
  • Pretty sure this is related to heap and stack usage. Also, you should nave "myVar" from the _print() method differently. It yields the same results and makes it easier to understand. – Matthieu Raynaud de Fitte Nov 12 '17 at 03:04
  • I remember my professor used to always emphasise that variables in C must always be initialised. – bitnahian Nov 12 '17 at 03:05
  • they do, else they tend to read in an undefined location. In this case, since the code is small, it looks like it is the first variable. You can also test it by changing the first value's type and you will still get consistent results – Matthieu Raynaud de Fitte Nov 12 '17 at 03:09
  • Thanks @bitnahian for the response it makes sense now. – metadata Nov 12 '17 at 03:15
3

Here you are accessing the garbage value. The garbage value turns out to be 41. You run it next time or someday letter you may get different result. The value that is initially in myVar of _print() function is 41. More precisely the piece of memory that myVar got contains 41.(As an explalantion of possible reason) The previous call to init() function initialized a variable with 41, when that call is over, still that 41 stays in memory. You get that value next time.

Accessing an uninitialized value is Undefined behaviour.

user2736738
  • 30,591
  • 5
  • 42
  • 56
  • @metadata.: Yes it is simply some garbage value. You can run it multiple times or do anything, not everytime you will get the same result. It's undefined behavior to access uninitialized value. – user2736738 Nov 12 '17 at 03:03
  • if it was random you would get a different value each time but you do not. It is consistent – Matthieu Raynaud de Fitte Nov 12 '17 at 03:05
  • Thats my point i am not getting random value if i change that 41 to be something else like 78 it prints out 78 and the even when i make the variable names different in the two different function. It still prints out value from init function. Strange. – metadata Nov 12 '17 at 03:07
  • thanks @coderredoc i think it makes sense now. – metadata Nov 12 '17 at 03:14
  • 1
    @MatthieuRaynauddeFitte.: If we consider that calling to a function is realized using stack frame then the first call to the function pushes a stack frame with value 41 assigned to a variable. Then it is popped. Then again a function is called with same number of local variables.But the memory still contains the previosu 41 value. That's why it gets it. – user2736738 Nov 12 '17 at 03:19
  • @MatthieuRaynauddeFitte.: Now if you use more local variables then you might get different result. The alignment and all that may differ. Maybe they will be laid out differenlt in memory. That value is indeterminate, That's all I wanted to say – user2736738 Nov 12 '17 at 03:20
2

From C Standards#6.7.9p10:

If an object that has automatic storage duration is not initialized explicitly, its value is indeterminate.

In C language, there is no default value for non-static local variables. The variable holds whatever garbage happened to be at that memory location right before the storage for the variable was allocated from the stack.

The value 41 you are getting for uninitialized variable xyz is garbage value.

Community
  • 1
  • 1
H.S.
  • 11,654
  • 2
  • 15
  • 32