null and NULL: is there any difference?

R

RHNewBie

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

Thanks.
 
I

Irrwahn Grausewitz

RHNewBie said:
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
 
D

Darrell Grainger

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[]=":mad: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;}
 
F

Fred L. Kleinschmidt

RHNewBie said:
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.
 
F

Fred L. Kleinschmidt

RHNewBie said:
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.
 
M

Malcolm

RHNewBie said:
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.
 
B

Ben Pfaff

Irrwahn Grausewitz said:
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.
 
B

Ben Pfaff

Malcolm said:
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.
 
P

pete

Darrell said:
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.
 
B

BruceS

Ben Pfaff said:
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)
 
J

Joona I Palaste

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 ([email protected]) ---------------------------\
| 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
 
J

Joona I Palaste

Joona I Palaste said:
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 ([email protected]) ---------------------------\
| 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
 
D

Dan Pop

In said:
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
 
J

Jack Klein

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
 
J

Jack Klein

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
 
J

Joona I Palaste

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):
"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".
"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.
"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.
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 ([email protected]) ---------------------------\
| 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
 
D

Dan Pop

In said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top