Difficulty: Medium
Correct Answer: 6
Explanation:
Introduction / Context:
This C program demonstrates how command-line arguments are received via argc and argv, how atoi parses strings into integers, and why argv[0] does not break the sum even though it is the program name. Understanding iteration over argv and numeric conversion behavior is essential for basic CLI handling.
Given Data / Assumptions:
Concept / Approach:
The loop sums atoi(argv[i]) for i = 0..argc-1. atoi("myprog") → 0. atoi("1") → 1, atoi("2") → 2, atoi("3") → 3. Therefore the total is 0 + 1 + 2 + 3 = 6.
Step-by-Step Solution:
Verification / Alternative check:
Replacing atoi with strtol would also yield the same sums here; testing with additional non-numeric args also demonstrates atoi returns 0 when no leading digits are present.
Why Other Options Are Wrong:
Common Pitfalls:
Forgetting argv[0] is included; assuming atoi would fail or crash on non-numeric strings; expecting string concatenation instead of arithmetic.
Final Answer:
6
Discussion & Comments