NULL, '

Thread: NULL, '\0' and 0. Help

' and 0. Help

This is a discussion on NULL, '

Thread: NULL, '\0' and 0. Help

' and 0. Help
within the C Programming forums, part of the General Programming Boards category; Recently, some friends of mine said that NULL, '

Thread: NULL, '\0' and 0. Help

' and 0 are equal because they are all 0-value in ASCII. ...

  1. #1
    C/C++Newbie Antigloss's Avatar
    Join Date
    May 2005
    Posts
    212

    NULL, '\0' and 0. Help

    Recently, some friends of mine said that NULL, '\0' and 0 are equal because they are all 0-value in ASCII.

    I argued, " NO! They are different. Maybe '\0' and 0 are equal, but NULL is different from '\0' and 0 because NULL can be (void *)0. "

    But they still insisted on their point.
    I have no other points to beat them. Could you please tell me more about the differences among NULL, '\0' and 0?

    Thank you in advance.

  2. #2
    FOX
    Join Date
    May 2005
    Posts
    188
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void)
    {
            if (NULL == 0)
                    puts("TRUE: NULL == 0");
    
            if ('\0' == 0)
                    puts("TRUE: '\\0' == 0");
    
            if ('\0' == NULL)
                    puts("TRUE: '\\0' == NULL");
    
            return EXIT_SUCCESS;
    }
    No compiler warnings about type mismatching.

  3. #3
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Quote Originally Posted by ^xor
    No compiler warnings about type mismatching.
    Thats all well and good but it proves nothing. For if they truely were interchangable and then this would not raise any problems
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void)
    {
            int i = 5;
            int* ptr = malloc(1);
            char ch = 'e';
            /* Checking an int vs the other two */
            if ( i == NULL)
                    puts ("This shouldn't happen");
            else
                    puts ("But this should");
            if ( i == '\0')
                    puts ("This shouldn't happen");
            else
                    puts ("But this should");
            /* Checking a ptr vs the other two */
            if ( ptr == 0)
                    puts ("This shouldn't happen");
            else
                    puts ("But this should");
            if ( ptr == '\0')
                    puts ("This shouldn't happen");
            else
                    puts ("But this should");
            /* Checking a character vs the other two */
            if ( ch == NULL)
                    puts ("This shouldn't happen");
            else
                    puts ("But this should");
            if ( ch == 0)
                    puts ("This shouldn't happen");
            else
                    puts ("But this should");
            free(ptr);
            return 0;
    }
    It of course raises two warnings:
    bt21.c: In function `main':
    bt21.c:10: warning: comparison between pointer and integer
    bt21.c:28: warning: comparison between pointer and integer
    0 and '\0' are basically the same since they are both ints. However NULL is a pointer and not an int.

  4. #4
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Please read every question in

    http://www.eskimo.com/~scs/C-faq/s5.html

  5. #5
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    some old C compilers do not define NULL as 0. I've seen it defined as (void*)0, (void _near *)0, (void _far *)0, (char *)0, (char _near*)0, and as (char _far*)0, none of which can be used for integers. But see Question 5.4 in cwr's post, which says NULL should only be used with pointers, so using NULL with integers or other data types is probably implementation defined.