Difficulty: Easy
Correct Answer: fgets()
Explanation:
Introduction / Context:Reading entire lines including spaces is a common need in C. Historically, gets was used, but it became notorious for buffer overflow vulnerabilities and has been removed from the C standard. Knowing the safe alternative is essential for writing robust, secure C programs.
Given Data / Assumptions:
Concept / Approach:
Step-by-Step Solution:
Specify the buffer and its size → call fgets(buf, sizeof buf, stdin).On success, fgets stores the line including the trailing newline (unless the line exceeded the buffer or EOF occurred).Thus, the safe choice is fgets because it respects buffer boundaries.Verification / Alternative check:
Security guidelines and modern compilers warn against gets; many toolchains remove it entirely. Compiling code using gets often fails.Why Other Options Are Wrong:
Common Pitfalls:
Final Answer:
fgets()
Discussion & Comments