473,473 Members | 1,757 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

what is the difference between NULL and 0

hello everyone!
what is the difference between NULL and 0 in C language?
Oct 29 '08 #1
5 6885
iceman19860106 said:
hello everyone!
what is the difference between NULL and 0 in C language?
NULL is a macro defined in <stdio.h>

It represents a null pointer constant, and may be defined either as 0 or as
((void *)0), which gives you a big hint that there's precious little
difference between them in semantic terms. The most important difference
is that 0 can definitely be used in contexts where an int is required,
whereas the possibility that NULL may be defined as ((void *)0) deters the
wary from using it in such contexts.

Thus, given this:

T *p = malloc(sizeof *p);

these are equivalent:

if(p != NULL)

and

if(p != 0)

They do the same thing with the same meaning.

But, given this:

int x;

these are not equivalent, or at least might not be:

x = 0;

and

x = NULL; /* if NULL is ((void *)0), which it can be,
this will cause a diagnostic message
*/

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Oct 29 '08 #2
iceman19860106 wrote:
hello everyone!
what is the difference between NULL and 0 in C language?
This is a FAQ. Read section 5.

--
Mark McIntyre

CLC FAQ <http://c-faq.com/>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>
Oct 29 '08 #3
iceman19860106 wrote:
>
what is the difference between NULL and 0 in C language?
NULL is a macro defining a pointer. The pointer points nowhere,
and thus cannot be dereferenced.

0 is a number. It represents an integer one less than 1. It is
usable in all integer expressions, except as a divisor.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.
Oct 30 '08 #4
On Wed, 29 Oct 2008 19:29:29 -0500, CBFalconer <cb********@yahoo.com>
wrote in comp.lang.c:
iceman19860106 wrote:

what is the difference between NULL and 0 in C language?

NULL is a macro defining a pointer. The pointer points nowhere,
and thus cannot be dereferenced.
The statement above is sloppy to the point of being incorrect, Chuck.

NULL is a macro that represents a null pointer constant. It most
certainly NOT a pointer, it is a value that can be assigned to a
pointer object or compared against the value of a pointer object.

NULL in a source file is no more a pointer than a numeric literal '0'
is an int.
0 is a number. It represents an integer one less than 1. It is
usable in all integer expressions, except as a divisor.
Still sloppy, but close enough.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
Oct 30 '08 #5
On 30 Oct, 02:03, Jack Klein <jackkl...@spamcop.netwrote:
On Wed, 29 Oct 2008 19:29:29 -0500, CBFalconer <cbfalco...@yahoo.com>
<snip>
0 is a number. *It represents an integer one less than 1. *It is
usable in all integer expressions, except as a divisor.

Still sloppy, but close enough.
zero is a number equal to the cardinality of the empty set?
--
Nick Keighley
Oct 30 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

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.
44
by: MLH | last post by:
You have a table - tblOwners. Two of its fields are strOwnrFName and strOwnrLName. Would you make the default value "" (ZLS) most of the time?
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.
13
by: Jason Huang | last post by:
Hi, Would someone explain the following coding more detail for me? What's the ( ) for? CurrentText = (TextBox)e.Item.Cells.Controls; Thanks. Jason
4
by: Rachel Suddeth | last post by:
What is the difference between a managed/unmanaged resource, and how do you tell which is which? I'm trying to understand how to write some Dispose() methods, and we are supposed to put code that...
16
by: Abhishek | last post by:
why do I see that in most C programs, pointers in functions are accepted as: int func(int i,(void *)p) where p is a pointer or an address which is passed from the place where it is called. what...
4
by: Shwetabh | last post by:
Hi, My question is, is there any difference between a NULL and a Blank (Unknown, Not Applicable) field in MS SQL or are they the same? Awaiting your comments, Regards
2
by: Carl Johansson | last post by:
Except for the syntax, what is the difference, if any, between this... /* ---------------------------------------------------------------------- */ private void Form1_Paint(object sender,...
6
by: lenygold via DBMonster.com | last post by:
Here is my input table: TUE MON ----------- ----------- 2 - - 25 27 - - 48 50 - - 78
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,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.