We use printf to show messages or results on the screen. It helps us communicate what the program is doing.
Using printf for output in C
Introduction
Syntax
C
printf("format string", values...);The format string tells printf what to print and how.
Placeholders like %d or %s are used to insert values.
Examples
C
printf("Hello, world!\n");%d for an integer.C
printf("I have %d apples.\n", 5);
%s for a string.C
printf("My name is %s.\n", "Anna");
%.2f.C
printf("Pi is approximately %.2f.\n", 3.14159);
Sample Program
This program prints a greeting, then shows the age using %d to insert the number, and finally prints a closing message.
C
#include <stdio.h> int main() { int age = 25; printf("Hello!\n"); printf("I am %d years old.\n", age); printf("Nice to meet you!\n"); return 0; }
Important Notes
Always include \n at the end of your message to move to the next line.
Make sure the placeholders match the type of the values you provide (e.g., %d for int, %s for string).
printf does not add a new line automatically.
Summary
printf is used to show messages on the screen.
Use placeholders like %d, %s, and %f to insert values.
Remember to add \n to start a new line after printing.
Practice
1. What does the
printf function do in C?easy
Solution
Step 1: Understand the purpose of printf
Theprintffunction is used to show messages or values on the screen in C programs.Step 2: Compare options with the function's purpose
Only It displays text or values on the screen. correctly describesprintfas displaying output. Other options describe different functions.Final Answer:
It displays text or values on the screen. -> Option CQuick Check:
printf = output display [OK]
Hint: printf always prints output to the screen [OK]
Common Mistakes:
- Confusing printf with input functions like scanf
- Thinking printf saves data to files
- Assuming printf does calculations
2. Which of the following is the correct syntax to print an integer variable
num using printf?easy
Solution
Step 1: Identify the correct placeholder for integer
The placeholder for integers inprintfis%d.Step 2: Match syntax with variable and placeholder
printf("%d", num); uses%dand passesnumcorrectly. Other options use wrong placeholders or incorrect syntax.Final Answer:
printf("%d", num); -> Option AQuick Check:
Integer placeholder = %d [OK]
Hint: Use %d for integers in printf [OK]
Common Mistakes:
- Using %s for integers
- Passing variable without placeholder
- Putting variable name in quotes
3. What is the output of this code?
int x = 5;
printf("Value: %d\n", x);
medium
Solution
Step 1: Understand the printf statement
The placeholder%dis replaced by the value ofx, which is 5.Step 2: Consider the newline character
The\nmoves the cursor to the next line after printing.Final Answer:
Value: 5 -> Option DQuick Check:
Placeholder %d replaced by 5 [OK]
Hint: Placeholders show variable values in output [OK]
Common Mistakes:
- Printing the placeholder text instead of value
- Ignoring newline character
- Printing variable name instead of value
4. Find the error in this code snippet:
int a = 10;
printf("The value is %d", a)
medium
Solution
Step 1: Check syntax of printf statement
Theprintfline is missing a semicolon at the end, which is required in C.Step 2: Verify other parts
The placeholder%dis correct for integer, and variableais declared properly.Final Answer:
Missing semicolon after printf statement. -> Option BQuick Check:
Every statement ends with ; in C [OK]
Hint: Check for missing semicolons after statements [OK]
Common Mistakes:
- Forgetting semicolon after printf
- Using wrong placeholder for data type
- Not declaring variables before use
5. You want to print a floating-point number
pi with 2 decimal places using printf. Which code is correct?hard
Solution
Step 1: Understand floating-point formatting
To print a float with 2 decimals, use%.2finprintf.Step 2: Check each option's correctness
printf("%.2f", pi); uses%.2fcorrectly. printf("%2f", pi); misses the dot, C has wrong syntax, D passes extra argument incorrectly.Final Answer:
printf("%.2f", pi); -> Option AQuick Check:
Use %.2f for 2 decimal float output [OK]
Hint: Use %.2f to limit float decimals to 2 [OK]
Common Mistakes:
- Omitting the dot before precision
- Passing extra arguments to printf
- Using wrong placeholder format
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
