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 ?
- C Programming does not provide inbuilt ‘String‘ data type. [See Available Data Types in C]
- String is one of the most important and necessary Data Structure in C Programming
- We can say String as Variable length Structure stored in Fixed Length Structure
- 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 -
- Before Initializing String it Contain Garbage Characters.
- Length of String [i.e Declared character array] is 7 so it Contain 7 Garbage Characters.
- 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 |
Packing Up : Summary
Without NULL character string stored will be | CAT'@af |
---|---|
NULL Character | '\0' |
ASCII Value of NULL Character | 0 |