f



null and NULL: is there any difference?

Hello,
What is the difference between null and NULL.  Are x == null and x == 
NULL the same?  The compiler is gcc 3.2.

Thanks.

0
asdf209 (150)
9/24/2003 7:03:29 PM
comp.lang.c 30657 articles. 4 followers. spinoza1111 (3246) is leader. Post Follow

17 Replies
675 Views

Similar Articles

[PageSpeed] 36

RHNewBie <asdf@asdf.com> wrote:

>Hello,
>What is the difference between null and NULL.  Are x == null and x == 
>NULL the same?  The compiler is gcc 3.2.
>
NULL is defined as a null pointer constant by your implementation. 
null isn't required by the standard to be defined in an implementation.

See c.l.c-faq section 5
http://www.eskimo.com/~scs/C-faq/top.html


Regards

Irrwahn
-- 
Great minds run in great circles.
0
9/24/2003 7:31:29 PM
On Wed, 24 Sep 2003, RHNewBie wrote:

> Hello,
> What is the difference between null and NULL.  Are x == null and x ==
> NULL the same?  The compiler is gcc 3.2.

C language has a NULL but there is no null. When writing I use NULL to
indicate the macro defined in <stdlib.h> and when discussing pointers. I
use null when discussing the null character. By null character I am
referring to '\0'.

With that semantics out of the way, if you are asking for the difference
between (x == '\0') and (x == NULL) it would depend on what x is. Off the
top of my head, they will evaluate to the same result for all x. I,
personally, use (x == '\0') if x is a char or int and (x == NULL) if x is
a pointer. For everything else you'd have to consider whether the
comparison is valid and how things will be converted or promoted.

-- 
darrell at cs dot toronto dot edu
or
main(){int j=1234;char t[]=":@abcdefghijklmnopqrstuvwxyz.\n",*i=
"iqgbgxmdbjlgdv.lksrqek.n";char *strchr(const char *,int);while(
*i){j+=strchr(t,*i++)-t;j%=sizeof t-1;putchar(t[j]);} return 0;}
0
darrell13 (403)
9/24/2003 7:38:47 PM

RHNewBie wrote:
> 
> Hello,
> What is the difference between null and NULL.  Are x == null and x ==
> NULL the same?  The compiler is gcc 3.2.
> 
> Thanks.
Depends on how you have defined "NULL" and "null". These may or may not
be defined in some include file that you include.
-- 
Fred L. Kleinschmidt
Boeing Associate Technical Fellow
Technical Architect, Common User Interface Services
M/S 2R-94  (206)544-5225
0
9/24/2003 7:58:16 PM

RHNewBie wrote:
> 
> Hello,
> What is the difference between null and NULL.  Are x == null and x ==
> NULL the same?  The compiler is gcc 3.2.
> 
> Thanks.
Depends on how you have defined "NULL" and "null". These may or may not
be defined in some include file that you include.
-- 
Fred L. Kleinschmidt
Boeing Associate Technical Fellow
Technical Architect, Common User Interface Services
M/S 2R-94  (206)544-5225
0
9/24/2003 7:58:20 PM
"RHNewBie" <asdf@asdf.com> wrote in message
> What is the difference between null and NULL.  Are x == null and x
> ==  NULL the same?  The compiler is gcc 3.2.
>
NULL is the ANSI way of declaring a pointer which is known to be invalid.
ptr = NULL;
and
ptr = 0;
are equivalent.

It is annoying if your compiler comes with a header that declares "null"
because of the possibility of confusion. "null" may well be decared the same
as NULL, or there may be some subtle difference, for instance "null" might
not be cast to a void * type. You need to check your compiler documentation
for details.


0
Malcolm
9/24/2003 9:42:54 PM
Irrwahn Grausewitz <irrwahn33REMOVECAPS@freenet.de> writes:

> RHNewBie <asdf@asdf.com> wrote:
> 
> >Hello,
> >What is the difference between null and NULL.  Are x == null and x == 
> >NULL the same?  The compiler is gcc 3.2.
> >
> NULL is defined as a null pointer constant by your implementation. 
> null isn't required by the standard to be defined in an implementation.

`null' is required not to be defined by an implementation.  It is
not part of the reserved namespace.
-- 
"It wouldn't be a new C standard if it didn't give a
 new meaning to the word `static'."
--Peter Seebach on C99
0
blp (3955)
9/24/2003 10:06:47 PM
"Malcolm" <malcolm@55bank.freeserve.co.uk> writes:

