BlogsDope image BlogsDope

NULL pointer v/s Null character ('\0') v/s '0' in C

March 4, 2021 C POINTERS 14871

Today we are going to discuss one of the most interesting concept. Well, there is a significant difference between Null pointer, Null character and '0'. Let's discuss about them individually.

NULL Pointer 

In C programming language, a variable that can point to or store the address of another variable is known as pointer. And a null pointer is a pointer which points at nothing. Well, you may think why do we need something that is pointing at nothing well, let's understand how useful they are.

Whenever you declare a pointer in your program, it points to some random memory location. And when you try to fetch the information at that location, you get garbage values in return. And if you use that garbage value unintentionally or intentionally in your program then your program will crash. Here, in this case Null pointer is something helpful and can solve these types of issues.

Let's see how its done.

//C Code
#include <stdio.h>
int main() {
   int *ptr=NULL;//initializing the pointer to null.
   printf("The value of pointer is %x",ptr);
   return 0;
}

Output : The value of pointer is 0

It is not like uninitialized pointer which points to unknown location. If we don't know the location of a variable then we need initialise that variable to NULL to avoid errors. 

They are very useful while de-allocating memory. Suppose, you have a pointer which points to some memory location where data is stored. If you don’t need that data anymore, and you want to erase that data and free up that memory. But even after freeing the data, pointer still points to the same memory location. This pointer is called as a dangling pointer. To avoid this dangling pointer, you can set the pointer to NULL.

They are very useful in implementing linked list data structure. We know that each element in a linked list is called a node. A single node contains data and a pointer to the next node which helps in maintaining the structure of the list. The first node points to the second node, and second node points to the third node, and further like that. As there is no successor node to the last node, you need to assign a NULL value to the link of the last node which helps us determining we have reached the end of the list.

Well, NULL pointer is a concept. Like C programming you can see the same use of it in different languages too.

NULL Character  ('\0) and character '0'

In C language string is an array of char type. It stores each of the characters in a memory space of 1 byte. And each array is terminated with ‘\0’ or null character. It is also referred as NUL and is different than NULL or Null Pointer. The backslash (\) is an escape character. When followed by a number, the escape sequence represents the ascii character (in octal) associated with the number, thus \0 (that is a zero) means the character with ascii value zero, also known as NUL.

But why do we need them ?

Well in C, there are no strings. There are only arrays of characters instead. In C, arrays don't carry length information. So it would be impossible to determine the length of the string by just examining the array. Instead, functions like strlen read a string character by character until \0 is encountered in memory. This is why your program might crash or have improper behaviour if you don't NUL terminate the string.

Basically, ‘\0’ is defined to be a null character. It is a character with all bits set to zero.

But when we store a ‘0’ inside a string both are not same according to the C language. ‘0’ means 48 and ‘\0’ means 0 according to the ASCII table.  

'0' refers to the character zero, character types can be interpreted as numbers (e.g., 48) or as ASCII characters (e.g., '0'). 

Let's see an example so that you can get a better concept of it : 

//C Code
#include <stdio.h>
int main()
{
    printf("Hello 0 World\n"); // Adding '0' in between, it gets printed normally.
    printf("Hello \0 World "); //Adding '\0' in between, since it is a escape character, 
    // printf thinks this is the end point and stops itself.
    return 0;
}

Output : Hello 0 World
         Hello 

We added '0' in between a printf statement, and it got printed normally, but when we added '\0' i.e., null character between the printf statement, since it is a escape character printf function thinks that this is the end point and so it stops.

Hence, '0' and '\0' cannot be used interchangeably at all, because they are different values. 48 is different from 0, and the character zero is different from the null character (string terminator). Using a '\0' in a string where you meant '0' will terminate your string early. Using a 48 where you meant a 0 will make your math all wrong. So, we can say that '\0' is an escape sequence used to represent nothing, not even zero. It is used to represent null terminator. On contrast 0 is as it is, a simple zero.


Liked the post?
Hi, I am Priyansh Jha. I am a pre-final year student of Electronics and Communication major undergraduate.
Editor's Picks
0 COMMENT

Please login to view or add comment(s).