0
0
Cprogramming~3 mins

Why Accessing arguments? - Purpose & Use Cases

Choose your learning style9 modes available
The Big Idea

What if your program could change what it does just by typing different words when you start it?

The Scenario

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.

The Problem

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.

The Solution

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.

Before vs After
Before
int main() {
  int a = 5;
  int b = 10;
  int sum = a + b;
  printf("Sum is %d", sum);
  return 0;
}
After
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;
}
What It Enables

You can create programs that accept different inputs each time they run, making them much more useful and interactive.

Real Life Example

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.

Key Takeaways

Accessing arguments lets programs take input when they start.

This avoids rewriting code for every new input.

It makes programs flexible and user-friendly.