0
0
Cprogramming~10 mins

Accessing arguments - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Accessing arguments
Program starts
main(int argc, char *argv[
Check argc (argument count)
Access argv array elements
Use arguments in program
Program ends
The program starts and main receives argc and argv. argc tells how many arguments, argv holds them. The program checks argc, accesses argv elements, then uses them.
Execution Sample
C
#include <stdio.h>

int main(int argc, char *argv[]) {
    for (int i = 0; i < argc; i++) {
        printf("Argument %d: %s\n", i, argv[i]);
    }
    return 0;
}
This code prints all command-line arguments passed to the program, including the program name.
Execution Table
IterationiCondition (i < argc)ActionOutput
100 < argc (true)Print argv[0]Argument 0: ./program
211 < argc (true)Print argv[1]Argument 1: input.txt
322 < argc (true)Print argv[2]Argument 2: 123
433 < argc (false)Exit loop
💡 i reaches 3, condition 3 < argc (3) is false, loop ends
Variable Tracker
VariableStartAfter 1After 2After 3Final
i-0123
argc33333
argv[i]argv[0]argv[0]argv[1]argv[2]-
Key Moments - 3 Insights
Why does argv[0] contain the program name?
argv[0] always holds the program's name or path, so the first printed argument is the program itself, as shown in execution_table row 1.
What happens if argc is 1?
If argc is 1, it means no extra arguments were passed, so the loop runs once printing only argv[0], the program name, as the condition i < argc is true only for i=0.
Why does the loop stop when i equals argc?
The loop condition is i < argc. When i equals argc, the condition is false, so the loop exits, as shown in execution_table row 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is printed when i = 1?
AArgument 1: ./program
BArgument 1: input.txt
CArgument 1: 123
DNo output
💡 Hint
Check execution_table row 2 under Output column.
At which iteration does the loop condition become false?
AIteration 2
BIteration 3
CIteration 4
DIteration 1
💡 Hint
See execution_table row 4 Condition column.
If argc was 2, how many times would the loop run?
A2 times
B3 times
C1 time
D0 times
💡 Hint
Loop runs while i < argc, so for argc=2, i=0 and i=1 are valid.
Concept Snapshot
Accessing arguments in C:
- main receives int argc and char *argv[]
- argc = number of arguments including program name
- argv = array of strings with arguments
- argv[0] is program name
- Loop from i=0 to i<argc to access all arguments
Full Transcript
In C, the main function can access command-line arguments using two parameters: argc and argv. argc tells how many arguments were passed, including the program name. argv is an array of strings holding each argument. The program often loops from 0 to argc-1 to print or use each argument. argv[0] is always the program's name or path. The loop stops when the index reaches argc because the condition i < argc becomes false. This way, the program safely accesses all arguments passed to it.