Challenge - 5 Problems
Scanf Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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; }
Attempts:
2 left
💡 Hint
Remember scanf needs the address of the variable to store the input.
✗ Incorrect
The scanf function reads the integer input 42 and stores it in the variable num. Then printf prints it correctly.
❓ Predict Output
intermediate2: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; }
Attempts:
2 left
💡 Hint
scanf reads each integer separately when given multiple format specifiers.
✗ Incorrect
scanf reads two integers 10 and 20, stores them in a and b, then prints their sum 30.
❓ Predict Output
advanced2: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; }
Attempts:
2 left
💡 Hint
scanf with %d reads integer part and stops at decimal point.
✗ Incorrect
scanf with %d reads integer part 3 from input 3.14 and stores it in x. The decimal part is ignored.
❓ Predict Output
advanced2: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; }
Attempts:
2 left
💡 Hint
scanf with %s reads input until first whitespace.
✗ Incorrect
scanf reads the string HelloWorld into str and prints it exactly.
❓ Predict Output
expert2: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; }
Attempts:
2 left
💡 Hint
The second scanf reads the leftover newline character from input buffer.
✗ Incorrect
First scanf reads 'A', second scanf reads the newline character '\n' left in the input buffer.