> It is annoying if your compiler comes with a header that declares "null"
> because of the possibility of confusion. "null" may well be decared the same
> as NULL, or there may be some subtle difference, for instance "null" might
> not be cast to a void * type. You need to check your compiler documentation
> for details.

The compiler is not allowed to declare `null' in any of the
standard header.  `null' is not part of the reserved namespace.
-- 
int main(void){char p[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz.\
 \n",*q="kl BIcNBFr.NKEzjwCIxNJC";int i=sizeof p/2;char *strchr();int putchar(\
);while(*q){i+=strchr(p,*q++)-p;if(i>=(int)sizeof p)i-=sizeof p-1;putchar(p[i]\
);}return 0;}
0
blp (3955)
9/24/2003 10:07:25 PM
Darrell Grainger wrote:
> 
> On Wed, 24 Sep 2003, RHNewBie wrote:
> 
> > Hello,
> > What is the difference between null and NULL.  Are x == null and x ==
> > NULL the same?  The compiler is gcc 3.2.
> 
> C language has a NULL but there is no null. When writing I use NULL to
> indicate the macro defined in <stdlib.h> and when discussing pointers. 
> I use null when discussing the null character. 
> By null character I am referring to '\0'.
> 
> With that semantics out of the way,
> if you are asking for the difference
> between (x == '\0') and (x == NULL) it would depend on what x is.
> Off the
> top of my head, they will evaluate to the same result for all x.

if x is of type int and NULL is ((void*)0),
then the result of the comparison is not defined by the standard.

-- 
pete
0
pfiland (6613)
9/25/2003 10:53:51 AM
"Ben Pfaff" <blp@cs.stanford.edu> wrote in message
news:87fzil4z8i.fsf@pfaff.stanford.edu...
> "Malcolm" <malcolm@55bank.freeserve.co.uk> writes:
>
> > It is annoying if your compiler comes with a header that declares "null"
> > because of the possibility of confusion. "null" may well be decared the
same
> > as NULL, or there may be some subtle difference, for instance "null"
might
> > not be cast to a void * type. You need to check your compiler
documentation
> > for details.
>
> The compiler is not allowed to declare `null' in any of the
> standard header.  `null' is not part of the reserved namespace.

IOW, a conforming implementation must accept something like:

#include <stdio.h>

static int new( int class, int object )
{
return class - object;
}

int main(void)
{
int null = 12;
int instanceof = 54;

printf("%d\n", new(instanceof, null) );
return 0;
}


? (assuming I've not introduced an unrelated error)


0
nobody76 (62)
9/25/2003 2:48:07 PM
BruceS <nobody@nospam.net> scribbled the following:
> "Ben Pfaff" <blp@cs.stanford.edu> wrote in message
> news:87fzil4z8i.fsf@pfaff.stanford.edu...
>> "Malcolm" <malcolm@55bank.freeserve.co.uk> writes:
>> > It is annoying if your compiler comes with a header that declares "null"
>> > because of the possibility of confusion. "null" may well be decared the
> same
>> > as NULL, or there may be some subtle difference, for instance "null"
> might
>> > not be cast to a void * type. You need to check your compiler
> documentation
>> > for details.
>>
>> The compiler is not allowed to declare `null' in any of the
>> standard header.  `null' is not part of the reserved namespace.

> IOW, a conforming implementation must accept something like:

> #include <stdio.h>

> static int new( int class, int object )
> {
> return class - object;
> }

> int main(void)
> {
> int null = 12;
> int instanceof = 54;

> printf("%d\n", new(instanceof, null) );
> return 0;
> }


> ? (assuming I've not introduced an unrelated error)

Yes. The implementation is allowed to reserve these kind of names
for itself:
- anything starting with "mem" or "str" and including zero or more
lowercase letters after that
- anything starting with "E" and including one or more uppercase
letters after that
- anything starting with an underscore
Other than those three, you're free to use any names you please, as
long as they don't contradict with standard names or keywords.

-- 
/-- Joona Palaste (palaste@cc.helsinki.fi) ---------------------------\
| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste       W++ B OP+                     |
\----------------------------------------- Finland rules! ------------/
"'So called' means: 'There is a long explanation for this, but I have no
time to explain it here.'"
   - JIPsoft
0
palaste (2323)
9/25/2003 3:07:00 PM
Joona I Palaste <palaste@cc.helsinki.fi> scribbled the following:
> Yes. The implementation is allowed to reserve these kind of names
> for itself:
> - anything starting with "mem" or "str" and including zero or more
> lowercase letters after that

Oops. As well as "mem" and "str", the "is" and "to" prefices are also
reserved.

> - anything starting with "E" and including one or more uppercase
> letters after that
> - anything starting with an underscore
> Other than those three, you're free to use any names you please, as
> long as they don't contradict with standard names or keywords.

-- 
/-- Joona Palaste (palaste@cc.helsinki.fi) ---------------------------\
| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste       W++ B OP+                     |
\----------------------------------------- Finland rules! ------------/
"It was, er, quite bookish."
   - Horace Boothroyd
0
palaste (2323)
9/25/2003 3:07:54 PM
In <3F72C942.884@mindspring.com> pete <pfiland@mindspring.com> writes:

>Darrell Grainger wrote:
>> 
>> On Wed, 24 Sep 2003, RHNewBie wrote:
>> 
>> > Hello,
>> > What is the difference between null and NULL.  Are x == null and x ==
>> > NULL the same?  The compiler is gcc 3.2.
>> 
>> C language has a NULL but there is no null. When writing I use NULL to
>> indicate the macro defined in <stdlib.h> and when discussing pointers. 
>> I use null when discussing the null character. 
>> By null character I am referring to '\0'.
>> 
>> With that semantics out of the way,
>> if you are asking for the difference
>> between (x == '\0') and (x == NULL) it would depend on what x is.
>> Off the
>> top of my head, they will evaluate to the same result for all x.
>
>if x is of type int and NULL is ((void*)0),
>then the result of the comparison is not defined by the standard.

The standard actually requires a diagnostic in this case (constraint
violation).

Dan
-- 
Dan Pop
DESY Zeuthen, RZ group
Email: Dan.Pop@ifh.de
0
Dan.Pop (3615)
9/25/2003 3:17:55 PM
On Wed, 24 Sep 2003 19:58:16 GMT, "Fred L. Kleinschmidt"
<fred.l.kleinschmidt@boeing.com> wrote in comp.lang.c:

> 
> 
> RHNewBie wrote:
> > 
> > Hello,
> > What is the difference between null and NULL.  Are x == null and x ==
> > NULL the same?  The compiler is gcc 3.2.
> > 
> > Thanks.
> Depends on how you have defined "NULL" and "null". These may or may not
> be defined in some include file that you include.

NULL is required to be defined in a number of standard C headers.  It
produces undefined behavior to try to define it in a program that
includes any of the standard headers defining it.

-- 
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
0
jackklein (3930)
9/26/2003 2:51:12 AM
On 25 Sep 2003 15:07:00 GMT, Joona I Palaste <palaste@cc.helsinki.fi>
wrote in comp.lang.c:

> BruceS <nobody@nospam.net> scribbled the following:
> > "Ben Pfaff" <blp@cs.stanford.edu> wrote in message
> > news:87fzil4z8i.fsf@pfaff.stanford.edu...
> >> "Malcolm" <malcolm@55bank.freeserve.co.uk> writes:
> >> > It is annoying if your compiler comes with a header that declares "null"
> >> > because of the possibility of confusion. "null" may well be decared the
> > same
> >> > as NULL, or there may be some subtle difference, for instance "null"
> > might
> >> > not be cast to a void * type. You need to check your compiler
> > documentation
> >> > for details.
> >>
> >> The compiler is not allowed to declare `null' in any of the
> >> standard header.  `null' is not part of the reserved namespace.
> 
> > IOW, a conforming implementation must accept something like:
> 
> > #include <stdio.h>
> 
> > static int new( int class, int object )
> > {
> > return class - object;
> > }
> 
> > int main(void)
> > {
> > int null = 12;
> > int instanceof = 54;
> 
> > printf("%d\n", new(instanceof, null) );
> > return 0;
> > }
> 
> 
> > ? (assuming I've not introduced an unrelated error)
> 
> Yes. The implementation is allowed to reserve these kind of names
> for itself:

Sorry, but all three of these are just a little bit wrong.  I've
pasted in text from the current standard (but it has been the same
since ANSI 89):

> - anything starting with "mem" or "str" and including zero or more
> lowercase letters after that

"7.26.11 String handling <string.h>
1 Function names that begin with str, mem, or wcs and a lowercase
letter may be added to the declarations in the <string.h> header."

So it's "one or more", not "zero or more".

> - anything starting with "E" and including one or more uppercase
> letters after that

"7.26.3 Errors <errno.h>
1 Macros that begin with E and a digit or E and an uppercase letter
may be added to the declarations in the <errno.h> header."

So it's also E followed by a digit.  E2 is reserved, for example.

> - anything starting with an underscore

"7.1.3 Reserved identifiers
1 Each header declares or defines all identifiers listed in its
associated subclause, and optionally declares or defines identifiers
listed in its associated future library directions subclause and
identifiers which are always reserved either for any use or for use as
file scope identifiers.
� All identifiers that begin with an underscore and either an
uppercase letter or another underscore are always reserved for any
use.
� All identifiers that begin with an underscore are always reserved
for use as identifiers with file scope in both the ordinary and tag
name spaces."

So the identifiers _1, _2, and so on, are in the user namespace, as
are _local and such, with a lower case letter in block scope
variables, and so on.

> Other than those three, you're free to use any names you please, as
> long as they don't contradict with standard names or keywords.

Joona, are you familiar with the American slang expression, "Close,
but no cigar"?

-- 
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
0
jackklein (3930)
9/26/2003 2:58:32 AM
Jack Klein wrote:
 
> Joona, are you familiar with the American slang expression, "Close,
> but no cigar"?

I associate that expression with this type of device:

http://www.rental-world.com/images/Carnival%20Equipment%20Easy%20Striker.jpg
0
pfiland (6613)
9/26/2003 11:14:27 AM
Jack Klein <jackklein@spamcop.net> scribbled the following:
> On 25 Sep 2003 15:07:00 GMT, Joona I Palaste <palaste@cc.helsinki.fi>
> wrote in comp.lang.c:
>> Yes. The implementation is allowed to reserve these kind of names
>> for itself:

> Sorry, but all three of these are just a little bit wrong.  I've
> pasted in text from the current standard (but it has been the same
> since ANSI 89):

>> - anything starting with "mem" or "str" and including zero or more
>> lowercase letters after that

> "7.26.11 String handling <string.h>
> 1 Function names that begin with str, mem, or wcs and a lowercase
> letter may be added to the declarations in the <string.h> header."

> So it's "one or more", not "zero or more".

>> - anything starting with "E" and including one or more uppercase
>> letters after that

> "7.26.3 Errors <errno.h>
> 1 Macros that begin with E and a digit or E and an uppercase letter
> may be added to the declarations in the <errno.h> header."

> So it's also E followed by a digit.  E2 is reserved, for example.

>> - anything starting with an underscore

> "7.1.3 Reserved identifiers
> 1 Each header declares or defines all identifiers listed in its
> associated subclause, and optionally declares or defines identifiers
> listed in its associated future library directions subclause and
> identifiers which are always reserved either for any use or for use as
> file scope identifiers.
> � All identifiers that begin with an underscore and either an
> uppercase letter or another underscore are always reserved for any
> use.
> � All identifiers that begin with an underscore are always reserved
> for use as identifiers with file scope in both the ordinary and tag
> name spaces."

> So the identifiers _1, _2, and so on, are in the user namespace, as
> are _local and such, with a lower case letter in block scope
> variables, and so on.

>> Other than those three, you're free to use any names you please, as
>> long as they don't contradict with standard names or keywords.

> Joona, are you familiar with the American slang expression, "Close,
> but no cigar"?

Yes I am. It comes from old American pinball-type machines where the
machine offered minuscule cash prizes, but the main prize was a
luxurious cigar. Thanks for the corrections.

-- 
/-- Joona Palaste (palaste@cc.helsinki.fi) ---------------------------\
| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste       W++ B OP+                     |
\----------------------------------------- Finland rules! ------------/
"O pointy birds, O pointy-pointy. Anoint my head, anointy-nointy."
   - Dr. Michael Hfuhruhurr
0
palaste (2323)
9/26/2003 11:55:05 AM
In <bkv0ak$9u2$1@oravannahka.helsinki.fi> Joona I Palaste <palaste@cc.helsinki.fi> writes:

>Yes. The implementation is allowed to reserve these kind of names
>for itself:
>- anything starting with "mem" or "str" and including zero or more
>lowercase letters after that

s/zero/one

>Other than those three, you're free to use any names you please, as
>long as they don't contradict with standard names or keywords.

Nope, the list of reserved prefixes is much longer.  "is" and "to" are
particularly easy to be accidentally used: "iso_name" or "toxicity" are
perfectly natural and innocent looking identifiers.

Dan
-- 
Dan Pop
DESY Zeuthen, RZ group
Email: Dan.Pop@ifh.de
0
Dan.Pop (3615)
9/26/2003 2:20:05 PM
Reply: