473,395 Members | 1,987 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Difference between null and zero

44
Hello.

Can I have an elaboration on the difference between null and zero.

For example, we say we set a byte to null (using bzero() to null), how different is it from setting the bytes to zero?

Also, I'd love to be directed to some source which is explicit about the difference. I googled but, found nothing good enough.

Thanks :)
Hanaa
Apr 21 '09 #1
5 5641
JosAH
11,448 Expert 8TB
The C Standard garantees that the numerical value 0 (zero) in a pointer context is converted to a NULL value. Bitwise speaking this NULL value can be interpreted as any numerical value but the C Standard garantees that the appropriate conversion takes place.

This NULL pointer value always compares to a 'false' value in a boolean context (such as in an if-expression). You shouldn't care about the actual, internal, bit values of a NULL pointer, i.e. you can make one by using a numerical value 0 as in:

Expand|Select|Wrap|Line Numbers
  1. char* p= 0; // a NULL pointer;
  2. char* q= NULL; // also a NULL pointer.
  3.  
A NULL pointer is garanteed to point to nothing, i.e. no object. In 'normal' hardware a NULL pointer has all bits zero but it could have been 0xdeadbeef or any other value as well.

kind regards,

Jos
Apr 21 '09 #2
donbock
2,426 Expert 2GB
The term 'null' is overloaded in C. JosAH described what is meant by a 'null pointer'. Here are a few other usages of the word in C. For all I know, there could be additional usages in C++.

1. The null character (used at the end of a null-terminated string).
The null character is of type char and it must be encoded as the value 0. Notice that this must be true of whatever character encoding system (ASCII, EBCDIC, etc.) is used by any and all C compiler implementations. The proper way to indicate this value in your software is as character constant '\0'. The null character always compares to a 'false' value in a boolean context (such as in an if-expression). Notice that in the ASCII character encoding, the name of the null character is "NUL".

2. The null wide character (used at the end of a wide string).
The null wide character is of type wchar_t and it must be encoded as the value 0. The null wide character always compares to a 'false' value in a boolean context (such as in an if-expression).

3. The null statement.
The null statement consists simply of a semicolon. The null statement is used primarily in two situations. In the first case, a null body is used as the body of an iterative statement (while, do, or for). The second case is where a label is desired just before the right brace that terminates a compound statement. A label cannot simply precede the right brace, but must always be attached to a statement.

Regarding null pointers, pedantically portable C code is careful to treat data pointers differently than function pointers. The C Standard permits an implementation to use different size pointers for each. The NULL macro has data pointer type void*. Therefore, you should not compare function pointer variables to NULL; instead compare against a literal "0". Unfortunately, Standard C doesn't provide a generic null function pointer macro. My personal practice is to provide macro FNULL (Function pointer NULL) with value "0".
Apr 21 '09 #3
hanaa
44
@donbock
What does "encoding as 0" mean? Does it mean that the 0 is stored in the memory, in the place of the character that we are setting to null?
Apr 21 '09 #4
donbock
2,426 Expert 2GB
@hanaa
For instance, if your compiler implementation uses the ASCII encoding standard for characters, then character constant 'A' is encoded [stored] as 0x41. On the other hand, if your compiler implementation uses the EBCDIC encoding standard for characters, then character constant 'A' is encoded [stored] as 0xC1.

My point was that the C Standard requires that encoded value 0 be reserved for the null character in all encoding standards. An encoding standard that requires 0 to mean something else would be unsuitable for use by any C compiler implementation.
Apr 21 '09 #5
hanaa
44
Got it. Thanks. :-)
Apr 21 '09 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Paul T. Rong | last post by:
Do "" and Null both mean nothing?¡¡ If I don't type anything in text box, the its value is Null£¿¡¡Or it is ¡°¡±£¿ I don¡¯ think they are the same, but I don¡¯t know their difference. Thanks.
24
by: RHNewBie | last post by:
Hello, What is the difference between null and NULL. Are x == null and x == NULL the same? The compiler is gcc 3.2. Thanks.
102
by: junky_fellow | last post by:
Can 0x0 be a valid virtual address in the address space of an application ? If it is valid, then the location pointed by a NULL pointer is also valid and application should not receive "SIGSEGV"...
39
by: August Karlstrom | last post by:
Hello all, If c is a char then is there any difference between c = '\0' and c = 0
3
by: Asha | last post by:
greetings, i have some questions below, what are the differences between private string _strVal = string.Empty; and _strVal = null; does the string.Empty; allocate memory for it? how about...
8
by: venkatesh | last post by:
hai to everybody, i am having doubt in difference between the malloc and calloc function. please tell where to use and when to use those functions?
10
by: Sek | last post by:
I have a property that returns a ArrayList object. On failure condition, is it right to return null or return an ArrayList object with zero elements?
45
by: anto frank | last post by:
hi friends, is ther any difference in array in c and array in c++?
8
by: lak | last post by:
What is the difference b/w '\0' and NULL? In which case It is useful?
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.