In C programming, character strings are terminated by NULL Byte. The representation for NULL Byte is ‘\0’ or ‘0’ (zero) or NULL. This way we wouldn’t be able to use NULL character in ours’ strings. Then why NULL character and not any other is used as a string terminator? Firstly, C doesn’t have explicit “string data type” and so how strings, an integral component, of everyday requirement to be handled? This requires to use some character to terminate the character arrays so the same could be treated as strings. Then which character? And why NULL character? The fact is NULL character doesn’t have any graphic printable symbol associated with it and consequently not required and this is why it’s used as a string terminator.
Recall that character strings and string literals are each terminated by NULL byte. String literal, however, doesn’t have visible NULL terminator. Compiler provides every string literal a NULL terminator while compiling the program. But character arrays may or may not contain NUL terminator. Character arrays which contain terminating NULL byte are strings. For example:
char season[] = {'a','u','t','u','m'}; /* character array */ char country[] = {'I','n','d','i','a','\0'}; /* character string */ char *str = "Hello, how are you?"; /* string literal: No visible NULL terminator */
Non-string data, however, can contain NULL characters. But string handling functions are particular with STRINGS and can’t deal with NON-STRING DATA. To process NON-STRING data, use memory functions instead.
Sanfoundry Global Education & Learning Series – 1000 C Tutorials.
- Practice Computer Science MCQs
- Practice BCA MCQs
- Apply for Computer Science Internship
- Watch Advanced C Programming Videos
- Check Computer Science Books