What if your program could change what it does just by typing different words when you start it?
Why Accessing arguments? - Purpose & Use Cases
Imagine you want to write a program that can take different inputs each time it runs, like a calculator that adds numbers you type when you start it.
Without a way to access these inputs, you'd have to change the program's code every time you want to try new numbers.
Manually changing the program for each input is slow and boring.
It also causes mistakes because you might forget to update some parts or type wrong values.
This makes testing or using the program again and again very frustrating.
Accessing arguments lets your program receive inputs directly when it starts.
This means you can run the same program many times with different inputs without changing the code.
It makes your program flexible and easy to use.
int main() {
int a = 5;
int b = 10;
int sum = a + b;
printf("Sum is %d", sum);
return 0;
}int main(int argc, char *argv[]) {
int a = atoi(argv[1]);
int b = atoi(argv[2]);
int sum = a + b;
printf("Sum is %d", sum);
return 0;
}You can create programs that accept different inputs each time they run, making them much more useful and interactive.
Think of a program that calculates the total price of items you buy by entering the prices when you start it, instead of changing the code for every new purchase.
Accessing arguments lets programs take input when they start.
This avoids rewriting code for every new input.
It makes programs flexible and user-friendly.