0

I am a noob so plz be merciful,

in the code given below, plz focus on array named B, numbers at position 0 and 2 are declared as 1 and 3, and number on 1st position is not declared, when i run it on an online compiler like onlinegbd, or programiz etc,

#include <stdio.h>

int main()
{
    int j, B[3];
    B[0]=1;
    B[2]=3;
    
    for (j=0; j<3; j++)
    {
        printf("%d\n", B[j]);
    }

    return 0;
}

the output is

1
0
3

but when I run the same program on my compiler (MinGW) and code blocks (software), I get

1
-1405179899
3

or

1
-1895397996
3

so i am not sure what to say here, it also gives positive values as shown in the title as well, plz help me

sepp2k
  • 363,768
  • 54
  • 674
  • 675
Nikhil
  • 63
  • 6
  • i am concerned because when using bitwise and logical operators such things matter a lot – Nikhil Nov 19 '20 at 07:47
  • 2
    Reading uninitialized local variables is undefined behavior. You may get different values on different compilers, different computers, or different days of the week. You may also get an error, a crash, severe data corruption, or demons flying out of your nose. Just don't do it! – Nate Eldredge Nov 19 '20 at 07:54
  • `0` is a perfectly valid **garbage** value; as much as `-1405179899` :-) – pmg Nov 19 '20 at 08:07
  • thanks, @pmg, but still these garbage values will vary all the time, so when they are used as an input somewhere in the program, we will have varying results, off course, this thing is not used by sane programmers – Nikhil Nov 19 '20 at 08:22

0 Answers0