C programming — identify the issue (if any) in this structure input loop.\n\n#include<stdio.h>\n\nint main()\n{\n struct emp\n {\n char name[20];\n float sal;\n };\n struct emp e[10];\n int i;\n for(i = 0; i <= 9; i++)\n scanf("%s %f", e[i].name, &e[i].sal);\n return 0;\n}\n\nDoes this code have a language/compilation error?

Difficulty: Easy

Correct Answer: No error

Explanation:


Introduction / Context:
This question checks whether the given structure definition, array usage, and scanf calls are valid in standard C, independent of any toolchain-specific linker quirks.


Given Data / Assumptions:

  • struct emp has char name[20] and float sal.
  • Array e[10] is declared.
  • Loop scans a string and a float using "%s %f".


Concept / Approach:
In standard C, reading a string into a char array with %s (no width limit though) and a float with %f (address passed) is valid. The code shows &e[i].sal, which is correct because %f expects float* for scanf. There is no language-level error with the struct layout or the loop bounds (i from 0 to 9 covers 10 elements).


Step-by-Step Solution:

1) Check members: name is a char array; sal is float — valid.2) e is an array of struct emp of size 10 — valid.3) scanf("%s %f", e[i].name, &e[i].sal); — types match the format.4) The code compiles and runs in standard C.


Verification / Alternative check:
Compile with a modern compiler (e.g., gcc/clang). Input sample names/salaries; values populate correctly. Adding a field width to %s (e.g., "%19s") is recommended to prevent overflow but not required for compilation.


Why Other Options Are Wrong:

  • Invalid structure member: Both members are standard.
  • Floating point formats not linked: This is not a C language error; historically it was a toolchain/linker setting on legacy compilers.
  • Missing &: The code uses &e[i].sal correctly.


Common Pitfalls:
Forgetting the & for non-array scalars in scanf, omitting width limits for %s, or assuming toolchain quirks are language errors.


Final Answer:
No error

More Questions from Structures, Unions, Enums

Discussion & Comments

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