Basic Output

The typical first program taught to anyone learning C (or any other programming language) is Hello, world! It’s fun, which sets up false expectations, but it’s also genius.

When that first program uses standard output, it generates results a beginner instantly sees on the screen. That feedback helps boost confidence. It also encourages play because, if you’re like most people, you next want to change the program’s output to say something else, perhaps your name or something filthy. At least that’s how I remember learning to program.

The statement presented in C is:

printf("Hello, world!\n");

Yet, the printf() function is perhaps one of the most complex functions for a beginner to learn. Another function, one that’s designed to put a string to standard output is:

puts("Hello, world");

This statement also has the benefit of not requiring the \n (newline) escape character, which saves a writer such as myself time because I don’t have to explain escape characters right away. (Though I do use printf() in my first program examine in Beginning Programming with C For Dummies.)

Both printf() and puts() are higher-level output functions. Internally, they rely upon lower level functions that send a single character at a time to standard output. These functions include:

  • putchar()
  • putc()
  • fputc()

Of these three, the only “real” one is fputc(). That’s because putchar() is usually implemented as a macro in the stdio.h header file. And the putc() function is pretty much identical to fputc(), though on some compilers it’s dedicated to sending a character to standard output. That leaves fputc() as the bottom-level character output function. Here’s the man page format:

int fputc(int c, FILE *stream);

The function deals with int variable, but it’s forgivable to use char variables instead.

Anyway, here is code that uses putchar() to display a string of text:

#include 

int main()
{
    char string[] = "Hello there, beautiful!\n";
    int index = 0;

    while(string[index])
    {
        putchar(string[index]);
        index++;
    }

    return(0);
}

The while loop spins through each character in the string[] array. putchar() sends the character to standard output. The index variable is incremented (Line 11), and the loop repeats until the string’s terminating character, \0 (null character) is encountered.

To use putc() instead of putchar(), you replace Line 10 with:

        putc(string[index],stdout);

On the compiler I’m using, putc() and fputc() are the same function, so you could stick an f before putc (above) and get the same output. The stdout constant represents the standard output device, the screen.

The bottom line is that at some point internally, the printf() and puts() function also use one of the single-character output functions to send their text to standard output. Yet you don’t have to use those functions, or craft your own string output function, because both printf() and puts() are available.

Leave a Reply