A summary of how to work with strings.
Characters versus strings
A character is a single byte, and is enclosed with single quotes. 'a' and '\n' are examples or characters (the second is the escape sequence for a carriage return).
Strings are an array of characters, and must end with the null character, denoted by \0 and equal to zero. "string" is an example of a string.
Space must always be reserved for this null character, so "string" is actually 7 characters long (one extra for the \0 at the end).
escape sequences, trigraphs
This is a partial list of the escape sequences that can appear in a format string
A trigraph represents a "wide byte" character introduced with ISO C.
This is to allow rich character sets, such as Chinese. They begin with a double
question mark, ??. The most important thing to remember about trigraphs is that you
probably do not want to use them, but the compiler will think that you are if you put
double question marks in a comment, such as:
breaking strings across lines
There are two ways to break a string across a line. The first is to place a \ character immediately before the carriage return, and continue the string in the first column of the next line. This is used in the following example:
code code code
In this case the second line of the string could not be tabbed to the same alignment as the block since the tab would occur in the string. This can be avoided by specifying the string as two strings that will be concatenated with a single null character at the end:
code code code
These two examples should produce exactly the same string.
The following is legal since in C you can initialize a variable when it is defined :
but you cannot copy a string the same way, this will not work:
To work with strings you must use routines in the string.h header.
strcpy: Copy string to variable:
strcat: Copy string to end of existing string:
strlen Determine the length of a string.
strcmp Comparing two strings.
Most often you need to put a "not" (!) in front of strcmp for it to make
writing numbers into a string
sprintf works like printf, but writes its output into a string.