Jump to content
  • Advertisement
Sign in to follow this  
gp343

Differences been NULL and '\0' in C

This topic is 3717 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

In Ansi C, is there a differences between '\0' and NULL? If so, can someone explain what it is?

Share this post


Link to post
Share on other sites
Advertisement
'\0' is of type char and is used for c-string termination.
NULL (I believe) is a macro for (void*)0, so it is of type "pointer to something".

So what's different about them? Everything, because they have nothing do do with each other.

Share this post


Link to post
Share on other sites
In ANSI C, NULL is equivalent to (void *)0 (typically defined in one of the headers, like stdlib.h), while '\0' is equivalent to 0. ANSI C does not perform automatic conversion of 0 to pointer types, so there is a difference.

Share this post


Link to post
Share on other sites
Quote:
Original post by Oluseyi
'\0' is equivalent to 0

Really? Does that mean that there are no character literals in C?

Share this post


Link to post
Share on other sites
Quote:
Original post by DevFred
Quote:
Original post by Oluseyi
'\0' is equivalent to 0

Really? Does that mean that there are no character literals in C?

No, it means that the character \0 has character code 0. Similarly, 'A' is equivalent to 65.

Share this post


Link to post
Share on other sites
Quote:
Original post by DevFred
Really? Does that mean that there are no character literals in C?


Character literals each have an equivalent integer value. So for any given character you can use the character literal or the integer literal in decimal, hex, or octal formats. Take a look at an ASCII table for details.

Share this post


Link to post
Share on other sites
Quote:
Original post by DevFred
Quote:
Original post by Oluseyi
'\0' is equivalent to 0

Really? Does that mean that there are no character literals in C?


yes, but char is an integral type. The ' notation is really just a shorthand for easily assigning the ASCII value to a char. '\0' is the value 0 in the ASCII table, so '\0' == 0. In the same way 'A' == 65. It's the same in C++.

Edit:
Quote:
Original post by Sneftel
No, it means that the character \0 has character code 0. Similarly, 'A' is equivalent to 65.

Same example, what are the odds [smile]

Share this post


Link to post
Share on other sites
Er, yeah, my brain skipped. Character literals have int type in C, and char type in C++.

Share this post


Link to post
Share on other sites
Quote:
Original post by Sneftel
Character literals have int type in C, and char type in C++.

It seems you're right. The following code
#include <stdio.h>

int main()
{
printf("\\0: %d\n", sizeof('\0'));
printf(" 0: %d\n", sizeof(0));
}

produces different results on C and C++ compilers.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!