0
#include<stdio.h>
int main ()
{
    int *b;
    printf("The value is  %d\n",*b);
    return 0;
}

Here, in this program, the pointer b is uninitialised. When I run the program I always get the value 0. Shouldn't the pointer b always give different values like the garbage values, just like when we declare an uninitialised variable, like in this program:

#include<stdio.h>
 int main()
{
    int a; 
    printf ("The garbage value of a is = %d\n",a);
    return 0;
}
  • 7
    Printing an uninitialized value is undefined behavior. On some platforms, you will get 0. On others, you will get "garbage" values that may or may not change with each run. On other platforms, demons will fly out of your nose. – William Pursell Dec 02 '21 at 17:39
  • 1
    It does. The memory there just happens to be 0 each time you run it. There's no guarantee the data is "random" or different each time. It could be `0xDEADBEEF` each time. Given that it's also undefined behavior, it could even delete your entire hard drive or even summon the dead and it would be standards compliant. :D EDIT: dammit William, you beat me. – Qix - MONICA WAS MISTREATED Dec 02 '21 at 17:39
  • The point everyone here is making is, the value of an uninitialized pointer is undefined by the C standard. The compiler could choose to initialize it to zero, but this is not required nor guaranteed, so you the programmer should not rely on it being zero. – h0r53 Dec 02 '21 at 17:45
  • 1
    "Shouldn't the pointer b always give different values like the garbage values" --- zero is as good of a garbage value as any other. It doesn't have to change every execution. – Yakov Galka Dec 02 '21 at 17:46
  • When you said "Shouldn't it always give ______?", and no matter *what* word you fill in the blank with, you were expecting something, and the word "undefined" means *you cannot expect that*. You cannot expect *anything*. – Steve Summit Dec 02 '21 at 17:59
  • Uninitialized local variables *never* start out holding what you expect. If you expect them to be random, you'll find that (at least on any given day) they're utterly repeatable and predictable, and often 0. But if you expect them to be predictable (and especially, god help you, if you write code that depends on it), then by jingo, you'll find that they're quite random. – Steve Summit Dec 02 '21 at 18:01
  • But with that said, a better way to see whether `b` contained zero, or "random garbage", would be to do `printf("The pointer is %p\n", b);`. – Steve Summit Dec 02 '21 at 18:05
  • @h0r53: Per the C standard, the value of an uninitialized pointer is unspecified, not undefined. These are different things. – Eric Postpischil Dec 02 '21 at 18:23
  • @WilliamPursell: In general, when an uninitialized value is printed, the C standard only says the value is unspecified, not that the behavior is undefined. It is undefined in some cases, including the one shown in the question, where the object has automatic storage duration and has not had its address taken, but it is not generally so. – Eric Postpischil Dec 02 '21 at 18:24

0 Answers0