-3
#include <stdio.h>

int main(){
    int a = 3;
    int b[] = {1,2};
    int c = 4;
    b[3] = 10;

    printf("a = %d\n",a);

    return 0;
}

Output: 10

It is an integer output perhaps, it does not carry any garbage value too, but after when i assign the value to the variable a as a = 5 and above value 5, it just prints the output as it is, tell me what's going on here!

Thirumalai vasan
  • 655
  • 2
  • 6
  • 19

1 Answers1

7

There is undefined behavior in your code which is

b[3] = 10;

When you initialized your array you have allocated space only for 2 items.You are trying to access 4th element and that memory is not owned by array so you are getting wierd results. It has happened that the varible a is stored in the same location as b[3] so that is overwritten

Try to print sizeof your array and check how much memory is allocated for the array

Gopi
  • 19,784
  • 4
  • 24
  • 36