Command Line Argument in C

What is command prompt?
Ans: Command prompt is text mode interface by which user can enter the command from keyboard. It is non-graphical interface. These commands invoke the specific program .
What is command line argument?
Ans: With help of main function in c we can invoke the program. main function is called by operating system. In main function has the parameter. They are:
1. Argument counter
2. Argument vector
3. Environment vector
It is written as :
main (int argument_counter,char *argument_vector[ ],char *environment_vector[ ])
Argument counter:
It is integer type data type. It always count the number of argument written to execute the any command and store that number.
Example:
void main (int argument_counter )
{
printf(“Total number of argument :\t%d”,argument_counter);
}
Save this program .Let file name is count.c and compile and execute the program then
open run in window then write cmd and press enter. Now go to your current working directory (generally it is c:\tc\bin)





Now if you will use count command and argument like India,pak etc then output will be total number of argument including count.

Argument vector: It is array which contain address of char data type i.e address of all argument.
Example:
void main(int argument_counter,char *argument_vector[])
{
int i;
for(i=0;i{
printf("%s\n",argument_vector[i]);
}
}
Save this program as arg_vect.c. now write the command in command prompt.


It is displaying all the argument including arg_vect.


0 comments:

Post a Comment