C Output (printf)

C output means displaying information from a program on the screen. In C, the most common beginner-friendly function for output is printf, which prints formatted text.

The printf Function

The printf function is part of the standard input/output library. To use it, include <stdio.h> at the top of the program.

A simple printf call places the text you want to print inside double quotes. The call is a statement, so it ends with a semicolon.

#include <stdio.h>

int main(void)
{
    printf("Hello from C!\n");
    return 0;
}

Output:

Hello from C!

How It Works

The line #include <stdio.h> gives the program the declaration for printf. Without it, the compiler may not know how printf should be used.

The text inside the double quotes is called a string literal. printf writes that text to standard output, which usually means the terminal or console where the program is running.

The characters \n do not print as a backslash and the letter n. Together, they represent a newline character. A newline moves the cursor to the beginning of the next line.

Printing Multiple Lines

You can call printf more than once. Each call prints exactly what you ask it to print. If you want each message on its own line, include \n where the line should end.

#include <stdio.h>

int main(void)
{
    printf("C is a compiled language.\n");
    printf("printf displays output.\n");
    printf("Newlines make output easier to read.\n");
    return 0;
}

Output:

C is a compiled language.
printf displays output.
Newlines make output easier to read.

Without A Newline

If a printf call does not contain \n, the next output continues on the same line. This can be useful when you want to build one line from several calls.

#include <stdio.h>

int main(void)
{
    printf("C ");
    printf("output ");
    printf("uses printf.\n");
    return 0;
}

Output:

C output uses printf.

Common Beginner Mistakes

  • Use double quotes for printed text, such as "Hello".
  • End each printf statement with a semicolon.
  • Spell printf in lowercase. C is case-sensitive.
  • Include <stdio.h> before using printf.
  • Use \n when you want the next output to start on a new line.

The key idea is that printf sends text from your C program to the screen. Next, you will build on this by learning how C comments help explain code.