Skip to main content

Working With Command-Line Arguments in Practice

Updated 4 min readMarkdown
Part of seriesC Programming

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:

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

Example

If you run:

./program apple banana

Then:

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:

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

Example Program

#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:

./program hello world

Output:

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

3. Checking If Arguments Exist

Before using an argument, always check if it exists.

Example

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

👉 Why?

Because this will crash:

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()

#include <stdlib.h>

int num = atoi(argv[1]);

Example Program

#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:

./program 25

Output:

Number: 25

5. Important Warning About atoi()

If the input is not a number:

./program hello

Then:

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

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:

./program start
./program stop

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

8. Common Beginner Mistakes

❌ Accessing arguments without checking

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

❌ Forgetting arguments are strings

int x = argv[1];  // wrong

❌ Not handling missing input

Programs should not assume arguments exist.

9. Mental Model

Think of your program like this:

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

10. Practice Thinking

Try to reason through these:

  1. If you run:

    ./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.

About the author

  • Heya! 👋 I love helping people, and one of the best ways I do this is by sharing what I learn and build. I started as a pharmacist at a tertiary hospital in Northern Ghana, where I saw how much technology could improve healthcare. That sparked my interest in digital health. I taught myself to code, then joined the ALX Software Engineering program, which became a turning point. I went on to complete a Master of Health Informatics at the University of Toronto. Today I am a software and AI engineer and a health informatician, and I practise remotely as a Digital Health Pharmacist. I combine clinical experience with technical skills to build tools that solve real problems in health and beyond, and I write here about what I build and what I learn.

No comments yet. Be the first to comment.

More from this blog