A NULL ending is what distinguishes a char array from a string (NULL-terminated char -array) in C. Most string management functions rely on NULL to know when a string is completed (and its job is executed) and will not work with simple char -array (for example, they will continue to work beyond the boundaries of the array and continue until they find NULL somewhere in memory - they often corrupt memory when it goes).
In C, 0 (an integer value) is considered logical FALSE - all other values ββare considered TRUE. if , for and , whereas it uses 0 (FALSE) or nonzero (TRUE) to remove from branching or if loop. char is an integer type, the NULL character (\ 0) is actually and just a char with a decimal integer value of 0 - i.e. FALSE. This simplifies the execution of functions for operations such as manipulating or copying strings, since they can safely loop until the character he is working on is non-zero (i.e. TRUE) and stops when he encounters a NULL character ( i.e. FALSE) - since this means the end of the line. It makes very simple loops, since we donβt need to compare, we just need to know if it is 0 (FALSE) or not (TRUE).
Example:
char source [] = "Test"; // Actually: T est \ 0 ('\ 0' is the NULL-character)
char dest [8];
int i = 0;
char curr;
do {
curr = source [i];
dest [i] = curr;
i ++;
} while (curr); // Will loop as long as condition is TRUE, ie. non-zero, all chars but NULL.
source share