📖 Scenario: You are creating a simple program to display different types of data about a product in a store. You will learn how to use format specifiers to print integers, floating-point numbers, and characters correctly.
🎯 Goal: Build a C program that stores product details and prints them using the correct format specifiers.
📋 What You'll Learn
Create variables with exact names and values
Use correct format specifiers for each variable type
Print the output exactly as instructed
💡 Why This Matters
🌍 Real World
Format specifiers are used in many programs to display data clearly, such as showing prices, IDs, or grades in reports or user interfaces.
💼 Career
Understanding format specifiers is essential for C programming jobs, especially in systems programming, embedded systems, and any application that requires formatted text output.
Progress0 / 4 steps
1
Create product data variables
Create three variables with these exact names and values: product_id as integer 101, price as float 29.99, and grade as character 'A'.
C
Hint
Use int for whole numbers, float for decimal numbers, and char for single characters.
2
Add a format string variable
Create a variable called format_str of type char* and set it to the string "ID: %d, Price: %.2f, Grade: %c".
C
Hint
Use char* to store a string and include the exact format specifiers for integer, float with 2 decimals, and character.
3
Print product details using format specifiers
Write a printf statement using format_str to print product_id, price, and grade in that order.
C
Hint
Use printf with the format string and variables in the correct order.
4
Display the output
Run the program and print the output exactly as: ID: 101, Price: 29.99, Grade: A
C
Hint
Make sure the output matches exactly, including spaces and punctuation.
Practice
(1/5)
1. Which format specifier is used to print an integer value in C using printf?
easy
A. %d
B. %f
C. %c
D. %s
Solution
Step 1: Understand integer data type printing
The %d specifier is used to print integer values in C.
Step 2: Identify other specifiers
%f is for floats, %c for characters, and %s for strings.
Final Answer:
%d -> Option A
Quick Check:
Integer print = %d [OK]
Hint: Use %d for integers in printf [OK]
Common Mistakes:
Using %f for integers
Confusing %c with %d
Using %s for numbers
2. Which of the following is the correct way to print a character variable ch in C?
easy
A. printf("%c", ch);
B. printf("%s", ch);
C. printf("%d", ch);
D. printf("%f", ch);
Solution
Step 1: Identify character format specifier
The correct specifier for a character is %c.
Step 2: Check other options
%s is for strings, %d for integers, and %f for floats, so they are incorrect for a character.
Final Answer:
printf("%c", ch); -> Option A
Quick Check:
Character print = %c [OK]
Hint: Use %c to print single characters [OK]
Common Mistakes:
Using %s for characters
Using %d which prints ASCII code
Using %f for characters
3. What will be the output of the following code?
int x = 10; float y = 5.5; printf("%d %f", x, y);
medium
A. 10 5.5
B. 10 5.500000
C. 5.5 10
D. Error
Solution
Step 1: Understand format specifiers in printf
%d prints integer as is, %f prints float with 6 decimal places by default.
Step 2: Match variables to specifiers
x is 10 (int), printed by %d. y is 5.5 (float), printed by %f as 5.500000.
Final Answer:
10 5.500000 -> Option B
Quick Check:
Integer and float print with %d and %f [OK]
Hint: Floats print with 6 decimals by default using %f [OK]
Common Mistakes:
Expecting %f to print fewer decimals
Swapping variable order
Using %d for float causing wrong output
4. Identify the error in the following code snippet:
char name[10]; scanf("%c", name);
medium
A. No error, code is correct
B. Use &name instead of name
C. Use %d instead of %c
D. Use %s instead of %c to read string
Solution
Step 1: Understand scanf format specifiers
%c reads a single character, but name is an array for a string.
Step 2: Correct specifier for string input
To read a string into name, use %s which reads until whitespace.
Final Answer:
Use %s instead of %c to read string -> Option D
Quick Check:
String input needs %s in scanf [OK]
Hint: Use %s to read strings with scanf [OK]
Common Mistakes:
Using %c to read strings
Forgetting & with non-array variables
Using %d for characters
5. You want to print a floating-point number float pi = 3.14159; with only 2 decimal places. Which printf statement is correct?
hard
A. printf("%2f", pi);
B. printf("%f.2", pi);
C. printf("%.2f", pi);
D. printf("%f", pi);
Solution
Step 1: Understand precision in format specifiers
To limit decimal places, use %.2f where 2 is the number of decimals.
Step 2: Check other options
%2f sets minimum width, not decimals; %f.2 is invalid; %f prints 6 decimals by default.
Final Answer:
printf("%.2f", pi); -> Option C
Quick Check:
Use %.2f to print 2 decimals [OK]
Hint: Use %.2f to print float with 2 decimals [OK]
Common Mistakes:
Using %2f expecting 2 decimals
Placing .2 after %f incorrectly
Using %f prints 6 decimals by default
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.