Multiple choice technology security

Which of the following is a secure way to use scanf?

  1. scanf("%.8s", name);

  2. scanf("%8s", name);

  3. scanf("%8c", name);

  4. scanf("%s", name);

Reveal answer Fill a bubble to check yourself
B Correct answer
Explanation

%8s reads up to 8 characters, adding null terminator (fits in 9-byte buffer). %.8s also works but reads at most 8 without null. Option C (%8c) reads 8 characters including no null terminator. Option D (%s) has no length limit and is unsafe.