Command Line Arguments in C++
In this lesson: Pass arguments to a C++ program with argc and argv — run from terminal with examples. In Hindi & English.
What are command line arguments?
Most beginner programs ask for input with cin after they start. Command line arguments are a different way to give input: you type the values after the program's name when you *launch* it.
./result Aman 78 85 91
Here Aman, 78, 85 and 91 are handed to the program before it starts running. Real tools work this way — g++ main.cpp -o main passes three arguments to the compiler. Arguments are ideal for file names, options and scripts that run without a human typing input.
The main() signature
To receive arguments, write main with two parameters:
int main(int argc, char* argv[]) // or, equivalently: int main(int argc, char** argv)
| Parameter | Meaning |
|---|---|
argc | Argument count — how many strings were passed, *including* the program name |
argv | Argument vector — array of C-strings; argv[0] is the program name |
argv[argc] | Always a null pointer (end marker) |
For ./result Aman 78 85 91: argc is 5, argv[0] is "./result", argv[1] is "Aman", … argv[4] is "91".
Example 1: print every argument
#include <iostream>
int main(int argc, char* argv[]) {
std::cout << "argc = " << argc << "\n";
for (int i = 0; i < argc; i++) {
std::cout << "argv[" << i << "] = " << argv[i] << "\n";
}
return 0;
}g++ args.cpp -o args ./args hello "Class 12" 2026
Quotes group words with spaces into a single argument: "Class 12" is one argument.
Example 2: numbers from arguments
Arguments are always text. Convert them with std::stoi (int) or std::stod (double), and check the count first.
#include <iostream>
#include <string>
#include <stdexcept>
int main(int argc, char* argv[]) {
if (argc < 3) {
std::cerr << "Usage: " << argv[0] << " <name> <mark1> [mark2 ...]\n";
return 1;
}
std::string name = argv[1];
int total = 0;
try {
for (int i = 2; i < argc; i++) {
std::size_t used = 0;
int mark = std::stoi(argv[i], &used);
if (argv[i][used] != '\0') // "7x" is not a valid mark
throw std::invalid_argument(argv[i]);
total += mark;
}
} catch (const std::exception& e) {
std::cerr << "All marks must be whole numbers.\n";
return 1;
}
int subjects = argc - 2;
std::cout << name << ": total " << total
<< ", average " << static_cast<double>(total) / subjects << "\n";
return 0;
}./result Aman 78 85 91
The program returns 1 on bad input — a non-zero exit code tells scripts that something went wrong.
Copying arguments into a vector
Working with raw char* is awkward. Copy them into modern types once:
#include <string>
#include <vector>
int main(int argc, char* argv[]) {
std::vector<std::string> args(argv + 1, argv + argc); // skip program name
for (const auto& a : args) {
if (a == "--help") { /* show help */ }
}
return 0;
}Now you can compare with ==, use .size() and pass the vector to functions.
Passing arguments from an IDE
| Tool | Where to set arguments |
|---|---|
| Terminal / Command Prompt | program.exe arg1 arg2 or ./program arg1 arg2 |
| Code::Blocks | Project → Set programs' arguments |
| Dev-C++ | Execute → Parameters |
| Visual Studio | Project Properties → Debugging → Command Arguments |
| VS Code | "args": ["arg1", "arg2"] in launch.json |
Where command line arguments are used
- Passing a file name:
./marks-report students.csv - Options and flags:
--verbose,-o output.txt - Automation: batch scripts and scheduled jobs that run without user input
- Competitive and practical exams sometimes read test sizes this way
Common mistakes
- Using
argv[1]without checkingargc— crashes when no argument is given. - Forgetting that
argv[0]is the program name, so real arguments start at index 1. - Comparing C-strings with
==(argv[1] == "--help"compares pointers). Usestd::string(argv[1]) == "--help"orstrcmp. - Treating arguments as numbers without converting them.
- Calling
std::stoiwithout handling the exception for non-numeric text — and forgetting thatstoi("7x")silently returns 7 unless you check how many characters were used.
Write a program calc that takes three arguments — a number, an operator (+ - x /) and another number — and prints the result, e.g. ./calc 12 x 5 prints 60. Print a usage message when the argument count is wrong and handle division by zero. (Use x for multiply because * is expanded by most shells.)
Summary
- Command line arguments are values typed after the program name when it is launched.
- Receive them with
int main(int argc, char* argv[]). argccounts arguments including the program name;argv[0]is the name.- Arguments are strings — convert with
stoi/stodand handle errors. - Always check
argcbefore readingargv[i].
Related: functions, strings, file handling, exception handling.
Frequently Asked Questions
What are argc and argv in C++?
argc (argument count) is the number of command-line arguments, including the program name. argv (argument vector) is an array of C-strings: argv[0] is the program name, argv[1] the first real argument, and so on up to argv[argc - 1]; argv[argc] is a null pointer.How do I convert a command line argument to an integer in C++?
std::stoi(argv[1]) for int, std::stod for double, or std::stol for long (from <string>). Wrap the call in try/catch because stoi throws std::invalid_argument if the text is not a number.How do I pass command line arguments in Code::Blocks or VS Code?
"args" array to launch.json, or simply run the compiled program from the terminal: ./program arg1 arg2.Is argc ever zero?
argc is at least 1 because argv[0] holds the program name. The C++ standard technically allows argc to be 0, so robust programs check argc before using argv[0].