NULL Character : Terminating Character in String [ C programming ]

NULL Character : Terminating Character in String

We have already studied how string is declared and initialized now in this chapter we will be learning about terminating character of String i.e NULL Character.

Why do we need Terminating Character ?

  1. C Programming does not provide inbuilt ‘String‘ data type. [See Available Data Types in C]
  2. String is one of the most important and necessary Data Structure in C Programming
  3. We can say String as Variable length Structure stored in Fixed Length Structure
  4. Array Size is not the Actual Length of the String , So to recognize the End of the String we use NULL character

Pictorial Understanding :

Explanation :

In the above picture we can see -

  1. Before Initializing String it Contain Garbage Characters.
  2. Length of String [i.e Declared character array] is 7 so it Contain 7 Garbage Characters.
  3. When we attempt to store “CAT” in the string of Character length 7 then String Would be “CAT@’af
Location Value Before Initialization Value After Initialization
name[0] a C
name[1] ; A
name[2] # T
name[3] ` NULL Character
name[4] @ @
name[5] a a
name[6] f f
Since valid String is “CAT” whenever we are trying to access the array using character array variable then valid string before NULL character will be considered as Valid.

Packing Up : Summary

Without NULL character string stored will be CAT'@af
NULL Character '\0'
ASCII Value of NULL Character 0