command line arguments in c

It is very important to understand command line arguments in c language in order to crack an interview round of many companies.

The process of passing a parameter or parameters to a program during execution of a program or runtime of a program is called as command line arguments in c. In other words, the Command line argument is a parameter which is passed to a program during execution. It is an important concept in C programming. It is possible to pass some values from the command line to your C program whenever that program executes. These values are called command line arguments in c and the other name of an argument is a parameter.

So what is a parameter or parameters?

A parameter is a singular of parameters (grammatically). However, the original definition goes here: A parameter is just a value which is passed to a program in the parenthesis of a function during execution of a program through command line. A parameter is also called as an argument.

So when to use command line arguments in c?

Many times it becomes necessary to control a program from outside instead of hardcoding those values inside the program. In that situation, you need to use the command line argument method to implement your program.

Components in command line arguments in c

There are two important components in command line arguments in c programming. It is eminent to note that command line arguments in c language are handled using the main() function.

  • agrc 
  • argv[]

Look At Above Components One By One

  • argc: The argc stands for argument count. It refers to the number of arguments or parameters passed.
  • argv[]: The argv[] stand for argument vector. It is a pointer array or an array of a pointer which points to each argument passed to the program.

Syntax

The syntax of command line arguments looks like this:

int main(int argc, char *argv[])

As you can clearly see in the above syntax that argc (argument counter ) and argv[] (argument vector) is passed as parameters inside the main function. The asterik (*) is prefixed before argv[] to instruct the compiler this is a special type of variable and that is called pointer variable. The data type of argc is an integer because arguments passed inside the function is always an integer. For example, it may be 1,2,3 (all are an integer) but never be 1.3 (this is just meaningless).

Example

#include <stdio.h>
#include <conio.h>
int main(int argc, char *argv[])
{
    if( argc == 2 )
     {
      printf("The argument passed is %s\n" , argv[1]);
     }
    else if(argc >2)
     {
      printf("Too many arguments are passed\n");
     }
     else
     printf("One Argument is needed");
return 0;
}

Now there Arise 3 Cases:

First Case: No argument is Passed

When the above code is compiled and executed without passing any parameter, it gives the following result.

OUTPUT: One Argument is needed

Second Case: One argument is Passed 

When the above code is compiled and executed by passing the only one parameter, it gives the following result.

OUTPUT: The argument passed is Test_Program(name of program)

First Case: Two arguments are Passed

When the above code is compiled and executed by passing two parameters, it gives the following result.

OUTPUT: Too many arguments are passed

Important Points

  • The argv[0] holds the name of the program itself and argv[1] is a pointer to the first command line argument supplied, and *argv[n] is the last argument.
  • The default value of argc is zero. In other words, if no arguments are passed, argc will be one, and if you will pass one argument then the value of argc will be 2.

What if you want to pass all arguments in one go?

Well, its possible to pass all the arguments in one go. You can pass all the arguments through the command line by separating each argument by space. For example, argument1 argument2 (not like argument1argument2).

What happens when the argument itself has space?

When the argument itself has space then you can pass them in two ways. One way is put all the arguments inside double quotes (” “) and other is inside single quote (‘ ‘).

For example, “argument1 argument2” or ‘argument1 argument2’.

Your friends are waiting for this content. Please Share To Aware Them!