Solution for Exercise 19-14

ex1914

#include <stdio.h>

int main()
{
    char sample[] = "From whence cometh my help?\n";
    char *ps;

    ps = sample;        /* initialize the pointer */

    while(putchar(*ps++))
        ;
    return(0);
}

Output

From whence cometh my help?

Notes

* That single while statement at Line 10 shows how you can use a pointer to output a string.

* Here is the progression of how the while loop's statements are eliminated. First:

Then, eliminate the null character comparison:

The next step is to move the putchar() function up into the while loop's comparison, though it's not shown in any Exercise file:

This code can also be expressed in this manner:

And finally:

* As I wrote in the book, the putchar() function returns the value displayed. At the end of the string, the null character \0 is encountered. So the while loop terminates just as it did before.

* The while loop's semicolon is set on a line by itself (Line 11) to avoid a compiler warning.