In C programming, NULL is a special macro that represents a null pointer constant. It is used to indicate that a pointer does not point to any valid memory address or object. Understanding NULL is essential for preventing crashes and managing memory safely.
NULL is typically defined as (void *)0 in standard libraries like <stdio.h> or <stddef.h>. When you assign NULL to a pointer, you are explicitly stating that the pointer is currently "empty" or "unassigned."
\0).It is a best practice to initialize pointers to NULL immediately if you don't have a valid address to give them yet.
While they look similar, they have different meanings in C:
| Term | Description |
|---|---|
| NULL | A macro used for pointers to indicate they point to nothing. |
| NUL (\0) | The character constant used to terminate strings. |
| 0 | An integer constant. In pointer contexts, it is equivalent to NULL. |
Trying to access the value of a NULL pointer is a critical error. This will almost always result in a Runtime Crash (Segmentation Fault).
NULL is frequently used as a return value by functions to indicate failure, especially in memory allocation or file handling.
malloc() without checking if it is NULL.if (ptr != NULL) { ... }. This simple habit will save you from 90% of pointer-related crashes in C!