🚀 Learn BLE on Linux (BlueZ) – Programming Part 1
Hands-on Bluetooth Low Energy course. Special price: ₹399
Enroll Now on Udemy →C Formatted I/O Quiz Lab
A tricky scanf and printf quiz with 46 questions in 8 different styles: multiple choice, true or false traps, predict the output, buffer tracing, bug hunting, format writing, matching and coding. Part of our free embedded C programming course.
You have learned scanf field width, printf width and precision, and the scanf suppression character. Reading about them is one thing; getting a trick question right under pressure is another. This scanf and printf quiz is built to find the gaps: every question has a trap that catches students who only half remember the rules.
Every answer was checked by compiling and running the code with GCC 13 on Ubuntu 24.04. Treat this lab like an interview round from our free embedded C programming course, the foundation of our free embedded systems course, free embedded Linux course, free Linux device drivers course and free Linux kernel development course.
What You Will Practice
- Predicting exactly what printf prints, including blanks, rounding and the return value
- Tracing what scanf stores and what it leaves in the input buffer
- Spotting bugs that the compiler catches and bugs it silently ignores
- Writing correct format strings from a plain-English requirement
- Combining width, precision, flags and the suppression star
Prerequisites
- The lectures on scanf field width, printf width and precision, and the scanf suppression character
- GCC installed so you can run any snippet yourself and check your answer
How to Use This scanf and printf Quiz
- Cover the answer. Write your own answer on paper first.
- Click Show answer and compare. Each answer explains the trap.
- Give yourself one point for every fully correct answer. Partial answers score zero, just like a real compiler.
- Total your points in the score card at the end.
Tip
When a question says “predict the output”, do not type the code into a compiler until you have committed to an answer. The learning is in the gap between your guess and the real result.
Round 1: scanf and printf Quiz Multiple Choice
One correct option each. Read all four options before choosing.
Q1. What does sscanf("12345", "%3d", &x) store in x?
- A) 12345
- B) 123
- C) 345
- D) 12
Show answer
B) 123. The width of 3 limits the item to three characters. The characters 4 and 5 stay unread.
Q2. What does printf("[%2d]", 12345) print?
- A) [12]
- B) [45]
- C) [12345]
- D) [ 12345]
Show answer
C) [12345]. A printf width is a minimum. It pads but never cuts.
Q3. With char buf[10];, which is the safest way to read one word?
- A)
scanf("%s", buf); - B)
scanf("%10s", buf); - C)
scanf("%9s", buf); - D)
scanf("%*s", buf);
Show answer
C) %9s. Nine characters plus the terminator fill the ten bytes. Option B writes eleven bytes in the worst case. Option A has no limit at all. Option D suppresses the assignment, so nothing is stored.
Q4. The input is 1 2 3 and the call is scanf("%d %*d %d", &a, &b). What is returned and stored?
- A) returns 3, a=1, b=3
- B) returns 2, a=1, b=3
- C) returns 2, a=1, b=2
- D) returns 3, a=1, b=2
Show answer
B. The middle number is read and discarded. Only two assignments happen, so the return value is 2.
Q5. What does printf("%.2s", "embedded") print?
- A) embedded
- B) em
- C) ed
- D) (two blanks, then em)
Show answer
B) em. For strings, precision is the maximum number of characters printed.
Q6. What does printf("[%5.1f]", 9.96) print?
- A) [ 9.9]
- B) [ 10.0]
- C) [10.0 ]
- D) [ 9.96]
Show answer
B) [ 10.0]. One decimal place rounds 9.96 up to 10.0. That is four characters, so a width of five adds one blank on the left. The rounding carried into a new digit.
Q7. What does printf("[%+d] [%+d]", 5, -5) print?
- A) [5] [-5]
- B) [+5] [+-5]
- C) [+5] [-5]
- D) [ 5] [-5]
Show answer
C) [+5] [-5]. The plus flag forces a sign on positive numbers. Negative numbers always show their own sign.
Round 2: True or False Traps
Each statement sounds reasonable. Decide whether it is true, then check.
Q8. “A scanf width pads short input with blanks.”
Show answer
False. In scanf the width is a maximum. Padding belongs to printf.
Q9. “printf("%3d", 12345) prints 123.”
Show answer
False. It prints 12345. Width never truncates.
Q10. “The value returned by scanf counts suppressed conversions such as %*d.”
Show answer
False. Only conversions that assigned a value are counted.
Q11. “printf("%5s", "ab") prints three blanks followed by ab.”
Show answer
True. The string has two characters and the width is five, so three blanks are added on the left.
Q12. “Leading spaces count toward the width of %3d in scanf.”
Show answer
False. Leading whitespace is skipped first and is not counted. With input " 12345", %3d still reads 123.
Q13. “printf("%.0f", 2.5) prints 3.”
Show answer
False. It prints 2. An exact tie rounds to the nearest even digit with the C library on Linux.
Q14. “%c skips leading whitespace just like %d.”
Show answer
False. sscanf(" A", "%c", &ch) stores a space (code 32). To skip blanks, put a space in the format: " %c" stores A.
Round 3: Predict the Output
Write the exact output, including every blank. Square brackets show where fields start and end.
Q15.
int a = 0, b = 0;
sscanf("4096", "%2d%2d", &a, &b);
printf("%d-%d\n", a, b);
Show answer
40-96
The widths alone split the digits into two numbers.
Q16.
printf("[%6.2f][%-6.1f][%06.1f]\n", 3.14159, 2.55, -1.5);
Show answer
[ 3.14][2.5 ][-001.5]
The trap is the middle field. 2.55 cannot be stored exactly, and the stored value is slightly below 2.55, so one decimal place gives 2.5, not 2.6. In the last field the zero flag pads after the sign.
Q17.
char s[16], t[16];
int n = sscanf("hello world", "%3s%3s", s, t);
printf("%s|%s|%d\n", s, t, n);
Show answer
hel|lo|2
The first %3s takes “hel”. The second starts at “lo” and stops at the space. Both conversions succeed.
Q18.
int n = printf("%5s|%-5s|", "ab", "cd");
printf(" n=%d\n", n);
Show answer
ab|cd | n=12
Five characters, a bar, five characters, a bar: 5 + 1 + 5 + 1 = 12.
Q19. Reset x and y to 0 before each call. What does each call return and store?
n = sscanf("7,8", "%d%*c%d", &x, &y);
n = sscanf("7 , 8", "%d%*c%d", &x, &y);
Show answer
First call: returns 2, x = 7, y = 8. Second call: returns 1, x = 7, y = 0.
In the second input, %*c swallows the space, so the next %d meets a comma and cannot convert.
Q20.
printf("[%*d][%-*d]\n", 5, 42, 5, 42);
Show answer
[ 42][42 ]
Each star takes its width from the argument just before the value.
Q21.
printf("[%.3d] [%5.3d]\n", 7, 7);
Show answer
[007] [ 007]
For integers, precision means a minimum number of digits. The width then pads the three-digit result to five columns.
Round 4: Trace the Input Buffer
For each case, give the stored values and the text left unread in the buffer.
Q22. Format "%3d%2d", input 9999 77.
Show answer
a = 999, b = 9, and the text 77 (starting with a space) is left.
Before
After both conversions
Legend
The second conversion reads one nine and then hits the space.
Q23. Format "%f%c", input -5.5e3x.
Show answer
The float is -5500 and the character is x. Nothing is left. The float conversion accepts an exponent, so it consumed “-5.5e3” entirely.
Q24. Format "%2d%c", input 123.
Show answer
The integer is 12 and the character is 3, which is the digit as a character, not the number three. Nothing is left.
Q25. Format "%2s%3s", input ABCDEFGH.
Show answer
The first string is “AB”, the second is “CDE”, and “FGH” is left.
After both conversions
Legend
There is no space in the input, yet the widths split the word. Each string also receives its own terminator.
Round 5: Spot the Bug
Each snippet compiles, and each has a bug. Name the problem and write the fix.
Q26.
char name[8];
scanf("%8s", name);
Show answer
Off by one. Eight characters plus the terminator need nine bytes. Fix: scanf("%7s", name);
Q27.
float f;
scanf("%lf", &f);
Show answer
In scanf, %lf stores a double, but f is a float, so scanf writes past the variable. Fix: use %f, or declare f as a double.
Q28.
double d = 3.5;
printf("%d\n", d);
Show answer
A double is printed with an integer conversion, which is undefined behaviour and usually prints nonsense. Fix: printf("%f\n", d);
Q29.
int a, b, c;
scanf("%d %*d %d", &a, &b, &c);
Show answer
Only two conversions store values, so the third pointer is never used and c stays uninitialised. Fix: remove &c, and check that scanf returned 2.
Q30.
const char *msg = get_user_text();
printf(msg);
Show answer
This is a format string vulnerability: if the user’s text contains conversions such as %s or %n, printf reads or writes memory it should not. Fix: printf("%s", msg);
Q31.
scanf("%d", &a);
scanf("%*[^\n]%*c");
Show answer
If nothing but the newline follows the number, %*[^\n] matches zero characters and fails, so the call stops before %*c and the newline stays in the buffer. Fix: use two calls, scanf("%*[^\n]"); then scanf("%*c");
Now check yourself. Which of these bugs does GCC warn about?
| Question | Bug | GCC warning with -Wall -Wextra? |
|---|---|---|
| Q26 | Width one too large | No. The compiler cannot know the array size relation |
| Q27 | %lf with a float pointer | Yes (format expects double pointer) |
| Q28 | %d with a double | Yes (format expects int) |
| Q29 | Extra argument | Yes (too many arguments for format) |
| Q30 | Non-literal format string | Only with -Wformat-security |
| Q31 | Single-call flush | No. It is valid code with a surprising result |
The lesson: always compile with warnings on, but remember that the quietest bugs (off-by-one widths and logic traps) are the ones only you can catch.
Round 6: Write the Format String
Write the format string that meets each requirement. Equivalent answers earn the point.
Q32. Read 2026-10-22 into three integers: year, month, day.
Show answer
"%d-%d-%d" returns 3. A width-based alternative is "%4d-%2d-%2d".
Q33. Print the name VBAT left-aligned in 10 columns, then 3.012 right-aligned in 8 columns with two decimals.
Show answer
"%-10s%8.2f". The output is VBAT 3.01: the name, six padding blanks, then four blanks before 3.01.
Q34. From the text abc,77 read only the number after the first comma.
Show answer
"%*[^,],%d" skips everything up to the comma, matches the comma, then reads 77. It returns 1.
Q35. Print the integer 31 as 0x0000001F.
Show answer
"0x%08X". The literal prefix is printed as text, the zero flag pads and 8 is the width.
Q36. Read one word safely into char buf[16].
Show answer
"%15s". Width is array size minus one.
Q37. From alpha 42 beta keep only the middle number.
Show answer
"%*s %d %*s" returns 1 and stores 42.
Round 7: Match the Following
Match each format (Q38 to Q43) with its behaviour (A to F). Each letter is used once. Remember that some formats belong to scanf and others to printf.
| # | Format | Letter | Behaviour |
|---|---|---|---|
| Q38 | scanf("%3s", ...) | A | Prints at most three characters of a string |
| Q39 | scanf("%*d") | B | Reads an integer and discards it |
| Q40 | printf("%.3s", ...) | C | Left-justifies text in eight columns |
| Q41 | printf("%*d", w, v) | D | Reads at most three characters and adds a terminator |
| Q42 | printf("%-8s", ...) | E | Takes the field width from an argument |
| Q43 | printf("%08.3f", ...) | F | Pads with zeros to eight columns with three decimals |
Show answer
Q38 matches D, Q39 matches B, Q40 matches A, Q41 matches E, Q42 matches C and Q43 matches F.
Check on Q43: printing 3.14159 gives 0003.142.
Round 8: Coding Challenges
Write the code before opening the solution. Each has a short trap.
Q44. The text is ID:0042 TEMP:36.5C. Write one sscanf call that stores the id as an int and the temperature as a float.
Show answer
int id; float temp;
int n = sscanf(line, "ID:%d TEMP:%f", &id, &temp);
It returns 2 with id = 42 and temp = 36.5. The trap: %d reads 0042 as decimal 42, and the literal text must match exactly. The trailing letter C is simply ignored.
Q45. Print a 5 by 5 multiplication table with every number right-aligned in four columns.
Show answer
#include <stdio.h>
int main(void)
{
for (int row = 1; row <= 5; row++) {
for (int col = 1; col <= 5; col++)
printf("%4d", row * col);
printf("\n");
}
return 0;
}
gcc -std=c11 -Wall -Wextra -o q_table q_table.c
./q_table
1 2 3 4 5
2 4 6 8 10
3 6 9 12 15
4 8 12 16 20
5 10 15 20 25
The width of 4 keeps the numbers aligned even when they change from one digit to two.
Q46. Parse the time 07:05:09 and print the total number of seconds. Reject bad input.
Show answer
#include <stdio.h>
int main(void)
{
const char *stamp = "07:05:09";
int hh = 0, mm = 0, ss = 0;
if (sscanf(stamp, "%2d:%2d:%2d", &hh, &mm, &ss) != 3) {
printf("bad stamp\n");
return 1;
}
printf("%02d:%02d:%02d = %d seconds\n",
hh, mm, ss, hh * 3600 + mm * 60 + ss);
return 0;
}
gcc -std=c11 -Wall -Wextra -o q_clock q_clock.c
./q_clock
07:05:09 = 25509 seconds
The trap is skipping the return-value check. If the text were malformed, the variables would hold garbage and your arithmetic would silently be wrong.
Score Card for the scanf and printf Quiz
| Round | Type | Questions | Your points |
|---|---|---|---|
| 1 | Multiple choice | 7 | ____ / 7 |
| 2 | True or false traps | 7 | ____ / 7 |
| 3 | Predict the output | 7 | ____ / 7 |
| 4 | Trace the input buffer | 4 | ____ / 4 |
| 5 | Spot the bug | 6 | ____ / 6 |
| 6 | Write the format string | 6 | ____ / 6 |
| 7 | Match the following | 6 | ____ / 6 |
| 8 | Coding challenges | 3 | ____ / 3 |
| Total | 46 | ____ / 46 |
| Score | What it means | What to do |
|---|---|---|
| 0 to 20 | The basics need another pass | Re-read the three lectures and rerun every example program |
| 21 to 33 | Solid, with some traps still working on you | Redo the rounds where you lost the most points |
| 34 to 41 | Strong. You read format strings like a compiler | Write your own tricky questions for a friend |
| 42 to 46 | Interview ready | Move on to the next chapter |
Common Traps Recap from the scanf and printf Quiz
| Trap | Rule to remember |
|---|---|
| Width in scanf versus printf | scanf width is a maximum; printf width is a minimum |
| Too-small printf width | The value is printed in full; nothing is cut |
| Precision on strings versus integers | Strings: maximum characters. Integers: minimum digits |
| Rounding | Stored binary value decides, and exact ties round to even |
| String buffers | Array size minus one is the safe width |
| Return value of scanf | Counts assignments only, never suppressed items |
| Spaces in a scanf format | They skip any amount of whitespace, including none |
| %c | Does not skip leading whitespace |
| Leftovers | Anything not consumed stays in the buffer |
| Types | printf %f takes float or double; scanf needs %f for float and %lf for double |
Summary and Key Takeaways
What this lab trained
- Reading a format string literally, one conversion at a time
- Tracking the input buffer from one conversion to the next
- Telling apart bugs the compiler reports from bugs it cannot see
- Choosing the right tool: width, precision, flag or star
A good scanf and printf quiz does not test whether you memorised syntax. It tests whether you can simulate the library in your head. If you missed questions, that is useful: pick the rounds where you lost points, rerun the matching programs from the lectures, and change the inputs until the surprises stop. These habits are exactly what make debug output readable and input parsing safe in real embedded firmware.
Frequently Asked Questions
What topics does this scanf and printf quiz cover?
Field width in scanf, width and precision in printf, flags such as minus, zero and plus, the star for dynamic width, the scanf suppression character, return values and the input buffer.
Are the answers tested on a real compiler?
Yes. Every snippet and answer was compiled and run with GCC 13 on Ubuntu 24.04 using the C11 standard.
Will the same answers hold on other compilers?
The rules about widths, precision and suppression come from the C standard, so they hold everywhere. Rounding of exact ties can differ on some embedded libraries, so check your own toolchain.
How should I prepare for C interview questions on printf and scanf?
Practise predicting output on paper, then verify. Be ready to explain width versus precision, the return values of printf and scanf, and why unbounded %s is dangerous.
Why do some answers depend on how floating-point numbers are stored?
Many decimal fractions, such as 2.55, cannot be stored exactly in binary. printf rounds the stored value, which can differ from your hand calculation.
Where can I learn embedded C and Linux for free?
EmbeddedPathashala offers a free embedded C programming course that leads into a free embedded systems course, a free embedded Linux course, a free Linux device drivers course and a free Linux kernel development course.
Keep Going With the Free Course
Finished the lab? Share your score with a classmate and challenge them to beat it. Then continue to the next chapter of the free embedded C programming course. Every lesson on EmbeddedPathashala is free, from C basics to Linux kernel and device drivers.

1 Comment