# Working With Command-Line Arguments in Practice

In the previous lesson, you learned:

*   What command-line arguments are
    
*   What `argc` and `argv` mean
    
*   How programs receive input from the terminal
    

Now we move to the practical side:

> **How do we actually use these arguments inside our programs?**

This is where your programs start becoming **dynamic and interactive**.

## 1\. Accessing Arguments

Each argument is stored in `argv`.

You can access them like this:

```c
argv[0]  // program name
argv[1]  // first argument
argv[2]  // second argument
```

### Example

If you run:

```bash
./program apple banana
```

Then:

```text
argv[0] = "./program"
argv[1] = "apple"
argv[2] = "banana"
```

## 2\. Looping Through Arguments

Instead of accessing arguments one by one, you can loop through them.

Concept:

```c
for (int i = 0; i < argc; i++)
{
    // process argv[i]
}
```

### Example Program

```c
#include <stdio.h>

int main(int argc, char *argv[])
{
    int i;

    for (i = 0; i < argc; i++)
    {
        printf("Argument %d: %s\n", i, argv[i]);
    }

    return 0;
}
```

### Run:

```bash
./program hello world
```

Output:

```text
Argument 0: ./program
Argument 1: hello
Argument 2: world
```

# 3\. Checking If Arguments Exist

Before using an argument, always check if it exists.

### Example

```c
if (argc > 1)
{
    printf("First argument: %s\n", argv[1]);
}
```

👉 Why?

Because this will crash:

```c
printf("%s\n", argv[1]);  // unsafe if no arguments
```

## 4\. Converting Arguments to Numbers

Remember:

> All arguments are strings

If you need numbers, you must convert them.

### Using `atoi()`

```c
#include <stdlib.h>

int num = atoi(argv[1]);
```

### Example Program

```c
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
    if (argc > 1)
    {
        int num = atoi(argv[1]);
        printf("Number: %d\n", num);
    }

    return 0;
}
```

### Run:

```bash
./program 25
```

Output:

```text
Number: 25
```

## 5\. Important Warning About `atoi()`

If the input is not a number:

```bash
./program hello
```

Then:

```text
atoi("hello") → 0
```

👉 This can be misleading.

Later, you will learn safer methods like `strtol()`.

## 6\. Handling Input Safely

Good programs always validate input.

### Example

```c
if (argc < 2)
{
    printf("Please provide an argument\n");
    return 1;
}
```

👉 This prevents errors and improves user experience.

## 7\. Practical Thinking

Instead of copying patterns, think about how arguments can be used.

### Scenario 1

A program receives multiple words and needs to process them one by one.

👉 You would:

*   loop through `argv`
    
*   handle each input
    

### Scenario 2

A program receives a number and performs a calculation.

👉 You would:

*   check if input exists
    
*   convert string → integer
    
*   process the number
    

### Scenario 3

A program behaves differently depending on input.

Example:

```bash
./program start
./program stop
```

👉 Your program logic changes based on `argv[1]`.

## 8\. Common Beginner Mistakes

### ❌ Accessing arguments without checking

```c
printf("%s\n", argv[1]);  // dangerous
```

### ❌ Forgetting arguments are strings

```c
int x = argv[1];  // wrong
```

### ❌ Not handling missing input

Programs should not assume arguments exist.

## 9\. Mental Model

Think of your program like this:

```text
User input → terminal → argv[] → your program processes it
```

## 10\. Practice Thinking

Try to reason through these:

1.  If you run:
    
    ```bash
    ./program 10 20 30
    ```
    
    How many arguments are there?
    
2.  How would you process each argument one by one?
    
3.  What happens if the user provides no arguments?
    

## Key Ideas to Remember

*   Use `argv[i]` to access arguments
    
*   Use loops to process multiple arguments
    
*   Always check `argc` before accessing
    
*   Convert strings when needed
    
*   Handle input safely
    

## What’s Next

Now that you can **work with arguments**, the final step is understanding:

> How `main` is structured and how real programs use arguments

In the next lesson, we will cover:

*   different `main` function forms
    
*   handling unused variables
    
*   real-world command-line applications
    

That’s where everything connects to real tools you already use.
