In C, consider the following program run from the command line as: myprog 1 2 3 The source code is: int main(int argc, char *argv[]) { int i, j = 0; for (i = 0; i < argc; i++) j = j + atoi(argv[i]); printf("%d", j); return 0; } What will this program print?

Difficulty: Easy

Correct Answer: 6

Explanation:


Introduction / Context:
This question checks how command-line arguments are passed to main in C and how functions like atoi interpret them. The program iterates over all arguments, including the program name, converts each to an integer using atoi, and accumulates their sum in j. Understanding which arguments are numeric and how atoi treats non-numeric strings is essential to determine the final result.



Given Data / Assumptions:

  • The program is invoked as myprog 1 2 3 from the command line.
  • argc is the count of arguments, including the program name; here argc = 4.
  • argv[0] is "myprog", argv[1] is "1", argv[2] is "2", and argv[3] is "3".
  • atoi converts a string to an int, returning 0 if the string does not start with a valid numeric sequence.


Concept / Approach:
The for loop runs from i = 0 to i < argc, visiting each argument string in argv. On each iteration, j is incremented by atoi(argv[i]). For numeric strings like "1", "2", and "3", atoi returns their integer values. For the program name "myprog", which does not begin with digits, atoi returns 0. The total sum is thus 0 + 1 + 2 + 3.



Step-by-Step Solution:
Step 1: Initialize j = 0.Step 2: On the first loop iteration, i = 0, argv[0] is "myprog". atoi("myprog") returns 0 because the string does not start with a digit. j remains 0.Step 3: On the second iteration, i = 1, argv[1] is "1". atoi("1") returns 1, so j becomes 0 + 1 = 1.Step 4: On the third iteration, i = 2, argv[2] is "2". atoi("2") returns 2, so j becomes 1 + 2 = 3.Step 5: On the fourth iteration, i = 3, argv[3] is "3". atoi("3") returns 3, so j becomes 3 + 3 = 6.Step 6: The loop ends when i reaches argc (4). The program then prints j, which is 6.


Verification / Alternative check:
You can mentally simulate atoi on each argument or run a similar test program. It is also helpful to remember that robust argument-summing programs usually start the loop at i = 1 to skip argv[0]. The fact that this program starts at i = 0 just adds an extra 0 from atoi("myprog"), which does not change the sum.



Why Other Options Are Wrong:
Option A (123) would correspond to concatenating the digits as a string, not summing them. Option C (Error) is incorrect because atoi handles non-numeric inputs by returning 0 rather than causing a run-time error. Option D ("123") would involve printing text, but the program prints an integer value without quotes or concatenation.



Common Pitfalls:
A common mistake is to assume that argv[0] is the first numeric argument, forgetting that it is actually the program name. Another pitfall is expecting atoi to throw an error on invalid input; instead, it returns 0 and does not report the failure unless additional checking is done with functions like strtol.



Final Answer:
The program prints the sum 1 + 2 + 3, which is 6.


More Questions from Programming

Discussion & Comments

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