Command Line Arguments provide a way to pass a set of arguments into a program at run time. When we execute certain commands from the operating system command line, we normally provide additional inputs along with the name of the program that we want to execute.
Example: C:\TC\BIN>sum 10 12
Command line arguments are the
arguments which are passed from the operating system’s command line during the
time of execution. It is an important concept in C programming. It is mostly
used when we want to pass the values to the program from outside and do not
want to use it inside the code.
Command line arguments are very helpful to any
programmer. With command line arguments, we can give data from the terminal of
our operating system.
We can give the input to the program through the command line. The arguments passed from command line are called command line arguments. These arguments are passed to the main () function of the program to be executed. The operating system passes the command line arguments to the main() function as two arguments. These two arguments are referred to as argc and argv in the main() function.
- argc: It is known as argument count. It is an integer which counts the number of arguments passed. It counts the file name as the first argument.
- argv: It is a pointer array and it points to every argument which is being passed to the program.
For
example, C:\TC\BIN>sum
10 12
For this command: argc=3
argv[0]= C:\TC\BIN\SUM.EXE
argv[1]= 10
argv[2]= 12
Usage of Command Line Arguments in C:
Whenever there is a need to pass the values to the program from outside and do not want to use it inside the code, command line arguments can be used in C
· The program to be
executed can be controlled from the outside than hard coding the values inside
the program by making use of command line arguments.
The main () function must be defined as given below in order to receive the command line arguments,
int main(int argc, char *argv[])
- argc (Argument Count) is int and stores number of command-line arguments passed by the user including the name of the program. So if we pass a value to a program, 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 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.
Properties of Command Line Arguments:
- They are passed to main() function.
- They are parameters/arguments supplied to the program when it is invoked.
- They are used to control program from outside instead of hard coding those values inside the code.
- argv[0] denotes the name of the program.
- argv[1] points to the first command line argument .
- Till argv[argc-1], every argument is a command line argument.
Exercise: Program to find the sum of N integer numbers using command line arguments
The following are
the steps to execute a program with command line argument using Turbo C/C++
Compiler:
Step1:
Type the program
and save with the name sum.c
Step2: Compile the program by pressing F9
Then it will start
linking
Step3: Go to the command prompt (OS shell) by
selecting File-DOS shell.
Step 4: Pass command line arguments while
executing the program.
Programming Examples:
1. C
program to print program's name (Executable file):
The program will print the name of its executable file name (program name).
Output:
2. C
program to count argument and print all arguments given through command line:
In this program, we will count argument and print all given arguments given through command line, there are two variables argc which stores the total number of arguments and argv which stores the array of strings that mean all arguments including command name (program’s executable file name).
Output:
3. Program to find the sum of two integer numbers
using command line arguments in C:
In this program, we will provide the
input (two integer values) through the command line and program will add them.
Output:
atoi():
atoi() is a library function that converts string to integer, when
program gets the input from command line, string values transfer in the
program, we have to convert them to integers (in this program). atoi() is
used to return the integer of the string arguments.
exit(int)
Exit is a
function, which closes all the files and returns a value to the operating
system. The return value is 0 to indicate the normal termination of a program.
Any other number can be used to indicate errors.
Example:
exit(5) returns the
value 5 to the operating system. Normally 0 is returned if the program executes
successfully. The value can be made use of by the operating system to decide
whether to continue or to abort with a process.
Exit
function is defined in stdlib.h and process.h.
The
exit() function is usually called by the main() function.
0 Comments
if you have any doubts plz let me know...