The Do-It-Yourself Option

I l @ ve RuBoard

The Do-It-Yourself Option

You aren't limited to the standard C library options for input and output. If you don't have these options or don't like them, you can prepare your own versions, building on getchar () and putchar () . Suppose you want a function like puts() that doesn't automatically add a newline. Listing 11.10 shows one way to do it.

Listing 11.10 The put1.c program.
 /* put1.c -- prints a string  without adding \n */ #include <stdio.h> void put1(const char * string) /* string not altered */ {     while (*string != ` 
  /* put1.c -- prints a string without adding \n */ #include <stdio.h> void put1(const char * string) /* string not altered */ { while (*string != `\0') putchar(*string++); }  
') putchar(*string++); }

The char pointer string initially points to the first element of the called argument. Because this function doesn't change the string, use the const modifier. After the contents of that element are printed, the pointer increments and points to the next element. This goes on until the pointer points to an element containing the null character. Remember, the higher precedence of ++ compared to * means that putchar ( *string++ ) prints the value pointed to by string but increments string itself, not the character it points to.

You can regard put1.c as a model for writing string-processing functions. Because each string has a null character marking its end, you don't have to pass a size to the function. Instead, the function processes each character in turn until it encounters the null character.

A somewhat longer way of writing the function is to use array notation:

 int i = 0; while (string [i]!= ` 
  int i = 0; while (string [i]!= `\0') putchar(string[i++]);  
') putchar(string[i++]);

This involves an additional variable for the index.

Many C programmers would use the following test for the while loop:

 while (*string) 

When string points to the null character, *string has the value , which terminates the loop. This approach certainly takes less typing than the previous version. If you are not familiar with C practice, it is less obvious. It could result in more efficient code, depending on the compiler. Regardless of its merits, this idiom is widespread.

Note

Why does Listing 11.10 use const char * string rather than const char string[] as the formal argument? Technically, the two are equivalent, so either form will work. One reason to use bracket notation is to remind the user that the function processes an array. With strings, however, the actual argument can be the name of an array, a quoted string, or a variable that has been declared as type char * . Using const char * string reminds you that the actual argument isn't necessarily an array.


Suppose you want a function like puts() that also tells you how many characters are printed. As Listing 11.11 demonstrates , it's easy to add that feature.

Listing 11.11 The put2.c program.
 /* put2.c -- prints a string and counts characters */ #include <stdio.h> int put2(const char * string) {     int count = 0;     while (*string != ` 
  /* put2.c -- prints a string and counts characters */ #include <stdio.h> int put2(const char * string) { int count = 0; while (*string != `\0') { putchar(*string++); count++; } putchar(`\n'); /* newline not counted */ return(count); }  
') { putchar(*string++); count++; } putchar(`\n'); /* newline not counted */ return(count); }

The following call prints the string pizza :

 put1("pizza"); 

The next call also returns a character count that is assigned to num , in this case, the value 5 .

 num = put2("pizza"); 

Listing 11.12 presents a driver using put1() and put2() and showing nested function calls.

Listing 11.12 The put_put.c program.
 #include <stdio.h> void put1(const char *); int put2(const char *); int main(void) {     put1("If I'd as much money");     put1(" as I could spend,\n");     printf("I count %d characters.\n",        put2("I never would cry old chairs to mend."));     return 0; } 

We assume you'll place the put1() and put2() functions in the same file. Note that we used #include stdio.h because on our system, putchar() is defined there, and our new functions use putchar() .

Hmmm, we are using printf() to print the value of put2() , but in the act of finding the value of put2() , the computer first must execute that function, causing the string to be printed. Here's the output:

 If I'd as much money as I could spend, I never would cry old chairs to mend. I count 37 characters. 
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