Sunday, August 23, 2020

Command Line Arguments

 8.1 Introduction to C Command Line Arguments


  • The arguments that pass the main () function to a C program are called command line arguments. 
  • These arguments are passed when running the program.
  • So far you have define the main () function in C programs several times. 
  • The main ()function is the most important function in any program. 
  • But do you know that the main () function can also be passed arguments like a normal function? Passing arguments to the main () function in a program can be beneficial for a number of reasons. 
  • For example, if you want to create an application that executes commands and accordingly perform operations accordingly, it can be done with the main function of the argument by passing the argument. 
  • Apart from this, one benefit is that before you run the program, you tell what you want to do. 
  • When you use the command line arguments, you do not need to wait for the input window, you provide the input from the outside of the program and accordingly, the program is executed accordingly. 
  • A program can be controlled from the command line arguments externally and can run the application by passing commands (arguments). 
  • For example, if you are creating a program that starts and stops a process, you can use  command line arguments for it, and you can process and control just by starting or stop commands while running the program.

Syntax of C Command Line Arguments :-


To define command line arguments in C programs, you can use syntax being used.

int main(int argc, char *argv[])
        {
//rest of code
        }



  • In the above code, the main () function has been defined with command line arguments. 
  • When you want to accept command line arguments in your program, then argc and argv parameters define for you as it has been done in the code given above. 
  • In this, argc is an integer number which states how many arguments are being passed to the main () function. Also argv is a pointer array that points to pass arguments. 
  • One thing you should always keep in mind is that the argv [] array of zero index (argv [0]) always tells the name of the program and the first index (argv [1]) represents the first command line argument. 
  • To pass the command line arguments, you write the arguments after the name of the program, separated by comma. Its syntax is being given below.


program-name arg1,arg2, arg3...argN;


  • If no argument is passed then the value of argc is 1. 
  • If you pass an argument then the value of argc is 2. 
  • Similarly, the arguments that you pass through, the value of argc is more than that.

Example of C Command Line Arguments


  • The use of the command line arguments in C language is being explained by the following example.

#include<stdio.h>
/* Defining command line arguments in main() function */
int main(int argc, char *argv[])
{
printf(“Hello %s”,argv[1]);
}



  • When executing the program given above, the name of the program passes its name as the user after the name of the program and it shows the welcome message to the program user.

cmdlineDemo Reader


The above example produces the below given output.


Hello


No comments:

Post a Comment

Please do not any spam link in Comment Box