Stack Exchange
Two equality comparisons with NULL in C
This is regarding a small confusion regarding pointers in C++ while comparing them to NULL.
Here's the code :
struct node
{
  int data;
  struct node *left;
  struct node *right;
}
int main()
{
  struct node *p;
  if(p!= NULL)
     printf("line1\n");
   else
     printf("line2\n");
   struct node *temp;
   if(temp == NULL)
     printf("line3\n");
} 

The output:
line2
line3

While for the following piece of code:
struct node
{
  int data;
  struct node *left;
  struct node *right;
}
int main()
{
  struct node *p;
  if(p!= NULL)
     printf("line1\n");
   else
     printf("line2\n");
   struct node *temp;
} 

This is the output:
line1

Can anyone please explain the reason of such occurrence ?

I would edit your code, but it is full of HTML and I don't have 10 minutes to get rid of it.  To format code simply indent it by four spaces.

Your code doesn't compile.  You need #include <stdio.h> for printf, and #include <stddef.h> for NULL, and you're missing a semicolon on the struct definition.  It's almost always best to copy-and-paste your actual code.

@Keith: actually <stdio.h> also defines the NULL macro.

@KeithThompson: I generally agree, but for this sample I think we can safely assume that anyone answering the question would be familiar with printf and NULL.

auto variables (i.e., local variables not declared static) such as p and temp are left uninitialized, so their value is indeterminate (essentially, whatever bit string is left in that particular memory cell from a previous operation, which may or may not be a valid value for the given type).  Never try to dereference an uninitialized pointer.
Variables declared at file scope (outside of any function block) or with the static keyword are initialized as follows:

pointers are initialized to NULL;
arithmetic types (integer or floating point) are initialized to 0
structs are initialized recursively according to the previous two rules
unions have their first named member initialized recursively according to the first two rules

If you change the declaration of p to
static struct node *p;

then p would be initialized to NULL.  If you don't want to declare p as static, then you'll have to initialize it as part of the declaration:
struct node *p = NULL;


You declare a pointer, but you do not initialize it.  It could take on any value, i.e., it is not guaranteed to be NULL.  Of course, it could be NULL (0), but again, that cannot be counted upon.  
The value of an uninitialized variable is indeterminate unless it has static storage duration.

You are reading an uninitialized variable. That is undefined behaviour. Basically, anything can happen. If you turned on your compiler warnings, the compiler would have told you exactly that.
I suspect you believe that your local variables will be initialized automatically. That is not the case. You must initialize them before reading them.

I don't see any UB here.  He is not dereferencing the pointer, simply reading the (uninitialized) value of the pointer variable itself, which is not UB.

@EdS. See item 10 here: https://www.securecoding.cert.org/confluence/display/seccode/CC.+Undefined+Behavior#CC.UndefinedBehavior-ub10

@EdS. The standard says this: "If
the lvalue designates an object of automatic storage duration that could have been
declared with the register storage class (never had its address taken), and that object
is uninitialized (not declared with an initializer and no assignment to it has been
performed prior to use), the behavior is undefined."

Oh wow, I didn't know that, thanks.  I suppose that I was speaking from practical experience and not the words of the standard.  I knew that the standard does define its value to be indeterminate, I just had no idea that reading it was technically UB.  Thanks again, learned something new +1.

You need to give the p amd temp values - as you have not they can contain anything.
