Command-line Arguments

Learn how to pass arguments to your program using the command-line.

Command-line arguments are those which are not part of the written code, but instead, are received from the terminal in which we compile our code. These arguments can then be used inside the code.

The main function

So far we have been using the main function without any parameters (which is what the void keyword in the parameter list signifies). In modern C, main can also be defined using two parameters that are traditionally named as:

  1. int argc

  2. char *argv[]

When the program is executed, the first parameter argc contains the number of command-line arguments passed to the program plus one.

The second parameter argv is a one-dimensional array of strings, where argv[0] (the first element in the array) is always the name of the program being executed.

Create a free account to access the full course.

By signing up, you agree to Educative's Terms of Service and Privacy Policy