Strings
Home

 

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

\n carriage return \0 null, end of string
\b backspace \f form feed (new page)
\r return, go to start of current line \t tab
\' apostrophe \" double quotes
\\ backslash \nnn ASCII character octal number nnn

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:
/*  is this possibly right??? */
Most compilers will flag this with a warning, which most people will find confusing.

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
    char chString[] = "this is \
a very long string.";
    more 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
    char chString[] = "this is "
    "a very long string.";
    more code code code

These two examples should produce exactly the same string.

String manipulation

The following is legal since in C you can initialize a variable when it is defined :
char chString[10] = "String";

but you cannot copy a string the same way, this will not work:
chString = "String";

To work with strings you must use routines in the string.h header.

strcpy:  Copy string to variable:
strcpy( chString , "String" );

strcat: Copy string to end of existing string:
strcpy( chString , "This is a ");
strcat( chString , "string." );
will result in "This is a string" stored in chString.

strlen Determine the length of a string.
i = strlen( chString );
i will be the number of characters in the string.  Since an array always starts with 0 in C, chString[i] will be the null character that terminates the string.

strcmp Comparing two strings.
strcmp( chString , "String" );
will return 0 if the two strings are equal (since it just subtracts the two strings as integers, and returns the difference).  This is a major trap in C, since 0 is FALSE and non-zero is TRUE.  In the following if statement, the branch will execute if the strings are not equal:
if( strcmp( chString , "String" ) )
{
    code for strings not the same
}
else
{
    code for strings the same
}

Most often you need to put a "not" (!) in front of strcmp for it to make logical sense:
if( !strcmp( chString , "String" ) )
{
    code for two strings equal
}

writing numbers into a string

sprintf works like printf, but writes its output into a string.