How Programs Receive Input (argc & argv Basics)
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! 🚀
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:
If you run:
./program apple banana mangoWhat will
argcbe?What will
argv[2]contain?If no arguments are passed, what is the value of
argc?
Key Ideas to Remember
Programs can receive input from the command line
argccounts how many arguments were passedargvstores those arguments as stringsargv[0]is always the program nameAll 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.