preface

In C, we sometimes see NULL and sometimes we see ‘\0’. What’s the difference?

nature

Essentially, NULL, 0, and ‘\0’ are the same; they are all 0. Yes, you heard me right. That’s about the end of this article. But I’ll keep talking so I don’t get hit. They both have a value of 0, but they have different meanings.

NULL

The value is 0, but it has a different meaning, or it has a different type. NULL is a pointer type, but it is a NULL pointer, which has a value of 0.

/ / source: public [programming of mission] blog: https://www.yanbinghu.com
//null.c
#include<stdio.h>
int main(void)
{
	int a = NULL;
	printf("%p\n",a);
	return 0;
}
Copy the code

We compile it:

$ gcc -o null null.c
null.c: In function'main' : null. C :14:10: Warning: initialization makesinteger from pointer without a cast [-Wint-conversion]
  int a = NULL;
          ^
Copy the code

It gives us a warning to try to convert the pointer to an integer. This also validates our previous statement.

In fact, NULL is usually defined as follows:

#define NULL (void*)0
Copy the code

So, if you want to initialize a pointer type, you give it a NULL to make it obvious that it is a pointer.

Of course, in C++, you’d rather use nullptr than NULL.

‘\ 0’

We all know that \ is an escape character, enclosed in single quotes and then escaped, which is actually 0, except that it represents characters. It goes down like this:

/ / source: public [programming of mission] blog: https://www.yanbinghu.com
//nul.c
#include<stdio.h>
int main(void)
{
    char a = '\ 0';
    char b = '0';
    printf("a = %d,b = %d\n",a,b);
    return 0;
}
Copy the code

Compile and run:

$ gcc -o nul nul.c
./nul
a = 0,b = 48
Copy the code

We see it most often as the end of a string. So we often see something like this:

char str[16]; / *do something*/
str[15] = '\ 0';
Copy the code

Remember how printf prints strings, and the STRCMP comparison stop rule? Yes, they all end with a ‘\0’.

Notice, it’s completely different from ‘0’. As you can see by printing, the value of ‘\0’ is actually 0.

In particular, if the 0 of ‘\0′ is followed by an octal number, it will be escaped. So **’\60’ has the same value as ‘0’ **.

0

This one needs no explanation.

int a = 0;
Copy the code

“0”

The 0 in double quotes is a string, and what we don’t see is that it ends with a ‘\0’.

#include<stdio.h>
int main(void)
{
    char str[] = "0";
    printf("sizeof str is %d,string len is %d\n".sizeof(str),strlen(str));
    return 0;
}
Copy the code

Running results:

sizeof str is 2,string len is 1
Copy the code

“\ 0”

This is also a string, just two empty characters. The value contains 0 characters.

“”

A string. The value contains 1 characters and occupies 2 bytes. It is a space and a blank character.

conclusion

By this point you should be aware that they may have the same value, but not the same meaning, and that you should use the right value when appropriate for good readability.

Wechat public account [Programming] : focus on but not limited to sharing computer programming basics, Linux, C language, C++, data structures and algorithms, tools, resources and other programming related [original] technical articles.