In Turbo C, consider main defined with three parameters to also receive the environment vector. After correcting argv's type, what will this program print when executed (it attempts to print env variables)? /* corrected Turbo C style */ #include<stdio.h> int main(int argc, char *argv[], char *env[]) { int i; for(i = 1; env[i] != 0 && i < argc; i++) printf("%s\n", env[i]); return 0; }

Difficulty: Medium

Correct Answer: List of all environment variables

Explanation:


Introduction / Context:
This question clarifies that many DOS/Turbo C compilers support a third parameter to main for environment variables (envp). With a correct signature, you can iterate over env and print strings like PATH, COMSPEC, etc. The original snippet had an incorrect argv type; here it is repaired to avoid type issues.


Given Data / Assumptions:

  • Turbo C environment where main(int, char*[], char*[]) is supported.
  • env is a NULL-terminated array of char* environment strings.
  • The loop prints env[i] lines while both i < argc and env[i] exists (shown for safety).


Concept / Approach:
The environment vector is analogous to argv: it is an array of C strings terminated by a NULL pointer. Printing them yields lines like "PATH=...". The repaired function signature ensures %s receives a proper char*.


Step-by-Step Solution:

Use the 3-argument form of main to receive env. Iterate over env until NULL, printing each variable. Output is a list of environment variable strings.


Verification / Alternative check:
If you replace env[i] with argv[i], you would print command-line arguments instead. Using env confirms distinct vectors.


Why Other Options Are Wrong:

  • Command-line list / count: That would require iterating argv or printing argc.
  • Error about two arguments: Turbo C supports envp; this is not inherently erroneous.
  • Compilation error: With corrected types, it compiles.


Common Pitfalls:
Mixing up argv and envp or assuming envp is unavailable on older compilers; in practice, many did support it.


Final Answer:
List of all environment variables

More Questions from Command Line Arguments

Discussion & Comments

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