0

I'm using code blocks gcc compiler, I also tried it on some online compilers but all of them gives zero, so it no longer gives random numbers or is it my system?

#include <stdio.h>

int main(void)
{
    int value;

    printf("%d\n", value); // Always printing 0
    return 0;
}
David Ranieri
  • 39,972
  • 7
  • 52
  • 94
  • 2
    An uninitialized value is not required to contain garbage, in this case you are reading from clean pages. – David Ranieri Feb 14 '21 at 07:52
  • 3
    No particular reason. Thus the nature of *undefined behavior*. Do not assume *observed* behavior is *defined* behavior. The latter will lead to the former, but the former is not conclusive proof of the latter. – WhozCraig Feb 14 '21 at 07:57
  • The value of uninitialized local variables is *indeterminate*. It *could* be seen as random or "garbage" but in short you can't rely on it having any specific value at all. – Some programmer dude Feb 14 '21 at 07:57
  • 1
    "*Always printing 0*" [Not always](https://godbolt.org/z/vcMzEx). – dxiv Feb 14 '21 at 07:59
  • It is common for the operating system (including components such as the program loader) to start new programs with zero-initialized memory for the parts that are not otherwise initialized. That includes the stack. So a program that is compiled without optimization and that attempts to use an object allocated on the stack but not explicitly initialized may often get zero for the object if no other part of the program has yet used that part of the stack for other purposes. Computers rarely give you **random** numbers. They behave mechanically. – Eric Postpischil Feb 14 '21 at 11:26

0 Answers0