📖 Scenario: You are learning how to show messages on the screen using the printf function in C. This is like writing a note on a board so others can see it.
🎯 Goal: You will write a simple C program that uses printf to display a greeting message.
📋 What You'll Learn
Create a variable to hold a name
Create a variable to hold an age
Use printf to show a message including the name and age
💡 Why This Matters
🌍 Real World
Displaying messages is a basic need in many programs, like showing user information or status updates.
💼 Career
Knowing how to output text is essential for debugging and user interaction in software development.
Progress0 / 4 steps
1
Create variables for name and age
Create a variable called name of type char[] and set it to "Alice". Create an integer variable called age and set it to 30.
C
Hint
Use char name[] = "Alice"; to create the name variable and int age = 30; for the age.
2
Prepare the message format
Create a string variable called message and set it to "Hello, my name is %s and I am %d years old." This will be used as the format for printf.
C
Hint
Use char message[] = "Hello, my name is %s and I am %d years old."; to create the message format string.
3
Use printf to display the message
Use printf with the message variable and pass name and age as arguments to show the full greeting.
C
Hint
Use printf(message, name, age); to print the message with the variables.
4
Print a new line after the message
Add a printf statement to print a new line character \n after the greeting message so the output looks clean.
C
Hint
Use printf("\n"); to add a new line after the message.
Practice
(1/5)
1. What does the printf function do in C?
easy
A. It reads input from the keyboard.
B. It saves data to a file.
C. It displays text or values on the screen.
D. It performs mathematical calculations.
Solution
Step 1: Understand the purpose of printf
The printf function 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 describes printf as displaying output. Other options describe different functions.
Final Answer:
It displays text or values on the screen. -> Option C
Quick 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
A. printf("%d", num);
B. printf("%s", num);
C. printf(num);
D. printf("num");
Solution
Step 1: Identify the correct placeholder for integer
The placeholder for integers in printf is %d.
Step 2: Match syntax with variable and placeholder
printf("%d", num); uses %d and passes num correctly. Other options use wrong placeholders or incorrect syntax.
Final Answer:
printf("%d", num); -> Option A
Quick 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
A. x
B. Value: %d
C. Value:
D. Value: 5
Solution
Step 1: Understand the printf statement
The placeholder %d is replaced by the value of x, which is 5.
Step 2: Consider the newline character
The \n moves the cursor to the next line after printing.
Final Answer:
Value: 5 -> Option D
Quick 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
A. Wrong placeholder used for integer.
B. Missing semicolon after printf statement.
C. Variable 'a' is not declared.
D. printf cannot print integers.
Solution
Step 1: Check syntax of printf statement
The printf line is missing a semicolon at the end, which is required in C.
Step 2: Verify other parts
The placeholder %d is correct for integer, and variable a is declared properly.
Final Answer:
Missing semicolon after printf statement. -> Option B
Quick 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
A. printf("%.2f", pi);
B. printf("%2f", pi);
C. printf("%f.2", pi);
D. printf("%f", pi, 2);
Solution
Step 1: Understand floating-point formatting
To print a float with 2 decimals, use %.2f in printf.
Step 2: Check each option's correctness
printf("%.2f", pi); uses %.2f correctly. printf("%2f", pi); misses the dot, C has wrong syntax, D passes extra argument incorrectly.
Final Answer:
printf("%.2f", pi); -> Option A
Quick 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 unknownstrongwhile earning +1 Grip toward your certificate.