Bird
Raised Fist0
Cprogramming~20 mins

Format specifiers - Practice Problems & Coding Challenges

Choose your learning style10 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.

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

  1. Step 1: Understand integer data type printing

    The %d specifier is used to print integer values in C.
  2. Step 2: Identify other specifiers

    %f is for floats, %c for characters, and %s for strings.
  3. Final Answer:

    %d -> Option A
  4. 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

  1. Step 1: Identify character format specifier

    The correct specifier for a character is %c.
  2. Step 2: Check other options

    %s is for strings, %d for integers, and %f for floats, so they are incorrect for a character.
  3. Final Answer:

    printf("%c", ch); -> Option A
  4. 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

  1. Step 1: Understand format specifiers in printf

    %d prints integer as is, %f prints float with 6 decimal places by default.
  2. Step 2: Match variables to specifiers

    x is 10 (int), printed by %d. y is 5.5 (float), printed by %f as 5.500000.
  3. Final Answer:

    10 5.500000 -> Option B
  4. 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

  1. Step 1: Understand scanf format specifiers

    %c reads a single character, but name is an array for a string.
  2. Step 2: Correct specifier for string input

    To read a string into name, use %s which reads until whitespace.
  3. Final Answer:

    Use %s instead of %c to read string -> Option D
  4. 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

  1. Step 1: Understand precision in format specifiers

    To limit decimal places, use %.2f where 2 is the number of decimals.
  2. Step 2: Check other options

    %2f sets minimum width, not decimals; %f.2 is invalid; %f prints 6 decimals by default.
  3. Final Answer:

    printf("%.2f", pi); -> Option C
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard