C (Turbo C under DOS) — determine the program's output when executed with command-line arguments.\n\nCommand used:\ncmd> sample Good Morning\n\n/* sample.c */\n#include<stdio.h>\n\nint main(int argc, char *argv[])\n{\n printf("%d %s", argc, argv[1]);\n return 0;\n}\n\nWhat will be printed to the screen?

Difficulty: Easy

Correct Answer: 3 Good

Explanation:


Introduction / Context:
This question checks understanding of how C command-line arguments are passed to main on DOS-based compilers such as Turbo C, and how argc (argument count) and argv (argument vector) influence the printed output.


Given Data / Assumptions:

  • The program is invoked as: sample Good Morning.
  • main signature is int main(int argc, char *argv[]).
  • The program prints using printf("%d %s", argc, argv[1]).


Concept / Approach:
In C, argv[0] holds the program name ("sample" here). Subsequent elements hold each argument token split on spaces: argv[1] = "Good", argv[2] = "Morning". argc is the count of tokens including the program name, so argc = 3 for this invocation.


Step-by-Step Solution:

1) Tokenize command: argv[0] = "sample".2) argv[1] = "Good".3) argv[2] = "Morning".4) argc = 3.5) printf prints: "3 Good".


Verification / Alternative check:
Running with an extra token, e.g., sample A B C, makes argc = 4 and argv[1] = "A", confirming the pattern.


Why Other Options Are Wrong:

  • 2 Good: Would imply argv[0] not counted, which is incorrect.
  • Good Morning: The format prints an integer and a single string, not two strings.
  • 3 Morning: argv[1] is "Good", not "Morning".


Common Pitfalls:
Confusing argv[0] with the first user-supplied argument and assuming spaces remain as a single string without quoting.


Final Answer:
3 Good

Discussion & Comments

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