0

I am new at C and using DevC++ as my IDE.

I learned that if variables are declared but not assigned any value they are initialized with a random value (except static and global variables which are initialized with 0).

But when I tried this:

int i, j, k;             
printf("%d %d %d", i, j, k);

it printed:

always 0 for i; any large random integer (always different) for j; a large random integer (always same) for k.

here is the output on running the above code 5 times: 0 13308816 32764 0 12391312 32764 0 11408272 32764 0 11015056 32764 0 7541648 32764

But I think these all should print random values (garbage values), different every time (same as for j).

  • 2
    `I learned that if variables are declared but not assigned any value they are initialized with a random valu` wrong!! Infact they are not initialized to any value at all. Whatever values that particular memory was having previously will be left as it is. – kiran Biradar Aug 24 '19 at 17:00
  • 1
    Slightly off-topic, no one should be using DevC++ in 2019. – Fang Aug 24 '19 at 17:07

1 Answers1

3

C has always been very specific about the initial values of objects. If global or static, they will be zeroed. If auto, the value is indeterminate.

Lakshitha Wisumperuma
  • 1,574
  • 1
  • 10
  • 17