C Output (printf)
C output means sending information from your program to a place the user can see, usually the terminal. In C, the most common output function is printf, which writes text and formatted values to standard output. Learning printf early matters because it is how you show results, debug simple programs, and make command-line programs communicate clearly.
Overview: How printf Works
The printf function belongs to the standard input/output library, declared in <stdio.h>. When you write printf("Hello\n");, your program passes a string literal to printf. A string literal is a sequence of characters stored by the program, ending internally with a null character so C library functions can find where the text stops.
printf writes to standard output, often called stdout. In a normal terminal run, stdout appears on the screen. In other situations, the operating system can redirect it to a file or pipe it into another program. Your C code can still call printf the same way; the environment decides where standard output goes.
The name printf means print formatted. It can print plain text, but it can also substitute values into the text using format specifiers such as %d for an int, %s for a string, and %.2f for a floating-point value rounded to two digits after the decimal point. This lesson introduces that idea gently; a later format specifier lesson goes deeper.
A call to printf returns an int: the number of characters printed, or a negative value if an output error occurs. Beginners usually ignore that return value, but knowing it exists helps explain that printf is a real function call, not a special language command.
Syntax
printf("text and format specifiers\n");
| Part | Meaning |
|---|---|
printf |
The standard library function that writes formatted output. |
"text and format specifiers\n" |
The format string. It contains literal text, escape sequences such as \n, and optional format specifiers. |
| Extra arguments | Values added after the format string when it contains specifiers such as %d or %s. |
; |
Ends the statement, as with other C statements. |
To use printf, include <stdio.h> before main. The header provides the function declaration so the compiler can check how you are calling it.
Examples
Printing One Line
#include <stdio.h>
int main(void)
{
printf("Hello from C!\n");
return 0;
}
Output:
Hello from C!
This complete program includes <stdio.h>, starts execution in main, calls printf, and returns 0 to report success. The characters \n represent one newline character, so the terminal cursor moves to the next line after the message.
Printing Several Lines
#include <stdio.h>
int main(void)
{
printf("C is a compiled language.\n");
printf("printf writes to standard output.\n");
printf("Newlines keep output readable.\n");
return 0;
}
Output:
C is a compiled language.
printf writes to standard output.
Newlines keep output readable.
Each call prints exactly the characters requested. printf does not automatically add spaces or line breaks. The output has three lines only because each format string ends with \n.
Building One Line from Multiple Calls
#include <stdio.h>
int main(void)
{
printf("C ");
printf("output ");
printf("can be built in parts.\n");
return 0;
}
Output:
C output can be built in parts.
Because the first two calls do not contain \n, the next output continues on the same line. Notice that the spaces after C and output are ordinary printed characters; without them, the words would run together.
Printing Values with Basic Format Specifiers
#include <stdio.h>
int main(void)
{
int lessons = 4;
double progress = 37.5;
printf("Completed lessons: %d\n", lessons);
printf("Course progress: %.1f%%\n", progress);
return 0;
}
Output:
Completed lessons: 4
Course progress: 37.5%
In the first call, %d is replaced by the value of the int variable lessons. In the second call, %.1f prints the double value with one digit after the decimal point. To print an actual percent sign, write %% in the format string.
How It Works Step by Step
- The compiler sees
#include <stdio.h>and reads the declaration forprintf. - The compiler checks that
printfis called like a function and that the program is syntactically valid. - At runtime, execution enters
mainand reaches theprintfstatement. - The address of the format string is passed to
printf. If there are additional values, they are passed after it. printfscans the format string from left to right. Ordinary characters are written as-is. Escape sequences in the string literal, such as\n, have already become their actual character values. Format specifiers tellprintfto read the next argument and convert it to text.- The resulting characters are sent to
stdout. On many systems, terminal output is line-buffered, which means a newline often causes buffered text to appear immediately.
This step-by-step view also explains a major safety rule: the format string and the extra arguments must agree. If the format string says %d, printf expects an int. If the provided value has the wrong type or is missing, the behavior is not reliable.
Common Mistakes
Forgetting the Newline
printf does not add a newline automatically. If you omit \n, the next prompt or output may appear on the same line. The corrected version includes the newline explicitly.
#include <stdio.h>
int main(void)
{
printf("Ready\n");
printf("Set\n");
printf("Go\n");
return 0;
}
Output:
Ready
Set
Go
Using the Wrong Quotes
Text for printf belongs in double quotes, such as "Hello". Single quotes are for a single character constant, such as 'A'. Use double quotes when printing a string of text.
Mismatching Format Specifiers and Values
A format specifier is a promise about the type of the next argument. For example, use %d with an int and %f with a double. A corrected program keeps the specifiers and values in the same order.
#include <stdio.h>
int main(void)
{
int count = 3;
double price = 19.99;
printf("Count: %d\n", count);
printf("Price: %.2f\n", price);
return 0;
}
Output:
Count: 3
Price: 19.99
Forgetting to Escape Special Characters
Some characters need escaping inside a string literal. Use \" to print a double quote, \\ to print a backslash, and \n for a newline.
#include <stdio.h>
int main(void)
{
printf("She said, \"Learn C.\"\n");
printf("Path example: C:\\tools\\bin\n");
return 0;
}
Output:
She said, "Learn C."
Path example: C:\tools\bin
Best Practices
- Include
<stdio.h>whenever you useprintf. - End user-facing lines with
\nunless you intentionally want more output on the same line. - Keep format strings simple and readable; split long output across multiple calls when it improves clarity.
- Match every format specifier with an argument of the correct type and in the correct order.
- Use
%%when you need to print a percent sign. - Compile with warnings enabled, such as
-Wall -Wextra, because compilers can catch many incorrectprintfcalls.
Practice Exercises
- Write a program that prints your name on one line and your favorite programming topic on the next line.
- Write a program that uses three separate
printfcalls to produce one sentence on a single line. Be careful with spaces. - Create two variables, one
intand onedouble, then print both using suitable format specifiers. Hint: try printing thedoublewith two digits after the decimal point.
Summary
printfwrites formatted output tostdout, which usually means the terminal.<stdio.h>provides the declaration forprintf.\nis a newline escape sequence;printfdoes not add it automatically.- Format specifiers such as
%d,%s, and%.2flet you insert values into output. - The format string and the extra arguments must match, or the program can behave incorrectly.
