×

Null character in C

Introduction

The Null character in the C programming language is used to terminate the character strings. In other words, the Null character is used to represent the end of the string or end of an array or other concepts in C. The end of the character string or the NULL byte is represented by ‘0’ or ‘\0’ or simply NULL. The NULL character doesn’t have any designated symbol associated with it and also it is not required consequently. That’s the major reason it is used as a string terminator.

Note: The memory space for each character stored by NULL is 1 byte.

Null Character in C

Not only strings or arrays, there are various concepts in the C programming language which are terminated by a NULL byte. In concepts like Arrays, string literals, character strings a NULL byte is used to indicate the end of the string. This can be well illustrated using an example of an array.

Example: 

Suppose there is an array of size 10 and we need to store a string “computer” inside it. This can be simply done using the following piece of code;

char a[10] = "computer";

When this piece of code is executed, an array of size 10 is created with string “computer” inside it which looks something like this;

computer\0 

Here, the ‘\0’ is used to indicate the end of a string.

Note: The NULL character must not be overwritten with something else because it may lead to a condition where the end of the string cannot be determined.

Example:

In the above array, the a[8] represents the NULL byte and if it is overwritten, suppose

a[8] = “$” ; 

The array will now look like;

computer$ 

This is an issue because if we overwrite the ‘\0’ to ‘$”, there is no way to tell the end of the string.

Code correction:

In the above code, we are assigning the size of the array beforehand which may lead to problems sometimes, the good practice is to declare the array in the following manner;

char a[ ] = “computer”;

Here, the size of the string can be used to determine the size of the array since it happens at compile time, hence it is statistically typed and not dynamically typed. Now, the array will look like this;

computer$

Null Character in C vs Null pointer in C

Null characterNull pointer
‘\0’ is defined to be a null character‘\0’ is defined to be a null character
The Null character is used to represent the end of the string or end of an array or other concepts in C. The end of the character string or the NULL byte is represented by ‘0’ or ‘\0’ or simply NULL.The constant ‘0’ has different meanings which are dependent on the use. This can be referred to as a null pointer constant if the use of ‘0’ is to compare any pointer to 0.
Eg: printf ("The value of \\0 is %d \n", '\0'); //prints the value of \0

Output: The value of \0 is 0
Eg:  printf("The value of 0 is %d \n", '0'); //prints the value of 0

Output: The value of 0 is 48

Program: Print the value of ‘0’ and ‘\0’

#include<stdio.h>
 #include <string.h>  //header files
 int main()
 {


 printf ("The value of \\0 is %d \n", '\0');  // prints the value of \0
 printf("and the value of 0 is %d \n", '0'); // prints the value of 0
 return 0;


 } 

Output of the program:

The value of \0 is 0

and the value of 0 is 48

 

 

Advantages of using the Null Character in C

  • By using the NULL character or NULL byte, we can quickly check whether a value is null or not.
  • Generally, in C, we can use any other type of variable instead of NULL, but using such might require an explicit or external comparison for a specific value.

Every time we come across a comparison where the result of the comparison is ‘0’ or zero, then there is a change in the status register. A flag is automatically set in the status register. This can be used in conditional instructions (used for comparisons), such as conditional jump.

  • Also, another advantage of using NULL is that it represents invalid value pointers.

Related Topics

C Data type

Data Types in C with Examples Data types are used to define the type of data that a variable can store to perform a specific operation.ANSI C provides three types of...

2 minutes read.

C Program to find the Roots of a Quadratic Equation

What is Quadratic Equation: Equations of degree 2 in polynomial form are called quadratic equations. A, B, and C are the coefficient variables in the equation, which is written as ax2...

3 minutes read.

If else Programs in C Programming

If else Programs in C programming The if-else statement in C is based on some particular conditions to perform the operations. If and only if the given condition is valid, the operations listed in...

4 minutes read.

Actual and Formal Parameters

Any variable declared within the parenthesis is referred to as the parameters during the function declaration. Parameters tell the function about the argument datatype, their order, and the number of...

5 minutes read.

C vs Java Strings

String in C In C, we can define a string as a bunch of characters. A character array is distinguished from a string by the presence of the special character '\0'...

5 minutes read.

Pure Virtual Function in C

Before knowing about the pure virtual function some of the important points about the virtual function in C++ are given below. In c++ the member function that is defined in the...

4 minutes read.

Assert() Function in C

Assert (): In C, the statements are executed with the exit statement. In C language declare, and it tests the condition parameters. If the statement executed will be false, it shows...

4 minutes read.

7 Best IDEs for C/C++ Developers in 2024

As we all know that in programming languages, C is known as the building block which cannot be contradicted, and C++ is the prolong kind of C and it can...

4 minutes read.

Multilevel Feedback Queue Scheduling (MLFQ) CPU Scheduling

In this article, you will learn how to initialise an array to 0 in C In C, an array is declared as: char ZEROARRAY[2022]; The global scope changes at runtime to all zeros....

3 minutes read.

Random function in C

Random function in C In the C programming language, the rand() is a function used for Pseudo Random Number Generator (PRNG). The random number generated by the rand() function is not...

4 minutes read.

memmove() in C

Introduction: In this article we are discuss about the memmove() function in C. This function transfers memory blocks from one location to another. The memmove() function is declared in the...

3 minutes read.

Passing Array to Function in C

Need to pass Arrays? The need to pass arrays to a function arises when we need to pass a list of values to a given function. During the course of our programming...

4 minutes read.

SJF Scheduling Program in C

The SJF (shortest job first) or the shortest job next is the programming scheduling in the C. It is one of the CPU scheduling programming. The SJF is defined as...

4 minutes read.

Explain the two-way selection in C

In C programming, a two-way selection statement is used to choose between two different options based on a Boolean expression. The two-way selection statement in C is the if statement. The...

6 minutes read.

Ftell() Function in C

Ftell(): In File Handling we have some special functions like Ftell(), Fseek(), rewind() etc.. while you are randomly accessing the file these functions play very important role and these functions...

3 minutes read.

#error #pragma in C

#error, #pragma in C #error, also known as the error directive in the C language. It will not allow you to make any compilation fail and immediately issues a statement which...

4 minutes read.

Bank management system in C

This is a mini project that is constructed completely using the C language. The bank management system is a beginner friendly project. To construct this user only needs to know...

12 minutes read.

If Statement in C

Decision Control Statements We frequently desire one set of instructions to be carried out in one circumstance while another set of instructions to be carried out in a completely different circumstance....

3 minutes read.

C pre-processor

The C processor is a macro processor which is used to compile the source code of the program (step by step) . It is not a part of the compiler....

4 minutes read.

String literals in C

Constants refer to the fixed values of the program that may not alter during the compiling ad execution. These fixed values are known as literals. In other words, the values...

3 minutes read.