Skip to main content

Command Palette

Search for a command to run...

Working With Command-Line Arguments in Practice

Published
4 min read
D

Heya! 👋 I love helping people, and one of the best ways I do this is by sharing my knowledge and experiences. My journey reflects the power of growth and transformation, and I’m here to document and share it with you.

I started as a pharmacist, practicing at a tertiary hospital in the Northern Region of Ghana. There, I saw firsthand the challenges in healthcare delivery and became fascinated by how technology could offer solutions. This sparked my interest in digital health, a field I believe holds the key to revolutionizing healthcare.

Determined to contribute, I taught myself programming, mastering tools like HTML, CSS, JavaScript, React, PHP, and more. But I craved deeper knowledge and practical experience. That’s when I joined the ALX Software Engineering program, which became a turning point. Spending over 70 hours a week learning, coding, and collaborating, I transitioned fully into tech.

Today, I am a Software Engineer and Digital Health Solutions Architect, building and contributing to innovative digital health solutions. I combine my healthcare expertise with technical skills to create impactful tools that solve real-world problems in health delivery.

Imposter syndrome has been part of my journey, but I’ve learned to embrace it as a sign of growth. Livestreaming my learning process, receiving feedback, and building in public have been crucial in overcoming self-doubt. Each experience has strengthened my belief in showing up, staying consistent, and growing through challenges.

Through this platform, I document my lessons, challenges, and successes to inspire and guide others—whether you’re transitioning careers, exploring digital health, or diving into software development.

I believe in accountability and the value of shared growth. Your feedback keeps me grounded and motivated to continue this journey. Let’s connect, learn, and grow together! 🚀

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.

C Programming

Part 5 of 40

From today, I will be starting lessons on C programming in my ALX Software Engineering class and I look forward to sharing with you everything I learn through this series.

Up next

How Programs Receive Input (argc & argv Basics)

Up to this point, most of your C programs have looked like this: int x = 5; You hardcode values directly inside the program. That works… but it’s limited. What if you want your program to behave diff