0
0
Cprogramming~10 mins

Using scanf for input - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to read an integer from the user using scanf.

C
#include <stdio.h>

int main() {
    int number;
    scanf([1], &number);
    printf("You entered: %d\n", number);
    return 0;
}
Drag options to blanks, or click blank then click option'
A"%d"
B"%f"
C"%c"
D"%s"
Attempts:
3 left
💡 Hint
Common Mistakes
Using %f or %c instead of %d for integer input.
Forgetting the quotes around the format specifier.
2fill in blank
medium

Complete the code to read a float value from the user using scanf.

C
#include <stdio.h>

int main() {
    float value;
    scanf([1], &value);
    printf("You entered: %f\n", value);
    return 0;
}
Drag options to blanks, or click blank then click option'
A"%d"
B"%c"
C"%f"
D"%s"
Attempts:
3 left
💡 Hint
Common Mistakes
Using %d instead of %f for float input.
Forgetting the ampersand (&) before the variable.
3fill in blank
hard

Fix the error in the scanf statement to correctly read a character.

C
#include <stdio.h>

int main() {
    char ch;
    scanf([1], &ch);
    printf("You entered: %c\n", ch);
    return 0;
}
Drag options to blanks, or click blank then click option'
A"%f"
B"%d"
C"%s"
D"%c"
Attempts:
3 left
💡 Hint
Common Mistakes
Using %d, %f or %s instead of %c for character input.
4fill in blank
hard

Complete the code to read a string safely using scanf.

C
#include <stdio.h>

int main() {
    char name[20];
    scanf([1], name);
    printf("Hello, %s!\n", name);
    return 0;
}
Drag options to blanks, or click blank then click option'
A"%19s"
B"%s"
D&
Attempts:
3 left
💡 Hint
Common Mistakes
Using %s without size limit, risking buffer overflow.
Using & before the array name when reading strings.
5fill in blank
hard

Fill all three blanks to read two integers and one float from the user.

C
#include <stdio.h>

int main() {
    int a, b;
    float c;
    scanf("[1] [2] [3]", &a, &b, &c);
    printf("Integers: %d, %d and Float: %f\n", a, b, c);
    return 0;
}
Drag options to blanks, or click blank then click option'
A"%d %d %f"
B"%d %f %d"
C"%f"
D"%d"
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up the order of format specifiers.
Using wrong format specifiers for variables.