How to Use the printf() in C Programming

By Dan Gookin

The puts() function is but one of many functions in the C programming language that sends text to the standard output device. A second, more popular and versatile function is printf(). It too displays information to the standard output device, but with a few more bells and whistles.

How to display text with printf()

On the surface, the printf() function looks and works a lot like puts(), displaying text to the screen. But printf() is far more potent and capable, and you’ll probably use it as the primary text-output function in your C code.

USING PRINTF TO DISPLAY TEXT

#include <stdio.h>
int main()
{
  printf("I have been a stranger in a strange land.");
  return(0);
}

Exercise 1: Eagerly create a new project in Code::Blocks, named ex0411. Type the source code for main.c as shown in Using printf to Display Text. Check your typing carefully because you’re using a new function, printf(), to display text. Save. Build. Run.

The output should look familiar and expected, although there’s one tiny difference. If you can spot it, great job. (Don’t worry about fixing the problem yet.) If you can’t, just proceed with Exercise 2.

Exercise 2: Create a new project, ex0412. Use the printf() function to create the same output as found in the listing Displaying Two Lines of Text. Use the full nursery rhyme:

Hickory, dickory, dock,

The mouse ran up the clock.

The clock struck one,

The mouse ran down,

Hickory, dickory, dock.

Don’t worry if the output doesn’t look right.

DISPLAYING TWO LINES OF TEXT

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

Basics of the printf() function in C programming

The printf() function sends a formatted stream of text to the standard output device. The official format is a bit overwhelming:

#include <stdio.h>
int printf(const char *restrict format, ...);

Don’t let your eyeballs pop out of your head. Instead, consider the abbreviated format:

printf("text");

In this definition, text is a string of text wedged between double quotes.

The printf() function requires the stdio.h header file.

The name printf() means print formatted, and the function really shows its horsepower in displaying formatted output. The print part of the name hails back to the days when C programs sent their output primarily to printers, not to video displays.

Differences between puts() and printf()

Unlike the puts() function, the printf() function doesn’t tack a newline character at the end of its output. A newline is the character that ends a line of text and directs the terminal to display any following text on the next line — the “new” line.

The following puts() function displays the text Goodbye, cruel world on a line by itself:

puts("Goodbye, cruel world");

Any text displayed after the preceding statement appears on the line following it.

The following printf() function displays the text Goodbye, cruel world:

printf("Goodbye, cruel world");

After displaying the text, the cursor waits at the space after the d in world. Any additional text that’s displayed appears on the same line, which is what you see if you complete Exercise 4-12:

Hickory, dickory, dock,The mouse ran up the clock.The clock struck one,The mouse ran down,Hickory, dickory, dock.

The program runs, and it does exactly what you direct the computer to do, albeit without knowing how printf() works ahead of time. But the results most likely aren’t what you intended.

To make the printf() function display text on a line by itself, insert the newline character into the text string. Don’t bother looking for the newline character on the keyboard; no, it’s not the Enter key. You can type the newline character only by using a C language escape sequence.

Escape sequences in C programming

To reference certain characters that you cannot type into your source code, the C language uses something called an escape sequence. The escape sequence allows you to direct the compiler to temporarily suspend its acceptance of what you’re typing and read in special characters and codes.

The standard escape sequence uses the backslash character followed by a second character or symbol; for example:

n

That’s the escape sequence for the newline character. The compiler reads both the backslash and the symbol that follows it as a single character, interpreting that character as one that you can’t type at the keyboard, such as the Tab key or Enter key or characters that may foul up the source code, such as a double quote.

Escape Sequence Character It Produces
a Bell (“beep!”)
b Backspace, non-erasing
f Form feed or clear the screen
n Newline
r Carriage return
t Tab
v Vertical tab
\ Backslash character
? Question mark
Single quote