# How Programs Receive Input (argc & argv Basics)

Up to this point, most of your C programs have looked like this:

```c
int x = 5;
```

You hardcode values directly inside the program.

That works… but it’s limited.

What if you want your program to behave differently **without editing the code every time**?

That’s where command-line arguments come in.

## 1\. Running Programs From the Terminal

When you run a program, you usually do this:

```bash
./program
```

But you can also do this:

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

Here, `hello` and `world` are extra inputs given to the program.

These are called:

> **Command-line arguments**

## 2\. What Are Command-Line Arguments?

Command-line arguments are:

> Values you pass to a program when you run it on the command line.

Instead of writing input inside your code, you pass it from the terminal.

This makes your programs:

*   more flexible
    
*   more reusable
    
*   more interactive
    

## 3\. How C Receives These Arguments

In C, arguments are received through the `main` function.

You may have seen this before:

```c
int main(void)
```

But now, we use a different version:

```c
int main(int argc, char *argv[])
```

This allows your program to receive input.

## 4\. Understanding `argc`

`argc` stands for:

> **argument count**

It tells you **how many arguments were passed** to your program.

### Example

If you run:

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

Then:

```text
argc = 3
```

Why 3?

Because:

```text
argv[0] → ./program
argv[1] → hello
argv[2] → world
```

👉 The program name is always counted.

## 5\. Understanding `argv`

`argv` stands for:

> **argument vector**

It is an **array of strings** that stores all arguments.

Each argument is stored like this:

```text
argv[0] → program name
argv[1] → first argument
argv[2] → second argument
...
```

## 6\. Important Insight

> All command-line arguments are stored as **strings**

Even if you type:

```bash
./program 10
```

Inside your program:

```text
argv[1] = "10"
```

Not the number `10`, but the string `"10"`.

We will learn how to convert it later.

## 7\. A Simple Example Program

Let’s write a basic program to see this in action.

```c
#include <stdio.h>

int main(int argc, char *argv[])
{
    printf("Number of arguments: %d\n", argc);
    printf("Program name: %s\n", argv[0]);

    return 0;
}
```

### Compile and run:

```bash
gcc program.c -o program
./program hello world
```

Output:

```text
Number of arguments: 3
Program name: ./program
```

## 8\. Visualizing What Happens

When you run:

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

Think of it like this:

```text
argc = 3

argv = [
    "./program",
    "hello",
    "world"
]
```

Your program now has access to everything passed from the terminal.

## 9\. Why This Matters

Without arguments:

```c
int x = 5;
```

👉 Fixed value (not flexible)

With arguments:

```bash
./program 5
```

👉 Value comes from the user

This allows you to build programs like:

*   calculators
    
*   file processors
    
*   command-line tools
    
*   utilities like `gcc`, `ls`, etc.
    

## 10\. Common Beginner Confusions

* * *

### ❌ “Why is argc always at least 1?”

Because:

```text
argv[0] = program name
```

Even if you pass no arguments:

```bash
./program
```

You still get:

```text
argc = 1
```

### ❌ “Why are arguments strings?”

Because the terminal passes everything as text.

If you need numbers, you must convert them.

### ❌ “Can I access argv\[5\] directly?”

Only if it exists.

Always check:

```c
if (argc > 5)
```

## 11\. Practice Thinking (Before Coding)

Try to reason through these:

1.  If you run:
    
    ```bash
    ./program apple banana mango
    ```
    
    What will `argc` be?
    
2.  What will `argv[2]` contain?
    
3.  If no arguments are passed, what is the value of `argc`?
    

## Key Ideas to Remember

*   Programs can receive input from the command line
    
*   `argc` counts how many arguments were passed
    
*   `argv` stores those arguments as strings
    
*   `argv[0]` is always the program name
    
*   All inputs are strings
    

## What’s Next

Now that you understand **how input reaches your program**, the next step is:

> **How to actually use and process these arguments in real programs**

In the next lesson, you’ll learn how to:

*   access each argument
    
*   loop through them
    
*   safely work with user input
    

That’s when your programs become truly dynamic.
