Course contentsShow
← C
Lesson 700 of 1,21910. Input and OutputFree lesson

Printing strings with puts

Printing strings with puts

What you'll learn: How to display a line of text on the screen using the simple and efficient puts function.

The basic idea

When you first started C, you likely used printf to show text on the screen. While printf is powerful, it is also a bit of a "heavy lifter"—it’s designed to handle complex formatting like mixing numbers and text together.

If all you want to do is print a plain string of text followed by a new line, C offers a specialized tool: puts. Think of printf like a high-end multi-tool, while puts is like a dedicated hammer. It does one specific job very well.

How it works

The puts function stands for "put string." Its behavior is very predictable: it takes the string you give it, prints it to the console, and then automatically adds a newline character (\n) at the end.

This automatic newline is a key difference. With printf, you have to remember to add \n manually if you want the cursor to move to the next line. With puts, it’s handled for you.

Here is a quick look at how it appears in code:

#include <stdio.h>

int main() {
    // No need for \n at the end
    puts("Hello, world!");
    puts("This text will be on a new line.");
    
    char message[] = "C programming is logical.";
    puts(message);
    
    return 0;
}

When to use puts

Because puts is simpler than printf, it is generally faster and uses less memory. You should reach for puts when:

  1. You are printing a simple message that doesn't require variables (like numbers or characters) mixed in.
  2. You want the output to end with a new line automatically.

An important note: puts only accepts a single argument (the string). You cannot use "format specifiers" like %d or %f inside it. If you try to do puts("Score: %d", score);, it will literally print the characters %d rather than the actual number. For that, you still need your trusty printf.

Key takeaway

Use puts for simple, text-only output because it is efficient and automatically handles moving the cursor to the next line.