Are NUL and NULL interchangeable in C?

Just read the index to the book of K. Kenneth Reck. It says that the NUL byte is the one whose bits are all 0, written like this '\0' , and NULL has a value of 0, which refers to a pointer whose value is zero.

Both are integers and have the same meaning, so they can be used interchangeably.

and this code also:

 char const *keyword[] = { "do", "for", "if", "register", "return", "switch", "while", NULL }; 

In this code, NULL allows search functions in a table to determine the end of a table. So, can we exchange NULL and NUL macros?

+10
c ++ c
Mar 18 '13 at 14:32
source share
2 answers

NUL is the designation for the ASCII 0 character code.

NULL is the macro defined in stddef for the null pointer.

So, no, they should not be used interchangeably.

+15
Mar 18 '13 at 14:34
source share

There is no standardized NUL macro, at least not in C.

In addition, NULL takes precedence over plain literal 0 , which tells the reader that β€œhey, we're dealing with a pointer,” which can be considered an advantage.

In C ++, the idiom simply wrote 0 for a long time, but then they seemed to change it and introduce for some reason a new literal nullptr .

In C, I recommend writing NULL for pointers and '\0' for characters.

+12
Mar 18 '13 at 14:34
source share



All Articles