if statement - Why am I getting this output in C++? Explain the logic -


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

the output above question is:

world

kindly explain me why getting output. not able satisfactory answer after referring several different sources.

null results in false condition. imagine null 0, this:

if(null) 

would equivalent this:

if(0) 

thus code become:

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

where obvious because 0 results false, if condition not satisfied, body of if-statement not executed. result, body of else-statement executed, explains output see.


ps: correct way of defining null , null_pointer?


Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -