Printing argv[0] only: observe program name output regardless of the numeric arguments.
/* sample.c */
#include
int main(int argc, char argv[])
{
/ Invocations: sample 1 2 3 | sample 2 2 3 | sample 3 2 3 */
printf("%s
", argv[0]);
return 0;
}
-
Asample 3 2 3
-
Bsample 1 2 3
-
Csample
-
DError
Answer
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", 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