Text stored on a computer consists of various displayable characters and perhaps some control codes, such as a tab (\t) or the newline (\n). The string has a starting point, but determining where and how the string ends differs depending on what is storing or reading the string.
As an example, one of the old DOS functions used the $ character to mark the end of a string of text. (The $ was borrowed from the even older CP/M operating system.) So if you wanted to display a string of text, you’d specify text like this:
This is a string of text.$
That meant it was somewhat difficult to display any text that used the $ character. Therefore, most DOS programmers avoided using that specific function.
Some early programming languages used the newline character to end a string. To display a newline, you had to output the single \n character or, specifically, character code 13 or code 10.
In the C language, the null character (\0) terminates a string. That’s fine because character code zero is neither a displayable character nor a control code.
A string in C has a starting point, which is an address or memory location. Then each character in the string occupies the next byte in memory, on and on up to the \0 character. Ta-da! That’s the end of the string. Simple. Elegant.
Keep in mind that the
\0is the null character, which simply means ASCII code zero. It’s not the same as theNULLvariable, which is a pointer.
You usually don’t need to worry about adding the \0 at the end of a string; the compiler does that for you. The C library text functions anticipate and deal with the \0. For example:
#include <stdio.h>
int main()
{
char *string = "How long can a string be?\n";
char *ptr;
ptr = string;
while(*ptr)
{
putchar(*ptr);
ptr++;
}
return(0);
}
Don’t shield your eyes from the scary pointer variables! In Line 8, ptr is set to the start of the text, the string pointer. Then the while loop at Line 9 evaluates the character referenced by *ptr. As long as that character isn’t zero, or \0, the loop spins: The value of *ptr is displayed by putchar() (Line 11) and then the address in ptr is incremented (Line 12).
The program’s output displays the string, one character at a time:
How long can a string be?
To demonstrate how the \0 works, edit Line 5 to read:
char *string = "How long can a string\0 be?\n";
(I added the red color to show where to stick the \0.)
Save, compile, and run to see only the first part of the string displayed — an no newline added:
How long can a string
The \0 effectively truncates the string.
While the C language functions, as well as the while loop in the sample code, work with the \0, you need to remember to add the \0 when you write your own string manipulation routines. The \0 is required to terminate the string. If you forget it, the result is what I call program puke all over the screen.
Next week’s Lesson further explores the string-creation concept.