0
0
Cprogramming~5 mins

Using scanf for input - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is the purpose of scanf in C?

scanf is used to read input from the user through the keyboard and store it in variables.

Click to reveal answer
beginner
How do you read an integer value using scanf?

Use scanf("%d", &variable); where variable is an int type.

Click to reveal answer
beginner
Why do we use the ampersand (&) before the variable name in scanf?

The ampersand & gives the memory address of the variable so scanf can store the input value there.

Click to reveal answer
beginner
What format specifier is used to read a floating-point number with scanf?

Use %f to read a float value.

Click to reveal answer
beginner
How can you read a string (word) using scanf?

Use scanf("%s", variable); where variable is a character array (e.g., char name[20];).

Click to reveal answer
Which of the following is the correct way to read an integer using scanf?
Ascanf("%d", num);
Bscanf("%d", &num);
Cscanf("%i", num);
Dscanf("int", &num);
What does scanf return after reading input?
AThe number of input items successfully matched and assigned
BThe input value itself
CThe memory address of the variable
DNothing (void)
Which format specifier reads a floating-point number in scanf?
A%f
B%d
C%c
D%s
How do you read a single character using scanf?
Ascanf("char", ch);
Bscanf("%s", &ch);
Cscanf("%d", &ch);
Dscanf("%c", &ch);
Why might scanf("%s", name); be unsafe without precautions?
AIt reads only one character
BIt does not store the input
CIt can cause buffer overflow if input is longer than the array size
DIt requires an ampersand (&) before the variable
Explain how to use scanf to read an integer and why the ampersand (&) is needed.
Think about how scanf needs to know where to put the input value.
You got /4 concepts.
    Describe the risks of using scanf to read strings and how to avoid them.
    Consider what happens if user types more characters than the array can hold.
    You got /4 concepts.