Skip to main content

How Programs Receive Input (argc & argv Basics)

Updated 4 min readMarkdown
Part of seriesC Programming

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

./program

But you can also do this:

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

int main(void)

But now, we use a different version:

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:

./program hello world

Then:

argc = 3

Why 3?

Because:

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:

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:

./program 10

Inside your program:

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.

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

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

Output:

Number of arguments: 3
Program name: ./program

8. Visualizing What Happens

When you run:

./program hello world

Think of it like this:

argc = 3

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

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

9. Why This Matters

Without arguments:

int x = 5;

👉 Fixed value (not flexible)

With arguments:

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

argv[0] = program name

Even if you pass no arguments:

./program

You still get:

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:

if (argc > 5)

11. Practice Thinking (Before Coding)

Try to reason through these:

  1. If you run:

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

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.

C Programming

Part 35 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 nextWorking With Command-Line Arguments in PracticeIn 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 us

More from this blog