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

Using scanf("%8s", name) restricts the input to a maximum of 8 characters (plus a null terminator), preventing buffer overflow if name is sufficiently sized. %s is unbounded, while %.8s is invalid for scanf input.