Challenge - 5 Problems
Format Specifiers Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of integer and float formatting
What is the output of this C code snippet?
C
#include <stdio.h> int main() { int a = 42; float b = 3.14159; printf("%5d %.2f\n", a, b); return 0; }
Attempts:
2 left
💡 Hint
Look at the width and precision in the format specifiers.
✗ Incorrect
The %5d means print the integer right-aligned in a field 5 characters wide, so 42 is padded with 3 spaces. The %.2f means print the float with 2 digits after the decimal point, rounding 3.14159 to 3.14.
❓ Predict Output
intermediate2:00remaining
Output of string and character formatting
What will this C program print?
C
#include <stdio.h> int main() { char c = 'A'; char *str = "Hi"; printf("%3c %5s\n", c, str); return 0; }
Attempts:
2 left
💡 Hint
Check the width specifiers for character and string.
✗ Incorrect
%3c prints the character right-aligned in 3 spaces, so two spaces then 'A'. %5s prints the string right-aligned in 5 spaces, so three spaces then 'Hi'.
❓ Predict Output
advanced2:00remaining
Output with left alignment and zero padding
What is the output of this code?
C
#include <stdio.h> int main() { int x = 7; printf("%-5d %05d\n", x, x); return 0; }
Attempts:
2 left
💡 Hint
Look at the minus sign and zero in the format specifiers.
✗ Incorrect
%-5d means print integer left-aligned in 5 spaces, so '7' then 4 spaces. %05d means print integer padded with zeros to width 5, so '00007'.
❓ Predict Output
advanced2:00remaining
Output of pointer and hexadecimal formatting
What will this program print?
C
#include <stdio.h> int main() { int num = 255; printf("%p %x %X\n", (void*)&num, num, num); return 0; }
Attempts:
2 left
💡 Hint
%p prints pointer with 0x prefix, %x and %X print hex lowercase and uppercase without 0x.
✗ Incorrect
%p prints the pointer address with 0x prefix. %x prints the integer in lowercase hex without 0x. %X prints uppercase hex without 0x. The actual pointer address varies but includes 0x prefix.
❓ Predict Output
expert2:00remaining
Output of mixed format specifiers with precision and width
What is the output of this C program?
C
#include <stdio.h> int main() { double val = 123.456789; printf("%10.3f %.5s %04d\n", val, "HelloWorld", 9); return 0; }
Attempts:
2 left
💡 Hint
Check width and precision for float, string, and integer.
✗ Incorrect
%10.3f prints float in 10 spaces with 3 decimals, rounding 123.456789 to 123.457. %.5s prints first 5 chars of string "HelloWorld" as "Hello". %04d prints integer padded with zeros to width 4, so 0009.