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?✗ Incorrect
Option B correctly uses the format specifier %d and the address operator &.
What does
scanf return after reading input?✗ Incorrect
scanf returns how many inputs it successfully read and stored.
Which format specifier reads a floating-point number in
scanf?✗ Incorrect
%f is used for reading float values.
How do you read a single character using
scanf?✗ Incorrect
%c reads a single character and requires the address of the variable.
Why might
scanf("%s", name); be unsafe without precautions?✗ Incorrect
Without limiting input size, scanf can overflow the buffer causing errors.
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.