null and NULL: is there any difference?

Discussion in 'C Programming' started by RHNewBie, Sep 24, 2003.

  1. RHNewBie

    RHNewBie Guest

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

    Thanks.
    RHNewBie, Sep 24, 2003
    #1
    1. Advertising

  2. 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.
    >

    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.
    Irrwahn Grausewitz, Sep 24, 2003
    #2
    1. Advertising

  3. 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[]=":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;}
    Darrell Grainger, Sep 24, 2003
    #3
  4. 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
    Fred L. Kleinschmidt, Sep 24, 2003
    #4
  5. 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
    Fred L. Kleinschmidt, Sep 24, 2003
    #5
  6. RHNewBie

    Malcolm Guest

    "RHNewBie" <> 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.
    Malcolm, Sep 24, 2003
    #6
  7. RHNewBie

    Ben Pfaff Guest

    Irrwahn Grausewitz <> writes:

    > 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.
    > >

    > 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
    Ben Pfaff, Sep 24, 2003
    #7
  8. RHNewBie

    Ben Pfaff Guest

    "Malcolm" <> 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\
    );}return 0;}
    Ben Pfaff, Sep 24, 2003
    #8
  9. RHNewBie

    pete Guest

    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
    pete, Sep 25, 2003
    #9
  10. RHNewBie

    BruceS Guest

    "Ben Pfaff" <> wrote in message
    news:...
    > "Malcolm" <> 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)
    BruceS, Sep 25, 2003
    #10
  11. BruceS <> scribbled the following:
    > "Ben Pfaff" <> wrote in message
    > news:...
    >> "Malcolm" <> 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 () ---------------------------\
    | 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
    Joona I Palaste, Sep 25, 2003
    #11
  12. Joona I Palaste <> 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 () ---------------------------\
    | 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
    Joona I Palaste, Sep 25, 2003
    #12
  13. RHNewBie

    Dan Pop Guest

    In <> pete <> 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, Sep 25, 2003
    #13
  14. RHNewBie

    Jack Klein Guest

    On Wed, 24 Sep 2003 19:58:16 GMT, "Fred L. Kleinschmidt"
    <> 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
    Jack Klein, Sep 26, 2003
    #14
  15. RHNewBie

    Jack Klein Guest

    On 25 Sep 2003 15:07:00 GMT, Joona I Palaste <>
    wrote in comp.lang.c:

    > BruceS <> scribbled the following:
    > > "Ben Pfaff" <> wrote in message
    > > news:...
    > >> "Malcolm" <> 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
    Jack Klein, Sep 26, 2003
    #15
  16. RHNewBie

    pete Guest

    pete, Sep 26, 2003
    #16
  17. Jack Klein <> scribbled the following:
    > On 25 Sep 2003 15:07:00 GMT, Joona I Palaste <>
    > 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 () ---------------------------\
    | 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
    Joona I Palaste, Sep 26, 2003
    #17
  18. RHNewBie

    Dan Pop Guest

    In <bkv0ak$9u2$> Joona I Palaste <> 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, Sep 26, 2003
    #18
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    16
    Views:
    7,283
    Mike Schilling
    Oct 12, 2005
  2. Garg
    Replies:
    3
    Views:
    351
    =?ISO-8859-1?Q?Arne_Vajh=F8j?=
    Dec 24, 2006
  3. Carfield Yim
    Replies:
    2
    Views:
    369
    Carfield Yim
    Jul 2, 2009
  4. Lars Olsson
    Replies:
    2
    Views:
    100
    Jim Weirich
    Dec 2, 2005
  5. Xu, Qian
    Replies:
    2
    Views:
    74
Loading...

Share This Page