Command-Line Arguments
Explore how to utilize command-line arguments in C to pass data into programs from the terminal. Understand the roles of argc and argv in handling inputs, converting string arguments to integers, and using them to control program behavior. This lesson helps you write flexible C programs that interact with user inputs outside the code.
We'll cover the following...
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:
-
int argc -
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 ...