473,588 Members | 2,474 Online
Bytes | Software Development & Data Engineering Community
+ 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 6893
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********@yah oo.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.l earn.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...@spam cop.netwrote:
On Wed, 29 Oct 2008 19:29:29 -0500, CBFalconer <cbfalco...@yah oo.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
9566
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
1844
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
20024
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
5029
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
39974
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 deals with managed in one place, and code that deals with unmanaged in another place, but I can't seem to find anything that clearly explains what that means. I think if I used a Windows API function to optain a handle, that handle would be an...
16
10364
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 do you mean by pointing to a void and why is it done? Aso, what happens when we typecast a pointer to another type. say for example int *i=(char *)p; under different situations? I am kind of confused..can anybody clear this confusion by clearly...
4
32733
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
1375
by: Carl Johansson | last post by:
Except for the syntax, what is the difference, if any, between this... /* ---------------------------------------------------------------------- */ private void Form1_Paint(object sender, PaintEventArgs e) { Pen pen = null; try { pen = new Pen(Color.MidnightBlue, 1.0F);
6
3412
by: lenygold via DBMonster.com | last post by:
Here is my input table: TUE MON ----------- ----------- 2 - - 25 27 - - 48 50 - - 78
0
7929
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8228
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8357
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
7987
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
5729
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
3887
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2372
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 we have to send another system
1
1459
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1196
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.