472,784 Members | 962 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

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 5098
In article
<fd**********************************@d27g2000prf. googlegroups.com>,
lak <la********@gmail.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*********@gmail.comwrote:
lak <la********@gmail.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*********@gmail.comwrote:
lak <la********@gmail.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*********@gmail.comwrote:
>>lak <la********@gmail.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
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...
5
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...
8
by: Paulo Jan | last post by:
Hi all: I have here a table with the following schema: Table "todocinetv" Column | Type | Modifiers...
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.
2
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...
2
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...
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
6
by: lenygold via DBMonster.com | last post by:
Here is my input table: TUE MON ----------- ----------- 2 - - 25 27 - - 48 50 - - 78
5
by: iceman19860106 | last post by:
hello everyone! what is the difference between NULL and 0 in C language?
0
by: Rina0 | last post by:
Cybersecurity engineering is a specialized field that focuses on the design, development, and implementation of systems, processes, and technologies that protect against cyber threats and...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.