473,657 Members | 2,832 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Difference between '\0' and NULL

lak
What is the difference b/w '\0' and NULL?
In which case It is useful?
Nov 26 '07 #1
8 5178
In article
<fd************ *************** *******@d27g200 0prf.googlegrou ps.com>,
lak <la********@gma il.comwrote on Monday 26 Nov 2007 2:40 pm:
What is the difference b/w '\0' and NULL?
The first is the representation for the null character, i.e., a
character with value zero. It is used as a string terminator in C.
It is actually an "escape sequence" with an octal zero.

The second is a macro that resolves to a null pointer value. In C source
code a literal zero is also converted into a null pointer constant when
it occurs in a pointer context.

The first expression is of type int while the second is of a pointer
type.
In which case It is useful?
Use '\0' to terminate strings and NULL to initialise pointers and set
them to a "safe" value after they have been used.

Please browse the comp.lang.c FAQ:

<http://c-faq.com/>

Nov 26 '07 #2
santosh <sa*********@gm ail.comwrote:
lak <la********@gma il.comwrote on Monday 26 Nov 2007 2:40 pm:
What is the difference b/w '\0' and NULL?

The first is the representation for the null character, i.e., a
character with value zero. It is used as a string terminator in C.
It is actually an "escape sequence" with an octal zero.

The second is a macro that resolves to a null pointer value. In C source
code a literal zero is also converted into a null pointer constant when
it occurs in a pointer context.

The first expression is of type int while the second is of a pointer
type.
Subtly wrong. NULL is a null pointer constant, which is either an
integer constant with value zero, or such a constant cast to void *. In
the second case it has pointer type, but in the first case it has
integer type. In fact, confusingly, '\0' is a legal (but unwise)
spelling for a null pointer constant.

Richard
Nov 26 '07 #3
Richard Bos wrote:
>
santosh <sa*********@gm ail.comwrote:
lak <la********@gma il.comwrote on Monday 26 Nov 2007 2:40 pm:
What is the difference b/w '\0' and NULL?
The first is the representation for the null character, i.e., a
character with value zero. It is used as a string terminator in C.
It is actually an "escape sequence" with an octal zero.

The second is a macro that resolves to a null pointer value. In C source
code a literal zero is also converted into a null pointer constant when
it occurs in a pointer context.

The first expression is of type int while the second is of a pointer
type.

Subtly wrong. NULL is a null pointer constant, which is either an
integer constant with value zero, or such a constant cast to void *. In
the second case it has pointer type, but in the first case it has
integer type. In fact, confusingly, '\0' is a legal (but unwise)
spelling for a null pointer constant.
While this is valid:

void *ptr = '\0';

the following will most likely fail (or at least give a warning):

int i = NULL;

Yes, the standard allows NULL to be an "integer constant with value
zero" (such as "#define NULL 0"), but it is more likely defined as
a value cast to void * (as in "#define NULL ((void *)0)"), simply
because it's "safer". (In fact, it's probably required in an
implementation where int and void* are represented differently.
Consider the consequences of passing VOID to a varadic function.)

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer .h|
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:Th***** ********@gmail. com>

Nov 26 '07 #4
Kenneth Brody wrote, On 26/11/07 18:19:
Richard Bos wrote:
>santosh <sa*********@gm ail.comwrote:
>>lak <la********@gma il.comwrote on Monday 26 Nov 2007 2:40 pm:

What is the difference b/w '\0' and NULL?
The first is the representation for the null character, i.e., a
character with value zero. It is used as a string terminator in C.
It is actually an "escape sequence" with an octal zero.

The second is a macro that resolves to a null pointer value. In C source
code a literal zero is also converted into a null pointer constant when
it occurs in a pointer context.

The first expression is of type int while the second is of a pointer
type.
Subtly wrong. NULL is a null pointer constant, which is either an
integer constant with value zero, or such a constant cast to void *. In
the second case it has pointer type, but in the first case it has
integer type. In fact, confusingly, '\0' is a legal (but unwise)
spelling for a null pointer constant.

While this is valid:

void *ptr = '\0';

the following will most likely fail (or at least give a warning):

int i = NULL;

