printf() Function in C
printf() is the function you will use more than any other in C. It prints output on the screen: text, numbers, characters, anything.
You have already used it in many programs. In this lesson you will learn how it really works, how to print many values in one line, and what printf() returns (a favourite interview question).
What You Will Learn
- What
printf()does and where it comes from. - The syntax of
printf(). - How to print text, variables and expressions.
- How values are matched with format specifiers.
- The return value of
printf().
printf() Function in C Explained
Exam definition: printf() is a library function declared in stdio.h that prints formatted output on the screen. It returns the number of characters printed.
- The name means print formatted.
- It is a formatted output function: it converts values into text using format specifiers like
%d. - You must write
#include <stdio.h>to use it.
Memory trick: printf = print + format. The text in quotes is the format (a template); the values after the comma fill the blanks, like filling a form.
Syntax of printf() in C
printf("format string", value1, value2, ...);
| Part | Meaning |
|---|---|
"format string" | The text to print. It can contain format specifiers (%d, %f...) and escape sequences (\n). |
value1, value2, ... | Variables or expressions. One for each format specifier, in the same order. |
| Return value | The number of characters printed (an int). |
Example 1: Printing Text, Variables and Expressions
#include <stdio.h>
int main(void)
{
int apples = 5, price = 20;
char size = 'L';
printf("Welcome to the fruit shop!\n");
printf("Apples: %d\n", apples);
printf("Size: %c, Price: %d\n", size, price);
printf("Total: %d rupees\n", apples * price);
return 0;
}
Output
Welcome to the fruit shop! Apples: 5 Size: L, Price: 20 Total: 100 rupees
Code Explanation
printf("Welcome to the fruit shop!\n");: only text, no values.\nmoves to the next line.printf("Apples: %d\n", apples);:%dis replaced by the value ofapples(5).printf("Size: %c, Price: %d\n", size, price);: two specifiers, two values. The first%cgetssize, the second%dgetsprice.apples * price: you can pass an expression. It is calculated first (100), then printed.
How printf() Works
printf()reads the format string from left to right.- Normal characters are copied to the output as they are.
- When it finds
%, it takes the next value from the list and prints it in that format. - When it finds
\n, it moves to the next line. - Output first goes into a buffer (a waiting area) and is shown on the screen when a newline is printed or the program ends. That is why ending lines with
\nis a good habit.
Example 2: Return Value of printf()
printf() returns the number of characters it printed. This program prints that number:
#include <stdio.h>
int main(void)
{
int count = printf("Hello");
printf("\nCharacters printed: %d\n", count);
printf("%d\n", printf("C is fun"));
return 0;
}
Output
Hello Characters printed: 5 C is fun8
In the last line, the inner printf("C is fun") runs first and prints 8 characters (spaces count). Then the outer printf prints its return value, 8, right after it.
Important Points to Remember
printf()is declared instdio.h.- It is a formatted output function.
- Values match format specifiers left to right.
- It returns the number of characters printed.
- End lines with
\n.
Common Mistakes Beginners Make
| Mistake | What happens | Fix |
|---|---|---|
printf("%d", &x); | Prints an address, not the value | No & in printf: printf("%d", x); |
printf("Sum = %d\n"); (value missing) | Garbage value printed | printf("Sum = %d\n", sum); |
printf('Hello'); | Error: single quotes | Use double quotes |
Printf("Hi"); | Error: C is case-sensitive | printf in lowercase |
Missing #include <stdio.h> | Error or warning about printf | Include the header |
printf() Practice Questions
- What is
printf()? In which header file is it declared? - What does
printf()return? - What is the output of
printf("%d", printf("ABC"));? - Why don't we use
&with variables in printf? - Write a printf statement to print:
Marks: 90/100using a variablemarks.
Show answers
- A library function that prints formatted output on the screen. Declared in
stdio.h. - The number of characters printed.
ABC3- printf needs the value to print, not the address. (scanf needs the address because it stores a value.)
printf("Marks: %d/100\n", marks);
Coding Exercise
Write a program that stores a book's title (one letter code 'C'), pages (320) and price (450.75), and prints them in one line like: Book C has 320 pages and costs Rs. 450.75
Show solution
#include <stdio.h>
int main(void)
{
char code = 'C';
int pages = 320;
float price = 450.75f;
printf("Book %c has %d pages and costs Rs. %.2f\n", code, pages, price);
return 0;
}
Book C has 320 pages and costs Rs. 450.75
Quick Quiz: printf() in C (MCQ)
-
printf()is declared in:A)
conio.hB)stdlib.hC)stdio.hD)math.h -
What does
printf()return?A) Nothing B) 0 always C) Number of characters printed D) The first value
-
Output of
printf("%d", printf("Hi"));A) Hi B) 2 C) Hi2 D) 2Hi
-
printf()is which type of function?A) Formatted input B) Formatted output C) Unformatted output D) File function
-
Which is correct?
A)
printf("%d", &n);B)printf(%d, n);C)printf("%d", n);D)printf("n");to print the value
Show answers
- C:
stdio.h - C: the number of characters printed.
- C:
Hi2 - B: formatted output.
- C:
printf("%d", n);
Quick Revision for Exams and Interviews
Read this before your exam. The highlighted words are the answer.
Must-remember facts
| Full form | print formatted |
|---|---|
| Header file | stdio.h |
| Type | Formatted output function |
| Syntax | printf("format", values...); |
| Returns | Number of characters printed |
& needed? | No (only in scanf) |
Frequently asked questions
- What is the return type and value of printf()?
- It returns an
int: the number of characters printed. - Why is & not used in printf?
- printf only reads the value; it does not need to store anything, so it needs the value, not the address.
- What is the output of printf("%d", printf("Hello"))?
- Hello5: the inner printf prints Hello and returns 5.
- Is printf a keyword?
- No. It is a library function.
Remember
- printf = print + format: template + values that fill the blanks.
- Returns count of characters; spaces and
\ncount too.
Next Topic
Next lesson: scanf() Function in C. You will learn how to take input from the user with scanf().