Printing argv[0] only: observe program name output regardless of the numeric arguments.\n\n/* sample.c */\n#include <stdio.h>\n\nint main(int argc, char argv[])\n{\n / Invocations: sample 1 2 3 | sample 2 2 3 | sample 3 2 3 */\n printf("%s\n", argv[0]);\n return 0;\n}\n

Difficulty: Easy

Correct Answer: sample

Explanation:


Introduction / Context:
argv[0] conventionally contains the program name used to invoke the executable. Printing argv[0] therefore shows the program name regardless of any extra arguments supplied by the user.


Given Data / Assumptions:

  • Three different invocations are shown with various numeric arguments.
  • The code always prints argv[0] and ignores others.
  • Program binary is assumed to be named "sample" in the environment.


Concept / Approach:
Since only argv[0] is printed, the output remains constant. Arguments beyond argv[0] do not influence the string being printed.


Step-by-Step Solution:

At startup, argv[0] points to the program name string.printf("%s\n", argv[0]) prints that string.Other argv entries are not accessed, so variations in arguments do not matter.


Verification / Alternative check:
Replace %s with printing *argv to observe identical behavior, since *argv equals argv[0] initially.


Why Other Options Are Wrong:

  • sample 3 2 3 / sample 1 2 3: These would require printing multiple arguments.
  • Error: The code is valid and compiles.


Common Pitfalls:
Expecting the entire command line to be printed by %s with argv[0]; confusing argv printing with environment-specific shells that echo commands.


Final Answer:
sample

More Questions from Command Line Arguments

Discussion & Comments

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