Search code examples
cnullzero

How is NULL different from 0 in C?


If these 3 lines are the only time NULL is mentioned in stdio.h, how is NULL different from 0 and most importantly what tells the compiler NULL is actually an invalid address? Talking about C, maybe in other libraries, like in iostream, NULL is defined differently.

0085 #ifndef NULL
0086 #define NULL 0
0087 #endif

Solution

  • To the compiler, NULL is indistinguishable from 0 when used as a pointer. The standard actually defines 0 to be a null pointer value, and the standard library just introduces a convenient macro NULL defined to a null pointer value (usually 0 or ((void*)0)), so that you can use it in code for better readability and expressing intent. But there's nothing special about NULL itself; it's the 0 that is relevant.