The printf statement is your primary tool for sending data to standard output. In most development environments, that means your terminal screen. You can redirect that output to files or pipes, but for learning, you just need to know it appears on the display.
Here is a practical example to demonstrate how the function works in a real C program.
Writing the Code
Save the following code in a file named add.c.
Compiling and Running
You need to compile this before it runs. Use the GCC compiler with the -o flag to name your executable.
Run this command in your terminal:
gcc add.c -o add
Then execute the program:
./add
The output will be:
5 + 7 = 12
Breaking Down the Logic
The program does four distinct things before printing anything.
1. Variable Declaration
The line int a, b, c; creates three integer variables. Integers store whole numbers. They do not handle decimals or fractions.
2. Initialization
The next two lines assign values to the first two variables.
a = 5;
b = 7;
3. Assignment and Arithmetic
The line c = a + b; performs the calculation. The computer takes the value of a (5) and adds it to the value of b (7). The result is 12. This value is then stored in variable c. The equals sign here is technically the assignment operator. It does not mean “equal to” in the mathematical sense. It means “put the result of the right side into the variable on the left.”
4. Formatting the Output
This is where printf takes over. The function prints the string "5 + 7 = 12" to the screen. It does this by matching placeholders in the format string to the variables listed at the end.
The format string contains three %d placeholders.
– The first %d corresponds to a.
– The second %d corresponds to b.
– The third %d corresponds to c.
C replaces each placeholder with the current value of its matching variable. The plus signs, equals sign, and spaces are hardcoded into the format string. They appear exactly where you typed them. The \n at the end forces a new line after the output.
Why This Matters
You might wonder why we don’t just print the numbers directly. Hardcoding printf("5 + 7 = 12\n"); works for static values. It fails completely when variables change. The %d specifier allows your code to remain flexible. You write the structure once. The variables fill in the blanks at runtime. This separation between format and data is what makes formatted output powerful.
The \n character is another detail often overlooked. Without




















