Bird
0
0

You want to read a string (word) from the user into a character array name[20]. Which of the following scanf statements is correct and safe to avoid buffer overflow?

hard📝 Application Q15 of 15
C - Input and Output
You want to read a string (word) from the user into a character array name[20]. Which of the following scanf statements is correct and safe to avoid buffer overflow?
Ascanf("%s", name);
Bscanf("%s", &name);
Cscanf("%19s", name);
Dscanf("%20s", &name);
Step-by-Step Solution
Solution:
  1. Step 1: Understand string input with scanf

    To read a string into name, use %s without & because arrays decay to pointers.
  2. Step 2: Prevent buffer overflow

    Limit input length to 19 characters using %19s to leave space for the null terminator.
  3. Final Answer:

    scanf("%19s", name); -> Option C
  4. Quick Check:

    Limit string input length with %19s [OK]
Quick Trick: Use %19s for 20-char array to avoid overflow [OK]
Common Mistakes:
  • Using & with arrays in scanf
  • Not limiting input length causing overflow
  • Using wrong format specifier for strings

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More C Quizzes