[Solved] Why am I getting this output in C++? Explain the logic


NULL results in a false condition. You could imagine that NULL is a 0, so this:

if(NULL)

would be equivalent to this:

if(0)

thus your code would become:

#include <stdio.h>
#include <iostream>
int main() 
{
  if(0)
    std::cout<<"hello";
  else
    std::cout<<"world";
  return 0;
}

where is obvious that because 0 results to false, the if condition is not satisfied, thus the body of the if-statement is not executed. As a result, the body of the else-statement is executed, which explains the output you see.


PS: Correct way of defining NULL and NULL_POINTER?

solved Why am I getting this output in C++? Explain the logic