The Difference Between NULL and Zero

University professors will try to drive the point into your head all semester: NULL and zero are not the same thing! But yet, you’ve looked into memory, you’ve examined the code. That thing they call NULL sure does look like a zero! What’s the big difference?

Straight up, NULL is a pointer. It’s a constant, made available through stdio.h.

Whenever you see the NULL constant used, it’s as a pointer value. It can also be used to typecast pointers, such as (int *)NULL. And keep in mind the pointer mantra:

A pointer is a variable that holds a memory location.

Zero is a value. It’s not a memory location, as only pointers can hold memory locations. Further, pointers hold memory locations that reference something. It’s unwise (and often prohibited) to stuff a pointer with an immediate value just because you want to peek somewhere into memory.

Perhaps the element that causes the most confusion is the \0 escape character. That’s the null character, which is used in C to terminate a string of text. The problem is that \0 translates into character value zero. In fact, you can use a zero directly (if you know how) and it has the same effect. But that doesn’t mean that zero and NULL are the same thing, especially given that NULL is one use of the word and “null character” is another.

The following code should confuse you further, if you aren’t confused enough already:

#include <stdio.h>

int main()
{
    printf("NULL is %p\n",(int *)NULL);
    printf("\\0 is %d\n",'\0');
    printf("Size of int = %ld\n",sizeof(int));
    printf("Size of NULL = %ld\n",sizeof(NULL));

    return(0);
}

This code attempts to display the value of NULL. As an address, the first line output gives its address:

NULL is 0x0

The NULL pointer holds address zero, which may cause you to flare your nostrils, point at the screen, and shout, “I told you!” But it’s still not a zero in the “Isn’t NULL equal to zero?” sense.

The second line demonstrates how the \0 escape character is really value zero:

\0 is 0

Keep in mind that no one is offended when you use a zero value to terminate a string; the \0 escape sequence simply allows you to specify it within a set of quotes. That may be required, for example, when a function demands a char value as input and not an int.

Finally, to prove each item is of the appropriate type, the code outputs these lines:

Size of int = 4
Size of NULL = 8

The int value \0 is 4 bytes long (on my computer), which helps confirm it’s an int. But the NULL value is 8 bytes long, which is the size of an address.

Even when all this still confuses you, just drive it into your head: NULL isn’t used as a placeholder for zero. In the C language, which features a memory location variable type (the pointer), it’s necessary to have NULL to represent an empty memory location. That’s the purpose of the NULL constant.

2 thoughts on “The Difference Between NULL and Zero

  1. I see that this blog goes back only up to April 13, 2013. Were there older posts that are lost forever? Because if there are, I have to save these lessons. They are gold to someone like me.

  2. April 13 is when I started the blog, so you’re not missing anything.

    Thanks for the supportive comment, Zhukov!

Leave a Reply