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

11

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

    
asked by anonymous 16.01.2017 / 11:51

2 answers

11

All 3 are constant literals that are worth 0, this is correct.

0

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

NUL

This is the null character. Compilers usually define a macro called 0 with this value, which can even have more than one byte.

It really does not stop being a 0 and it can usually be used where a zero is expected. The slash is used as an escape to define that you want the zero value byte there and not the char character, which is something quite different from the zero value (it's byte 48). So this form is only used inside a literal character or string . This character is the string terminator. This is important since C does not save the string size in a standard way.

For 0 you can only use char itself, as long as you do not use single quotation marks. Remember that% type of% is a numeric type like any other type. By chance 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 a #define with a zeroed 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 zero memory address, therefore a null pointer.

A 0 is not as interchangeable as this. If you try to compare a NULL , that is void * with 0 until it works because it does an cast auto. But if you try to compare a NULL with an integer, what you expect will not happen. Actually a warning is generated and the compiler may not 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
}

See running on ideone . And at Coding Ground . Also put it on GitHub for future reference .

    
16.01.2017 / 11:51
7

I wanted to make an addition to the @bigown response.

Standard C does not require that NULL be set with the bits all 0.

  

§7.20.3.2

     

...

     

2 The calloc function allocates space for an array of nmemb objects, each of which size is size . The space is initialized to all zero bits. 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 for an array of nmemb objects, each with size . The 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.

    
16.01.2017 / 12:41