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 != `\0')
        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 ...

Get C Primer Plus®, Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.