NULL Character in C with Examples

This C Tutorial explains NULL Character in C with examples.

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.

If you wish to look at all C Tutorials, go to C Tutorials.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.