CHAPTER 21
Beginner
Command Line Arguments
Updated: May 17, 2026
5 min read
# CHAPTER 21
Command Line Arguments
1. Introduction
Have you ever run a command in the terminal likegcc program.c -o program? The words program.c, -o, and program are inputs passed directly to the gcc program before it even starts running. In C, you can accept these inputs using Command Line Arguments.
2. Learning Objectives
By the end of this chapter, you will be able to:-
Understand the parameters of the
main()function.
-
Explain the purpose of
argcandargv.
- Access and parse arguments passed from the terminal.
- Convert string arguments into integers.
3. Modifying the main() Function
Until now, we have written int main(). To accept command-line arguments, we must give main two specific parameters:
c
4. What are argc and argv?
-
argc(Argument Count): An integer representing the total number of arguments passed. Note: The name of the program itself counts as the first argument (soargcis always at least 1).
-
argv(Argument Vector): An array of character pointers (an array of strings). It holds the actual arguments.
-
argv[0]holds the name of the program.
-
argv[1]holds the first argument,argv[2]holds the second, and so on.
5. Example: Printing Arguments
Let's write a program (echo.c) that simply repeats back what you tell it.
c
Running it in the terminal:
bash
Output:
text
6. Converting String Arguments to Integers
All arguments inargv are passed as strings (char *). If you pass 10, it is seen as "10". To use it in math, you must convert it to an integer using the atoi() (ASCII to Integer) function from <stdlib.h>.
c
Running it:
bash
7. Common Mistakes
-
Assuming
argv[1]exists: If the user runs the program without arguments, accessingargv[1]will crash the program (Segmentation Fault). Always checkargcfirst!
-
Doing math on strings: Attempting
argv[1] + argv[2]will add their memory addresses, not their numeric values. Useatoi()oratof().
8. Exercises
- 1. Write a program that takes one string argument (a name) and prints "Welcome, [name]!". If no name is provided, print "Welcome, Guest!".
-
2.
Write a command-line calculator that takes three arguments:
./calc 10 + 5. (Hint: checkargv[2][0]for the operator).
9. MCQ Quiz with Answers
Question 1
What does argc stand for?
Question 2
What data type is argv?
Question 3
What does argv[0] contain?
Question 4
If you run ./app file.txt, what is the value of argc?
Question 5
Which function converts a string argument to an integer?
Question 6
Which header file is required to use atoi()?
Question 7
What happens if you access argv[1] when argc is 1?
Question 8
If you pass -v to your program, how is it stored in argv?
Question 9
Can main take a third argument?
Question 10
How can you read a float from the command line?
10. Interview Questions
-
Q: Explain the purpose and data types of
argcandargv.
- Q: How do you protect your program from crashing if a user forgets to provide command-line arguments?
-
Q: What is the difference between
argvbeing declared aschar *argv[]andchar argv? (Answer: They are identical in this context).
11. Summary
Command-line arguments allow users to pass data to a program before it executes.argc holds the number of arguments, while argv is an array of strings holding the arguments themselves. Always validate argc before accessing argv to prevent crashes.