Command Line Arguments in C
Last Updated :
10 Jan, 2025
The most important function of C is the main() function. It is mostly defined with a return type of int and without parameters.
int main() {
...
}
We can also give command-line arguments in C. Command-line arguments are the values given after the name of the program in the command-line shell of Operating Systems. Command-line arguments are handled by the main() function of a C program.
To pass command-line arguments, we typically define main() with two arguments: the first argument is the number of command-line arguments and the second is a list of command-line arguments.
Syntax
int main(int argc, char *argv[]) { /* ... */ }
or
int main(int argc, char **argv) { /* ... */ }
Here,
- argc (ARGument Count) is an integer variable that stores the number of command-line arguments passed by the user including the name of the program. So if we pass a value to a program, the value of argc would be 2 (one for argument and one for program name)
- The value of argc should be non-negative.
- argv (ARGument Vector) is an array of character pointers listing all the arguments.
- If argc is greater than zero, the array elements from argv[0] to argv[argc-1] will contain pointers to strings.
- argv[0] is the name of the program , After that till argv[argc-1] every element is command -line arguments.
For better understanding run this code on your Linux machine.
Example
The below example illustrates the printing of command line arguments.
C
// C program named mainreturn.c to demonstrate the working
// of command line arguement
#include <stdio.h>
// defining main with arguments
int main(int argc, char* argv[])
{
printf("You have entered %d arguments:\n", argc);
for (int i = 0; i < argc; i++) {
printf("%s\n", argv[i]);
}
return 0;
}
Output
You have entered 4 arguments:
./main
geeks
for
geeks
for Terminal Input
$ g++ mainreturn.cpp -o main $ ./main geeks for geeks
Note: Other platform-dependent formats are also allowed by the C standards; for example, Unix (though not POSIX.1) and Microsoft Visual C++ have a third argument giving the program’s environment, otherwise accessible through getenv in stdlib.h. Refer C program to print environment variables for details.
Properties of Command Line Arguments in C
- They are passed to the main() function.
- They are parameters/arguments supplied to the program when it is invoked.
- They are used to control programs from outside instead of hard coding those values inside the code.
- argv[argc] is a NULL pointer.
- argv[0] holds the name of the program.
- argv[1] points to the first command line argument and argv[argc-1] points to the last argument.
Note: You pass all the command line arguments separated by a space, but if the argument itself has a space, then you can pass such arguments by putting them inside double quotes "" or single quotes ''.
Example
The below program demonstrates the working of command line arguments.
C
// C program to illustrate
// command line arguments
#include <stdio.h>
int main(int argc, char* argv[])
{
printf("Program name is: %s", argv[0]);
if (argc == 1)
printf("\nNo Extra Command Line Argument Passed "
"Other Than Program Name");
if (argc >= 2) {
printf("\nNumber Of Arguments Passed: %d", argc);
printf("\n----Following Are The Command Line "
"Arguments Passed----");
for (int i = 0; i < argc; i++)
printf("\nargv[%d]: %s", i, argv[i]);
}
return 0;
}
Output in different scenarios:
1. Without argument: When the above code is compiled and executed without passing any argument, it produces the following output.
Terminal Input
$ ./a.out
Output
Program Name Is: ./a.out
No Extra Command Line Argument Passed Other Than Program Name
2. Three arguments: When the above code is compiled and executed with three arguments, it produces the following output.
Terminal Input
$ ./a.out First Second Third
Output
Program Name Is: ./a.out
Number Of Arguments Passed: 4
----Following Are The Command Line Arguments Passed----
argv[0]: ./a.out
argv[1]: First
argv[2]: Second
argv[3]: Third
3. Single Argument: When the above code is compiled and executed with a single argument separated by space but inside double quotes, it produces the following output.
Terminal Input
$ ./a.out "First Second Third"
Output
Program Name Is: ./a.out
Number Of Arguments Passed: 2
----Following Are The Command Line Arguments Passed----
argv[0]: ./a.out
argv[1]: First Second Third
4. A single argument in quotes separated by space: When the above code is compiled and executed with a single argument separated by space but inside single quotes, it produces the following output.
Terminal Input
$ ./a.out 'First Second Third'
Output
Program Name Is: ./a.out
Number Of Arguments Passed: 2
----Following Are The Command Line Arguments Passed----
argv[0]: ./a.out
argv[1]: First Second Third
Similar Reads
Command Line Arguments in C++
Command-line arguments are arguments that are passed to a program when it is executed from the command line or terminal. They are provided in the command-line shell of operating systems with the program execution command. The main function of C++ generally can have the following signature: int main(
4 min read
getopt() function in C to parse command line arguments
The getopt() function is a builtin function in C and is used to parse command line arguments. Syntax: getopt(int argc, char *const argv[], const char *optstring) optstring is simply a list of characters, each representing a single character option. Return Value: The getopt() function returns differe
2 min read
Variable Length Argument in C
Variable length argument is a feature that allows a function to receive any number of arguments. There are situations where we want a function to handle variable number of arguments according to requirement. 1) Sum of given numbers. 2) Minimum of given numbers. and many more. Variable number of argu
3 min read
How to Count Variable Numbers of Arguments in C?
C supports variable numbers of arguments. But there is no language provided way for finding out total number of arguments passed. User has to handle this in one of the following ways: 1) By passing first argument as count of arguments. 2) By passing last argument as NULL (or 0). 3) Using some printf
2 min read
C Function Arguments and Function Return Values
Prerequisite: Functions in CA function in C can be called either with arguments or without arguments. These functions may or may not return values to the calling functions. All C functions can be called either with arguments or without arguments in a C program. Also, they may or may not return any v
5 min read
Variable length arguments for Macros
Like functions, we can also pass variable length arguments to macros. For this we will use the following preprocessor identifiers.To support variable length arguments in macro, we must include ellipses (...) in macro definition. There is also "__VA_ARGS__" preprocessing identifier which takes care o
2 min read
One Dimensional Arrays in C
In C, an array is a collection of elements of the same type stored in contiguous memory locations. This organization allows efficient access to elements using their index. Arrays can also be of different types depending upon the direction/dimension they can store the elements. It can be 1D, 2D, 3D,
5 min read
Char Comparison in C
Char is a keyword used for representing characters in C. Character size in C is 1 byte. There are two methods to compare characters in C and these are: Using ASCII valuesUsing strcmp( ) .1. Using ASCII values to compare characters The first method is pretty simple, we all know that each character ca
3 min read
Assignment Operators in C
In C, assignment operators are used to assign values to variables. The left operand is the variable and the right operand is the value being assigned. The value on the right must match the data type of the variable otherwise, the compiler will raise an error.Let's take a look at an example:C#include
4 min read
Character Arithmetic in C
As already known character range is between -128 to 127 or 0 to 255. This point has to be kept in mind while doing character arithmetic. What is Character Arithmetic?Character arithmetic is used to implement arithmetic operations like addition, subtraction, multiplication, and division on characters
2 min read