What is the difference between "NULL", "0" and 0?

Asked

Viewed 13,387 times

20

Both are worth zero. I can use the 3 interchangeably always?

2 answers

20


All 3 are constant literals worth 0, that’s correct.

0

0 is a numerical value and can be used whenever you need the same zero number. In some places it ends up being used for other things. Where a boolean result is expected 0 is considered false, while any other value is considered true.

\0

This is the null character. Compilers often define a macro called NUL with this value, which may even have more than one byte.

It really doesn’t stop being a 0 and it can usually be used where one expects a zero. The slider is used as an escape to define that you want the zero byte there and not the character 0, which is something quite different from the character 0 (is byte 48). So this form is only used within a literal character or string. This character is the terminator of string. This is important since C doesn’t keep the size of string standardized.

In the case of char it is possible to use only the 0 even, as long as you do not use single quotes *not all compilers in some linked options accept). Remember that type char is a numeric type like any other. Actually it can be used to print characters, but it’s just a different representation. Computers only understand numbers, the rest is representation.

NULL

This is just one #define with a zero value. In fact there are some implementations that the value is 0 same. But it is more common the value (void *)0, making it clear that this is actually a pointer to the memory’s zero address, so a null pointer.

A 0 is not that interchangeable. If you try to compare a NULL, that is to say void * 0 even works because it makes a cast automatic. But if you try to compare a NULL with a whole will not happen what you expect. In fact it is generated a Warning and it’s possible that the compiler won’t even let it finish.

A code demonstrating all this:

#include <stdio.h>

int main(void) {
    int i = 0;
    char c = 0;
    char *s = "teste";
    void *p = NULL;
    printf("%d %c %s %p\n", i, c, s, p);
    if (!c) printf("i ok\n"); //verifica se não é zero
    if (!c) printf("c ok\n"); //verifica se não é um caractere nulo
    if (!s[5]) printf("s ok\n"); //verifica se o 6o. caractere é o terminador
    if (!p) printf("p ok\n"); //verifica se o ponteiro não é nulo
//  if (p == i) printf("p NULL ok\n"); //tipos incompatíveis sendo comparados
    printf("%zu", sizeof('x'));
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

11

I wanted to make an addendum to @Maniero’s reply.

The Standard C does not require that NULL is set with all bits to 0.

§7.20.3.2

...

2 The calloc Function allocates space for an array of nmemb Objects, each of Whose size is size. The space is initialized to all bits zero.255)

...

255) Note that this need not be the same as the representation of floating-point zero or a null Pointer Constant.

Translation:

§7.20.3.2

...

2 The calloc function allocates space to an array of nmemb objects, each with size size. Space is initialized with bits to 0.255)

...

255) Note that this does not have to be the same representation of 0 in floating representation, or the null pointer constant.

  • 2

    Very good complement. I always wait for answers like this or even complete so I can accept.

  • Really good.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.