0
0
Cprogramming~20 mins

Format specifiers - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Format Specifiers Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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;
}
A 42 3.14
B42 3.14
C 42 3.1416
D42 3.14159
Attempts:
2 left
💡 Hint
Look at the width and precision in the format specifiers.
Predict Output
intermediate
2: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;
}
A A Hi
BA Hi
C A Hi
DiH A
Attempts:
2 left
💡 Hint
Check the width specifiers for character and string.
Predict Output
advanced
2: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;
}
A70000 7
B 7 00007
C7 7
D7 00007
Attempts:
2 left
💡 Hint
Look at the minus sign and zero in the format specifiers.
Predict Output
advanced
2: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;
}
A7ffee3bff6ac ff FF
B0x7ffee3bff6ac ff FF
C0x7ffee3bff6ac 0xff 0xFF
D0x7ffee3bff6ac 255 255
Attempts:
2 left
💡 Hint
%p prints pointer with 0x prefix, %x and %X print hex lowercase and uppercase without 0x.
Predict Output
expert
2: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;
}
A 123.457 Hello 0009
B123.456789 Hello 0009
C 123.457 HelloWorld 9
D 123.456 Hello 9
Attempts:
2 left
💡 Hint
Check width and precision for float, string, and integer.