How to Add Text to a puts() Function in C Programming

By Dan Gookin

When you need to display another line of text, use your C programming skills to conjure up another puts() function in your source code, as shown in Displaying Two Lines of Text.

DISPLAYING TWO LINES OF TEXT

#include <stdio.h>
int main()
{
  puts("Hickory, dickory, dock,");
  puts("The mouse ran up the clock.");
  return(0);
}

The second puts() function does the same thing as the first. Also, because the first puts() function requires the stdio.h header file, there’s no need to include that line again; one mention does the job for any function that requires the header file.

Exercise 1: Create project ex0403 in Code::Blocks. Type the source code from Displaying Two Lines of Text into the editor. Save the project, compile, and run.

The output appears on two lines:

Hickory, dickory, dock,
The mouse ran up the clock.

As long as you use the puts() function and enclose the text in double quotes, the resulting program spits out that text, displaying it on the screen. Well, okay, puts() sends text to the standard output device. (Feel better, university sophomores?)

  • Include a header file to help prototype functions. The puts() function requires the stdio.h header.

  • The include compiler directive adds the header file into your source code. It’s formatted like this:

    #include <file.h>

    In this line, file represents the name of the header file. All header files sport the .h extension, which must be specified with the header filename in the angle brackets.

  • There’s no need to include the same header file more than once in a source code file.

  • Technically, you can include the header file several times. The compiler doesn’t balk over the duplication, it just keeps adding in the header files at compile time. That may add unnecessary bulk to the size of the object code file.

Exercise 2: Modify project ex0403 so that the entire nursery rhyme is displayed. Save the new project as ex0404. Here’s the full text:

Hickory, dickory, dock,

The mouse ran up the clock.

The clock struck one,

The mouse ran down,

Hickory, dickory, dock.

Yeah, it doesn’t really rhyme, so for a bonus, change the fourth line of the output so that it does rhyme!