0
0
Cprogramming~20 mins

Using scanf for input - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Scanf Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Reading an integer with scanf
What is the output of this C program when the user inputs 42?
C
#include <stdio.h>

int main() {
    int num;
    printf("Enter a number: ");
    scanf("%d", &num);
    printf("You entered: %d\n", num);
    return 0;
}
AYou entered: 42
BCompilation error
CYou entered: %d
DYou entered: 0
Attempts:
2 left
💡 Hint
Remember scanf needs the address of the variable to store the input.
Predict Output
intermediate
2:00remaining
Reading multiple inputs with scanf
What will be printed if the user inputs 10 20?
C
#include <stdio.h>

int main() {
    int a, b;
    scanf("%d %d", &a, &b);
    printf("Sum: %d\n", a + b);
    return 0;
}
ASum: 30
BSum: 1020
CSum: 10
DRuntime error
Attempts:
2 left
💡 Hint
scanf reads each integer separately when given multiple format specifiers.
Predict Output
advanced
2:00remaining
Using scanf with wrong format specifier
What happens when this program runs and the user inputs 3.14?
C
#include <stdio.h>

int main() {
    int x;
    scanf("%d", &x);
    printf("Value: %d\n", x);
    return 0;
}
ARuntime error
BValue: 0
CValue: 3.14
DValue: 3
Attempts:
2 left
💡 Hint
scanf with %d reads integer part and stops at decimal point.
Predict Output
advanced
2:00remaining
Reading a string with scanf
What will be the output if the user inputs HelloWorld?
C
#include <stdio.h>

int main() {
    char str[20];
    scanf("%s", str);
    printf("You typed: %s\n", str);
    return 0;
}
AYou typed: Hello World
BYou typed: HelloWorld
CYou typed: HelloWorld\n
DBuffer overflow error
Attempts:
2 left
💡 Hint
scanf with %s reads input until first whitespace.
Predict Output
expert
2:00remaining
Using scanf with character input and newline
What will be printed if the user inputs A and presses Enter?
C
#include <stdio.h>

int main() {
    char c1, c2;
    scanf("%c", &c1);
    scanf("%c", &c2);
    printf("Chars: %c and %c\n", c1, c2);
    return 0;
}
AChars: A and (space)
BChars: A and A
CChars: A and \n
DRuntime error
Attempts:
2 left
💡 Hint
The second scanf reads the leftover newline character from input buffer.