-1
#include<stdio.h>
void main(void)
{   char m,n;
    printf("%d\n",m);//value of m
    printf("%d",n);//value of n
}

in the above snippet value of m is always printed as 0 why?why it doesn't change even after multiple times compilation is it automatically assigned if we do not assign while the value of n always changes so why not both change randomly every time i compile?


Am i missing any concept?

PIRATE
  • 1
  • They don't have any value; they are uninitialized and their values are *indeterminate*. – P.P Sep 17 '17 at 18:00
  • It is possible that the compiler generates code that sets local variables to 0, but it is not required to do so, and does not contravene the C standard. It is also possible that the value always *happens* to be 0 for some reason not worth exploring. Either way, you must set the value specifically before reading it. – Weather Vane Sep 17 '17 at 18:18

1 Answers1

0

Local variables like yours are automatic variables. They are allocated on stack memory and their value is garbage.

Global variables are implicitly have static storage class and have value 0 by default.

Since m is a local variable, its value needn't always be the same. It is indeterminate. This memory location can be given to other processes.

J...S
  • 5,079
  • 1
  • 20
  • 35