|
Different Ways to Write Zero in CLast revised: Sunday January 19, 2003 05:50.Please be clear on the different ways of saying "zero" in C: 0 is a C integer with the value 0 '\0' is a C char const containing the null character, value 0 NULL is a define for the null pointer, value 0 FALSE is a define or enum Boolean, value 0 All these things are zero. (NULL and FALSE must be defined by you; the other two are native to C.) I'll also mention that the official ASCII 3-letter name for the character '\0' is NUL (not NULL), so you might prefer to use a #define to make NUL equivalent to '\0' and use NUL in your code instead of '\0'. Which one of these zeroes you use should depend on the context in your code where you use it. Don't confuse the zeroes and the contexts. Given these declarations: typedef enum { FALSE=0, TRUE } Boolean; #define NULL 0 /* usually done in <stdio.h> */ int i; char *ptr; Boolean truth; char ch; This is correct and easy to read: i = 0; ptr = NULL; truth = FALSE; ch = '\0'; The following generates exactly the same code (the code itself is "correct"); but, writing code like this is misleading and bad style: i = NULL; /* BAD: i is an integer, not a pointer */ ptr = '\0'; /* BAD: ptr is a pointer, not a char */ ch = FALSE; /* BAD: ch is a char, not a Boolean */ truth = 0; /* BAD: truth is a Boolean, not an int */ The compiler does not care about which form of zero you use. Use the form of zero that tells the human reader of your program what type of data you are using. Do not mislead your readers by using the wrong form of zero in the wrong program context. Last revised: Sunday January 19, 2003 05:50. |
Web Author: Ian! D. Allen idallen@idallen.ca Updated: 2003-01-19 05:50 Support free and non-commercial Internet. This site works best in Any Browser, a campaign for non-specific WWW. This work is licensed under a Creative Commons License. |