Open In App

Difference between NULL pointer, Null character (‘\0’) and ‘0’ in C with Examples

Improve
Improve
Like Article
Like
Save
Share
Report

NULL Pointer:
The integer constant zero(0) has different meanings depending upon it’s used. In all cases, it is an integer constant with the value 0, it is just described in different ways.
If any pointer is being compared to 0, then this is a check to see if the pointer is a null pointer. This 0 is then referred to as a null pointer constant. The C standard defines that 0 is typecast to (void *) is both a null pointer and a null pointer constant.
The macro NULL is provided in the header file “stddef.h”.

Below are the ways to check for a NULL pointer:

  • NULL is defined to compare equal to a null pointer as:
    if(pointer == NULL)
    
  • Below if statement implicitly checks “is not 0”, so we reverse that to mean “is 0” as:
    if(!pointer) 
    

Null Characters(‘\0’):
‘\0’ is defined to be a null character. It is a character with all bits set to zero. This has nothing to do with pointers. ‘\0’ is (like all character literals) an integer constant with the value zero.

  1. Below statement checks if the string pointer is pointing at a null character.
    if (!*string_pointer)
    
  2. Below statement checks if the string pointer is pointing at a not-null character.
    if (*string_pointer)
    

In C language string is nothing but an array of char type. It stores each of the characters in a memory space of 1 byte. Each array is terminated with ‘\0’ or null character but if we store a ‘0’ inside a string both are not same according to the C language. ‘0’ means 48 according to the ASCII Table whereas ‘\0’ means 0 according to the ASCII table.

Below is the C program to print the value of ‘\0’ and ‘0’:




// C program to print the value of
// '\0' and '0'
#include <stdio.h>
  
// Driver Code
int main()
{
    // Print the value of
    // '\0' and '0'
    printf("\\0 is %d\n", '\0');
    printf("0 is %d\n", '0');
    return 0;
}


Output:

\0 is 0
0 is 48

Last Updated : 01 Jun, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads