Summary: Always pass at least one string to the printf function.
Previous chapters have used the printf function to display results on the screen. By now you should be accustomed to how it works. There's one rule you must always follow when using printf: pass it at least one format string, which may be a literal string or a pointer to a string. The string may or may not include format specifiers, which are defined below.
The printf function always prints to the stdout device. Unless the output has been redirected, the standard output device is the screen.
The following program illustrates some typical ways to manipulate strings and to print them:
/* PRTSTR.C: Print strings. */
#include <stdio.h>
#include <string.h>
main()
{
char aline[80], more[80];
char *strptr;
/* aline = "Another line."; */
/* Note: This causes a compiler error */
strcpy( aline, "Another line." );
strcpy( more, aline );
strptr = aline;
strcat( aline, "dog" );
printf( "A line of text." );
printf( aline );
printf( more );
printf( strptr );
}
The declarations come first:
char aline[80], more[80];
char *strptr;
The variables aline and more are arrays of characters. In this program, they act as strings. Although these arrays have 80 characters each (numbered 0–79), the maximum string length is 79 characters, because strings must end with a null character. The variable strptr is a pointer to a string.
If you've previously programmed in Basic, you might expect to use the equal sign to assign a value to a string variable. The program won't compile if you remove the comment symbols from the following line:
/* aline = "Another line."; */
Faced with this line, QuickC prints the error message:
2106: '=' : Left operand must be lvalue
(An “lvalue” is a value allowed on the left side of an equal sign.)
Summary: Use the strcpy function— not the equal sign— to copy a string.
You can use the equal sign to assign a value to a numeric variable. When you're using strings, however, you almost always use the library function strcpy, which copies a string to a character array from either a string constant or another array:
strcpy( aline, "Another line." );
strcpy( more, aline );
The strcpy function makes an exact copy of a string. The first argument is the address of the destination string. The second is the address of the source string. The first strcpy above copies " Another line." to the aline string. The second copies aline to more.
Note that the first argument must be the address of an array, but the second is either a string constant (enclosed in quotation marks) or the address of a character array.
The 80-character arrays have more than enough room for the 13 characters of "Another line." and a null character. In your own programs, you should be aware of the declared size of an array and avoid overrunning the bounds of the array. For more information about this programming mistake, see topic .
It is possible to assign the address of a string to a pointer:
strptr = aline;
Notice that both strptr and aline point to the same string. There's one object in memory, but it has two different names. If aline changes, the same change occurs in the string referenced by strptr, because they're the same string. Below, the word "dog" is added to the end of the string aline:
strcat( aline, "dog" );
The strcat function concatenates one string to the end of a second string. In the line above, both aline and the string referenced by strptr have been changed from "Another line." to "Another line.dog".
Now four printf statements execute:
printf( "A line of text." );
printf( aline );
printf( more );
printf( strptr );
The screen should look like this:
A line of text.Another line.dogAnother line.Another line.dog
To the first printf we passed a string constant. To the other three we passed names of strings. Concatenating aline and "dog" also affected the string referenced by strptr, because they both point to the same string in memory. The contents of more weren't affected, however, because the strcpy function makes a complete and unique copy of the source string at the memory location referenced by more.
Unfortunately, the strings ran together. As we saw in Chapter 1, “Anatomy of a C Program,” printf is unlike QuickBasic's PRINT command or Pascal's Writeln procedure in one respect: it does not automatically move the cursor to the beginning of the next line. You need to include the newline character (\n), which is one of a series of available escape codes. (For more information, see “Character and String Constants”.) The program below includes a few examples of escape codes, each of which begins with the backslash character:
/* PRTESC.C: Print escape characters \",\n, and \t. */
#include <stdio.h>
#include <string.h>
main()
{
char b[80];
int i,j;
strcpy( b, "and seven years ago\n" );
printf( "\"Four score\n" );
printf( b );
printf( "\tone tab\n\t\ttwo tabs\n\t\t\tthree tabs\n" );
i = sizeof( b );
j = strlen( b );
printf( "Size is %d\nLength is %d.\n", i, j );
}
If you compile and run the PRTESC.C program, the following text prints on the screen:
"Four score
and seven years ago
one tab
two tabs
three tabs
Size is 80
Length is 20.
To print a newline character in a string, type a backslash and the letter n (\n). For a quotation mark, use \". For tabs, use \t. Escape sequences can appear anywhere within a string:
printf( "\tone tab\n\t\ttwo tabs\n\t\t\tthree tabs\n" );
You'll find complete lists of escape characters in Appendix A, “C Language Guide,” and in online help.
The last call to printf in PRTESC.C provides two pieces of information: the size of the character array and the length of the string inside the array.
The variable b was declared to be an 80-character array, but the string inside b contains only 20 characters; it holds 19 letters plus one newline character. Although typing \n takes two characters, it's stored in memory as one character— the ASCII value 10. As we'll see later in this chapter, the newline character is sometimes expanded to two characters (a carriage return and a linefeed) when it is written to disk. But while it's in memory, it's a single character.
Summary: The sizeof operator examines array size; the strlen function returns the length of a string.
There are two methods available to find the size of arrays and strings. The sizeof operator returns the size (in bytes) of an identifier or type. The string-handling function strlen counts the number of characters in a string, up to but not including the null that marks the end of the string:
i = sizeof( b );
j = strlen( b );
printf( "Size is %d\nLength is %d.\n", i, j );
The final line of the program PRTESC.C prints out two integer values, which follow the format string. When printf evaluates the format string, it substitutes the two values for the %d specifiers:
Size is 80
Length is 20
The sizeof operator is part of the C language. In this example, it evaluates to the value 80, which is the size of the array. The strlen function is a library function for measuring strings (up to, but not including the null at the end). It returns a 20 because that's the length of the string.