Tuesday 8 November 2011

What is the difference between NULL and NUL? in C programming

What is the difference between NULL and NUL?

NULL is a macro defined in <stddef.h> for the null pointer.
NUL is the name of the first character in the ASCII character set. It corresponds to a zero value. There’s no
standard macro NUL in C, but some people like to define it.

NOTE
The digit 0 corresponds to a value of 80, decimal. Don’t confuse the digit 0 with the value of
‘\0’ (NUL)!
NULL can be defined as ((void*)0), NUL as ‘\0’. Both can also be defined simply as 0. If they’re defined that
way, they can be used interchangeably. That’s a bad way to write C code. One is meant to be used as a pointer;
the other, as a character. If you write your code so that the difference is obvious, the next person who has
to read and change your code will have an easier job. If you write obscurely, the next person might have
problems. Hint: Typically, the “next person” is the person who originally wrote the code. The time you save
might be your own.

Cross Reference:

VII.3: What is a null pointer?

No comments:

Post a Comment