Short answer
Use NULL for pointers in C, and nullptr in C++11 and later. Both NULL and 0 can give a pointer a null value, but NULL shows the reader that a pointer is involved. A plain 0 looks like a number, and it can cause trouble in some calls.
Both can make a null pointer
In C, these two lines set the same null pointer value:
int *p = 0;
int *q = NULL;
The compiler accepts both. The difference is in what the code tells a reader. NULL marks the value as a pointer, while 0 looks like an integer, so the reader has to work out what it is for.
Readers often ask, is 0 == null in C? Yes, and the two compare equal. That means the choice is about intent and safety, not correctness.
Variadic functions in C
Functions such as printf and execl take a variable number of arguments. The compiler does not know the type of each extra argument, so the call passes the value as written. A plain 0 is an int. Pointers are often wider than int on modern systems, so the called function may read more bytes than the caller passed.
Casting the null value to a pointer type avoids this:
#include <stdio.h>
#include <unistd.h>
int main(void) {
execl("/bin/ls", "ls", "-l", (char *)NULL);
perror("execl");
return 1;
}
On many C systems, NULL is defined as ((void *)0), which already has a pointer type. The cast makes the type explicit even when NULL is a plain 0.
Overloads in C++
In C++, a plain 0 is an int, so it can select an integer overload when you meant a pointer:
void log_message(int code);
void log_message(const char* text);
log_message(0); // Calls log_message(int), not the pointer version.
NULL can behave like 0 here, depending on how the compiler defines it. nullptr always selects the pointer version, which is one of the main reasons C++11 added it.
Readability and review
NULL makes pointer code easier to read. Reviewers can see at a glance that a value is meant for a pointer. Tools can also use that signal. For example, a static analysis tool can flag a 0 that is being used as a pointer.
When 0 is correct
Use 0 when you mean the integer zero, such as a loop counter or a return value of zero. Reserve NULL in C and nullptr in C++ for pointers.
Summary
For pointers, use NULL in C and nullptr in modern C++. Pass a null value to a variadic function with a pointer cast. Keep 0 for integers.