In the previous lesson, you learned:
What command-line arguments are
What
argcandargvmeanHow 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
argvhandle 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:
If you run:
./program 10 20 30How many arguments are there?
How would you process each argument one by one?
What happens if the user provides no arguments?
Key Ideas to Remember
Use
argv[i]to access argumentsUse loops to process multiple arguments
Always check
argcbefore accessingConvert strings when needed
Handle input safely
What’s Next
Now that you can work with arguments, the final step is understanding:
How
mainis structured and how real programs use arguments
In the next lesson, we will cover:
different
mainfunction formshandling 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.
Comments
Join the discussionNo comments yet. Be the first to comment.
C Programming
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.
