Nearby lessons

24 of 124

C - Output

Printing to the screen in C using printf(), puts() and putchar(). Learn format specifiers, field width, decimal precision and why output sometimes appears in the wrong order.

The Three Output Functions

All three live in <stdio.h> and each has one job:

FunctionPrintsAdds newline?Formats values?
printf()AnythingNoYes
puts()A string onlyYesNo
putchar()One characterNoNo
In simple words: use printf when you need to mix text with variables, puts for a plain message, and putchar for a single character.

printf() — Formatted Output

Inside the quotes you write ordinary text plus format specifiers that start with %. Each specifier is replaced, in order, by the values you list after the comma:

Example02
CCode Cell
1#include <stdio.h>
2 
3int main()
4{
5 int age = 25;
6 float height = 5.9;
7 char grade = 'A';
8 
9 printf("Age : %d\n", age);
10 printf("Height : %f\n", height);
11 printf("Grade : %c\n", grade);
12 printf("%s is %d years old.\n", "Rahul", age);
13 return 0;
14}
Output
Age    : 25
Height : 5.900000
Grade  : A
Rahul is 25 years old.

The Specifiers You Need Most

SpecifierTypeExample output
%dint25
%ffloat / double5.900000
%ccharA
%sstringRahul
%lfdouble3.141593
%uunsigned int4000000000
%%a literal % sign%
Mismatched specifiers print garbage. Writing printf("%d", 3.14) does not print 3 — it reinterprets the float's bits as an integer and prints nonsense. The specifier must match the value's type.

Controlling Decimals and Width

Between the % and the letter you can add a width and a precision. This is how you line up columns and print money correctly:

Example04
CCode Cell
1#include <stdio.h>
2 
3int main()
4{
5 float price = 1234.5678;
6 
7 printf("[%f]\n", price); // default 6 decimals
8 printf("[%.2f]\n", price); // 2 decimals
9 printf("[%.0f]\n", price); // no decimals
10 printf("[%12.2f]\n", price); // width 12, right aligned
11 printf("[%-12.2f]\n", price); // width 12, LEFT aligned
12 printf("[%05d]\n", 42); // pad with zeros
13 return 0;
14}
Output
[1234.567749]
[1234.57]
[1235]
[     1234.57]
[1234.57     ]
[00042]

puts() and putchar()

puts() is shorter than printf for plain messages because it supplies the newline itself:

Example05
CCode Cell
1#include <stdio.h>
2 
3int main()
4{
5 puts("Line one"); // newline added for you
6 puts("Line two");
7 
8 putchar('H');
9 putchar('i');
10 putchar('\n'); // newline must be explicit here
11 return 0;
12}
Output
Line one
Line two
Hi

Printing Special Characters

Some characters cannot be typed directly inside quotes, so you write an escape sequence instead:

Example06
CCode Cell
1#include <stdio.h>
2 
3int main()
4{
5 printf("Tab\there\n");
6 printf("A \"quoted\" word\n");
7 printf("Backslash: \\ \n");
8 printf("100%% complete\n");
9 return 0;
10}
Output
Tab	here
A "quoted" word
Backslash: \
100% complete

Why Output Sometimes Appears Late

C does not write to the screen immediately. It collects characters in a buffer and flushes them when it sees a newline, when the buffer fills, or when the program ends.

That is why a prompt without \n can appear after you have already typed your answer:

Example07
CCode Cell
1#include <stdio.h>
2 
3int main()
4{
5 printf("Enter name: "); // no \n - may not appear yet
6 fflush(stdout); // force it out now
7 /* ... read input here ... */
8 printf("done\n");
9 return 0;
10}
Output
Enter name: done
📝 Key Takeaways
  • printf() prints formatted output; it needs a format specifier per value.
  • puts() prints a string and adds a newline automatically.
  • putchar() prints exactly one character.
  • Use %.2f to print two decimal places.
  • printf does not add a newline — you must write \n yourself.

🧠 Test Your Knowledge

4 Questions
Progress: 0 / 4