Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you deal with NUL?

Tags:

c++

c

From time to time, I run into communications issue with other programmers, when we talk about NULL. Now NULL could be

a NULL pointer
the NUL character
an empty data element in some sort of database.


NUL seems to be the most confusing. It is the ASCII character 0x00.
I tend to use '\0' in my code to represent it. Some developers in my group
tend to prefer to simply use 0, and let the compiler implicitly cast it to a char.


What do you prefer to use for NUL? and why?

like image 974
EvilTeach Avatar asked Oct 21 '08 00:10

EvilTeach


People also ask

How is null treated in order by?

Similarly to SQLite, MySQL treats NULL values as lower than any non-NULL value; thus, by default, it puts these values first when sorting in ascending order and last when sorting in descending order.

How do I stop null checks?

One way of avoiding returning null is using the Null Object pattern. Basically you return a special case object that implements the expected interface. Instead of returning null you can implement some kind of default behavior for the object. Returning a null object can be considered as returning a neutral value.

How do you handle null in Python?

Python uses the keyword None to define null objects and variables. While None does serve some of the same purposes as null in other languages, it's another beast entirely. As the null in Python, None is not defined to be 0 or any other value. In Python, None is an object and a first-class citizen!


2 Answers

I use '\0' for the nul-character and NULL for pointers because it is clearest in both cases.

BTW, both 0 and '\0' are ints in C and either one will be converted to char when stored in a char variable.

like image 183
Robert Gamble Avatar answered Oct 15 '22 19:10

Robert Gamble


I like the pre-defined NULL macro, as it preserves the semantic meaning, rather than some other use of the number 0.

like image 43
Matt J Avatar answered Oct 15 '22 19:10

Matt J