In the following C program that reads an array of structures, which problem are you most likely to encounter at runtime? int main() { struct emp { char name[20]; float sal; }; struct emp e[10]; int i; for (i = 0; i <= 9; i++) { scanf("%s %f", e[i].name, &e[i].sal); } return 0; }

Difficulty: Medium

Correct Answer: There is a risk of buffer overflow and memory corruption if the user types a name longer than 19 characters, because %s has no field width limit for the 20 byte array

Explanation:


Introduction / Context:
This question focuses on safe input handling in C, especially when reading strings into fixed size character arrays inside structures. Misuse of scanf with %s can easily lead to buffer overflows, which are serious security and stability problems.


Given Data / Assumptions:

  • Each struct emp has a name array of 20 chars and a float sal.
  • An array e of 10 such structures is declared.
  • A loop reads 10 pairs of values using scanf("%s %f", e[i].name, &e[i].sal);.
  • Users may type arbitrary length names at the console.


Concept / Approach:
The %s conversion in scanf reads a sequence of non whitespace characters into the provided char array and appends a terminating zero byte. However, %s does not automatically know the size of the destination buffer. If the user types more characters than the buffer can hold (including the terminating zero), scanf will write past the end of the array. In this program, name has room for at most 19 visible characters plus the terminating zero. Without a field width limit in the format string, there is a real risk of overflowing the buffer.


Step-by-Step Solution:
Step 1: Consider a user who types a short name such as "Bob". This fits safely in name and the program behaves normally.Step 2: Now consider a user who types a very long token such as "VeryLongEmployeeNameExceedingLimit".Step 3: scanf with %s will attempt to copy all of these characters into name, exceeding its 20 byte capacity.Step 4: This overwrites adjacent memory, potentially corrupting the e array, local variables, or even control data such as the stack frame.Step 5: Such corruption may lead to crashes or security vulnerabilities.


Verification / Alternative check:
A safer approach is to specify a maximum field width such as "%19s %f" so that scanf never writes more than 19 characters plus the terminating zero into name. Using this safer format removes the overflow risk for well formed input.


Why Other Options Are Wrong:
Option B is incorrect because passing e[i].name to scanf is valid; arrays decay to pointers, and scanf expects char * for %s.Option C is wrong because the loop condition i <= 9 correctly iterates 10 times.Option D is incorrect because for scanf, %f is the correct specifier for reading a float; %lf is used when reading a double.


Common Pitfalls:
Programmers often forget to include field width limits in scanf format strings. It is safer to use fgets followed by parsing, or to ensure that every %s includes a width that matches the destination buffer size minus one.


Final Answer:
The main likely problem is There is a risk of buffer overflow and memory corruption if the user types a name longer than 19 characters, because %s has no field width limit for the 20 byte array.

More Questions from Programming

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion