Skip to main content
C Programming Basics
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 like gcc 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 argc and argv.
  • 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
1234
int main(int argc, char *argv[]) {
    // Code goes here
    return 0;
}

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 (so argc is 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
123456789101112
#include <stdio.h>

int main(int argc, char *argv[]) {
    printf("Program Name: %s\n", argv[0]);
    printf("Total Arguments (including program name): %d\n\n", argc);

    for (int i = 1; i < argc; i++) {
        printf("Argument %d: %s\n", i, argv[i]);
    }

    return 0;
}

Running it in the terminal:

bash
12
$ gcc echo.c -o echo
$ ./echo hello world 123

Output:

text
123456
Program Name: ./echo
Total Arguments: 4

Argument 1: hello
Argument 2: world
Argument 3: 123

6. Converting String Arguments to Integers

All arguments in argv 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
12345678910111213141516
#include <stdio.h>
#include <stdlib.h> // Required for atoi()

int main(int argc, char *argv[]) {
    if (argc != 3) {
        printf("Usage: ./add num1 num2\n");
        return 1; // Exit with error
    }

    // Convert string arguments to integers
    int num1 = atoi(argv[1]);
    int num2 = atoi(argv[2]);

    printf("Sum: %d\n", num1 + num2);
    return 0;
}

Running it:

bash
12
$ ./add 15 20
Sum: 35

7. Common Mistakes

  • Assuming argv[1] exists: If the user runs the program without arguments, accessing argv[1] will crash the program (Segmentation Fault). Always check argc first!
  • Doing math on strings: Attempting argv[1] + argv[2] will add their memory addresses, not their numeric values. Use atoi() or atof().

8. Exercises

  1. 1. Write a program that takes one string argument (a name) and prints "Welcome, [name]!". If no name is provided, print "Welcome, Guest!".
  1. 2. Write a command-line calculator that takes three arguments: ./calc 10 + 5. (Hint: check argv[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 argc and argv.
  • Q: How do you protect your program from crashing if a user forgets to provide command-line arguments?
  • Q: What is the difference between argv being declared as char *argv[] and char 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.

12. Next Chapter Recommendation

In
Chapter 22: Linked Lists in C**, we will move beyond standard arrays and build dynamic, expandable lists using pointers and structs.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·