Yes, the standard allows NULL to be an "integer constant with value
zero" (such as "#define NULL 0"), but it is more likely defined as
a value cast to void * (as in "#define NULL ((void *)0)"), simply
because it's "safer".
It definitely makes sense for this to be done, although I'm sure one of
the implementations I've used in the past few years did not do it.
(In fact, it's probably required in an
implementation where int and void* are represented differently.
No, this is definitely not true. A conforming implementation can use
"#define NULL 0" irrespective of the relative sizes and representations .
Consider the consequences of passing VOID to a varadic function.)
Assuming you mean passing a null pointer to a varidac function, yes that
is a problem and one of the few instances where you need a cast.
--
Flash Gordon
Nov 26 '07 #5
On Mon, 26 Nov 2007 13:19:34 -0500, Kenneth Brody wrote:
(In fact, it's probably required in an implementation
where int and void* are represented differently.
It's not.
Consider the
consequences of passing VOID to a varadic function.)
Passing NULL to a variadic function is a bad idea, not only for that
reason, but also because the function probably expects a specific pointer
type other than void *.
Nov 26 '07 #6
Flash Gordon wrote:
>
Kenneth Brody wrote, On 26/11/07 18:19:
[...]
Yes, the standard allows NULL to be an "integer constant with value
zero" (such as "#define NULL 0"), but it is more likely defined as
a value cast to void * (as in "#define NULL ((void *)0)"), simply
because it's "safer".

It definitely makes sense for this to be done, although I'm sure one of
the implementations I've used in the past few years did not do it.
(In fact, it's probably required in an
implementation where int and void* are represented differently.

No, this is definitely not true. A conforming implementation can use
"#define NULL 0" irrespective of the relative sizes and representations .
Consider the consequences of passing VOID to a varadic function.)
D'oh... s/VOID/NULL/
Assuming you mean passing a null pointer to a varidac function, yes that
is a problem and one of the few instances where you need a cast.
Would you really need to pass "(void *)NULL"? Ewww.

Yes, the standard as-written probably requires it. I wonder if they
meant that to be the case?

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer .h|
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:Th***** ********@gmail. com>
Nov 26 '07 #7
Kenneth Brody wrote, On 26/11/07 20:40:
Flash Gordon wrote:
>Kenneth Brody wrote, On 26/11/07 18:19:
<snip possible NULL definitions>
>Assuming you mean passing a null pointer to a varidac function, yes that
is a problem and one of the few instances where you need a cast.

Would you really need to pass "(void *)NULL"? Ewww.

Yes, the standard as-written probably requires it. I wonder if they
meant that to be the case?
I might pass (void*)0 instead. I know someone who provides a macro
called NP, but the code in question pre-dates when you could rely on
having an ANSI C compiler.
--
Flash Gordon
Nov 26 '07 #8
Kenneth Brody wrote:
Would you really need to pass "(void *)NULL"? Ewww.
cf. http://c-faq.com/~scs/cgi-bin/faqcat.cgi?sec=null

specifically http://c-faq.com/null/nullreq.html
Nov 27 '07 #9

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

Similar topics

0
2449
by: Doug Reese | last post by:
hello, i have what seems to me a very common operation i'm performing. i need to find the balance on an invoice. i was not having any problems until the production server was upgraded to mysql v4.0.13-standard for pc-linux. there must be a better way to query for this information than the method i'm using, since the result with v4.0 is not what i expected, nor what i received with v3.23. i'm including sample data and queries with my...
5
3528
by: Neil Rutherford | last post by:
During testing of an application, i noticed a difference between SQL 2000 and SQL 7, both with identical config. In a nutshell: A table has a trigger for UPDATE and DELETE. When a column in the table is UPDATED the following happens: In autocommit mode, when entering a trigger the trancount equals 1 for both SQL 7 and 2000.
8
2265
by: Paulo Jan | last post by:
Hi all: I have here a table with the following schema: Table "todocinetv" Column | Type | Modifiers -------------+-----------------------------+---------------------- id | integer | not null default '0' datestamp | timestamp without time zone | not null thread | integer | not null default '0'
3
9567
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
20038
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.
2
4075
by: Kevin B Ebert | last post by:
Today, I ran into something I didn't quite expect. I'm sure there is a logical explanation for it so I want to post it and see if anyone can explain the difference to me. I came across a situation when I wanted to see if a public variable inside a struct was null or not. Sample code below: namespace SampleConsole { /// <summary>
2
1771
by: Adam Clauss | last post by:
Basically, my question is in terms of performance and the garbage collector - Is there any difference between a) letting a variable simply go out of scope b) explicity setting it to null once I am done with it I have no particular reason to think that b) would gain me anything, but I thought maybe it would be a sure sign to the garbage collector that Yes, you can go ahead and remove the object. Thanks!
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
6
3413
by: lenygold via DBMonster.com | last post by:
Here is my input table: TUE MON ----------- ----------- 2 - - 25 27 - - 48 50 - - 78
5
6900
by: iceman19860106 | last post by:
hello everyone! what is the difference between NULL and 0 in C language?
0
8385
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
8303
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8821
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
8602
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6162
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
5632
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4300
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1941
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1601
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.