Ultimate Guide to Understanding Command Line Arguments in C
Command Line Arguments in C
Parameters and Arguments:
Parameters are variables which are useful to receive data from outside into a function.The data or values passed to the function are called arguments.
There are 4 ways of declaring the main() function – Command Line Arguments in C
- main() function can be declared without using any parameters, as: void main()
- main() can be declared with void type parameter, as: void main(void)
- It can also be declared using two parameters, as: void main(int argc, char *argv[])
- It can be decalred using three parameters, as: void main(int argc, char *agv[], char *envp[])
main() without any parameters
We can write main() function without mentioning any parameters after the function name in the braces, as:
void main()
{
statements;
}
main() with void type parameter – Command Line Arguments in C
Another way to write the main() function is with void type parameter.
void main(void)
{
statements;
}
‘void’ is like a datatype that tells the compiler that no value is accepted by this function. It compiles only when this function is called without any value.
main() with two parameters
It is possible to write main() function with two parameters,as
void main(int argc, char *argv[])
{
statements;
}
Here, the first parameter ‘argc’ is an integer type paramter that represents the argument count. The second parameter ‘argv’ is a character type pointer array that reprsents argument vector. argv[0] represents the program name, argv[1] reprsents the first value passed to the program, argv[2] represents the second value passed to the program.
main() with three parameters
We can also write the main() function with 3 parameters.
void main(int argc, char *argv[], char *envp[])
Here, argc represents the argument count, argv represents a character type array that stores the arguments. The third parameter is ‘envp’ which is a character type pointer array. envp represents environment of the process. envp array contains the values of environment variables in our computer system.
envp[0] represents the first environment variable value, envp[1] represents the second environment variable value, envp[2] represents the third environment variable value, etc.
The environment variables are operating system variables which contain certain system specific information like PATH, CLASSPATH, processor information and user information.
Calling another program from main()
system() function in C is useful to call one program from another program. system() can also call a DOS command or batch file from a C program. This function is available in <process.h> and <stdlib.h>. To use system() function, we can write:
system("DOS command");
For example, system(“dir”); will execute the ‘dir’ command. dir command will display directory contents. In the same way we can call a program ‘progr1.exe’ from system() function,as:
system("progr1");
When system() is used, the compiler first goes to DOS prompt and searches for the program with the name ‘progr1.exe’ in the current directory, and then executes it.
Uses of Command line arguments:
Command line arguments enable us to pass values to main() function at the time of running program. Using envp parameter, it is possible to retrieve any computer environment variables. Command line arguments provide a way to construct commands.