String Output

I l @ ve RuBoard

String Output

Again, we will use library functions. C has three standard library functions for printing strings. They are puts() , fputs() , and printf() .

The puts() Function

The puts() function is very easy to use. Just give it the address of a string for an argument. Listing 11.8 illustrates some of the many ways to do this.

Listing 11.8 The put_out.c program.
 /* put_out.c -- using puts() */ #include <stdio.h> #define DEF "I am a #defined string." int main(void) {   char str1[80] = "An array was initialized to me.";   char * str2 = "A pointer was initialized to me.";   puts("I'm an argument to puts().");   puts(DEF);   puts(str1);   puts(str2);   puts(&str1[4]);   puts(str2+4);   return 0; } 

The output is this:

 I'm an argument to puts(). I am a #defined string. An array was initialized to me. A pointer was initialized to me. rray was initialized to me. inter was initialized to me. 

Note that each string appears on its own line. Unlike printf() , puts() automatically appends a newline when it displays a string.

This example reminds you that phrases in double quotation marks are string constants and are treated as addresses. Also, the names of character array strings are treated as addresses. The expression &str1[4] is the address of the fifth element of the array str1 . That element contains the character `r' , and that is what puts() uses for its starting point. Similarly, str2+4 points to the memory cell containing the `i' of "pointer" , and the printing starts there.

How does puts() know when to stop? It stops when it encounters the null character, so there had better be one. Don't emulate the program in Listing 11.9!

Listing 11.9 The nono.c program.
 /* nono.c -- no! */ #include <stdio.h> int main(void) {     char c1 = `A';     char dont[] = {`W', `O', `W', `!' };     char c2 = `B';          puts(dont);   /* dont is not a string */     return 0; } 

Because dont lacks a closing null character, it is not a string, so puts() won't know where to stop. It will just keep printing from memory following dont until it finds a null somewhere. Here's a sample run:

 WOW!B$@ 

You may get different results, depending upon what is in your computer's memory when you run the program. There are usually lots of nulls in memory, and if you're lucky, puts() might find one soon, but don't count on it.

The fputs() Function

The fputs() function is the file-oriented version of gets() . The main differences are these:

  • The fputs() takes a second argument indicating the file to write to. You can use stdout (for standard out put), which is defined in stdio.h , as an argument to output to your display.

  • Unlike puts() , fputs() does not automatically append a newline to the output.

Note that gets() discards a newline on input, but puts() adds a newline on output. On the other hand, fgets() stores the newline on input, and fputs() doesn't add a newline on output. Suppose you want to write a loop that reads a line and echoes it on the next line. You can do this:

 char line[81]; while (gets(line))     puts(line); 

Recall that gets() returns the null pointer if it encounters end-of-file. The null pointer evaluates as zero, or false, so that terminates the loop. Or you can do this:

 char line[81]; while (fgets(line, 81, stdin))     fputs(line, stdout); 

With the first loop, the string in the line array is displayed on a line of its own because puts() adds a newline. With the second loop, the string in the line array is displayed on a line of its own because fgets() stores a newline. Note that if you mixed fgets() input with puts() output, you'd get two newlines displayed for each string. The point is that puts() is designed to work with gets() , and fputs() is designed to work with fgets() .

The printf() Function

We discussed printf() pretty thoroughly in Chapter 4, "Character Strings and Formatted Input/Output." Like puts() , it takes a string address as an argument. The printf() function is less convenient to use than puts() , but it is more versatile because it formats various data types.

One difference is that printf() does not automatically print each string on a new line. Instead, you must indicate where you want new lines. Therefore,

 printf("%s\n", string); 

has the same effect as

 puts(string); 

As you can see, the first form takes more typing. It also takes longer for the computer to execute. On the other hand, printf() makes it simple to combine strings for one line of printing. For example, the following statement combines Well , with the user 's name and a #defined character string, all on one line:

 printf("Well, %s, %s\n", name, MSG); 
I l @ ve RuBoard


C++ Primer Plus
C Primer Plus (5th Edition)
ISBN: 0672326965
EAN: 2147483647
Year: 2000
Pages: 314
Authors: Stephen Prata

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net