C Programming

How to Use NULL Character in C with Examples

The Null character is an important character in C programming that symbolizes the end of an array or string. The Null character is not necessary for programming because it does not have a recognized symbol and has no related meaning. It is mostly used as a string terminator and takes 1 byte of memory with the value of 0.

Remember that the Null character shouldn’t be replaced with anything else, since doing so could result in a situation where the string’s end cannot be identified.

How to Use Null Character in C?

In C Programming, the Null character can be used in multiple ways, such as:

1: Terminating a String

You can use the Null character in C Programming to terminate a string, here is an example that terminates the string “linuxhint” from the “welcome to linuxhint” string.

#include <stdio.h>

int main()

{

    printf("welcome to \0 linuxhint"); //Adding '\0' in between, since it is an escape character,
    // printf thinks this is the end point and stops itself.
    return 0;


}

2: Terminating an Array

The Null character in C programming can also be used for terminating an array, and the example for such a case is given below:

#include <stdio.h>

#include <string.h>

int main() {

    char arr[] = {'l','i','n', 'u', 'x', '\0','h', 'i', 'n', 't'}; // initialize the array with NULL characters
    printf("\nlength of the array of characters is: %d",strlen(arr));
    return 0;


}

3: I/O Operation

The Null character in C programming can also be used in I/O file operation to indicate the end of a file or stream:

#include <stdio.h>

int main() {

    FILE *fp;
    char c;
    fp = fopen("file.txt", "r");
    if (fp == NULL) {
        printf("Error: File does not exist");
        return 1;
    }
    while ((c = fgetc(fp)) != EOF) {
        if (c == '\0') {
            printf("End of file reached\n");
            break;
        }
        printf("%c", c);
    }
    fclose(fp);
    return 0;

}

How NULL Character Differs from ‘0’?

The constant ‘0‘ has different meanings in different contexts. It can either be used to compare a pointer to 0, also called null pointer constant, or can be used as a NULL character ‘\0’ to mark the end of an array or string in C programming.

Keep in mind that ‘\0′ and ‘0‘ are not the same things in C Programming. If you use ‘\0‘ in a code, it returns a character with an ASCII value of 0. While you will get a character with an ASCII value of 48 if you use the ‘0‘ in a program. Further, you will fail to terminate a string or array if you use ‘0‘ instead of ‘\0‘ in your program.

#include<stdio.h>

#include <string.h>

 int main()
 {
 printf ("The value of \\0 is %d \n", '\0');
 printf("The value of 0 is %d \n", '0');
 return 0;
 }

Conclusion

In C programming, a NULL character ends the character strings. The symbol for it is ‘\0‘ or NULL. Although the NULL character serves many different kinds of operations, its main objective is to end a string, array, or other C concept. This tutorial taught us what is the NULL character and how to use it in C programming. We also explored the difference between \0 and ‘0’ using an example.

About the author

Komal Batool Batool

I am passionate to research technologies and new ideas and that has brought me here to write for the LinuxHint. My major focus is to write on programming languages and computer science related topics.