Command Line Arguments in C

Command Line Arguments in C
Techiio-author
Written by Shuvhojit DebFebruary 1, 2022
6 min read
C
0 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 0 VIEWS SHARE
Techiio-author
Shuvhojit Deb

Full Stack Developer

Let's know what are the Command Line Arguments in C.

Command Line Arguments in C

The arguments passed from command line are called command line arguments. These arguments are handled by main() function.

To support command line argument, you need to change the structure of main() function as given below.

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

Here, argc counts the number of arguments. It counts the file name as the first argument.

The argv[] contains the total number of arguments. The first argument is the file name always.

How Command-Line Arguments in C works?

  • Whenever there is a need to pass the values to the program from outside and do not want to use it inside the code, we make use of Command-Line Arguments in C.
  • The values passed to the C program from the command line when the program is executed are called command-line arguments in C.
  • The main() function in the program handles the command line arguments passed to the program when the program is executed.
  • The number of arguments passed to the program is represented by argc and every argument passed to the program is pointed by a pointer which is maintained in a pointer array represented by argv[].

Example

Let's see the example of command line arguments where we are passing one argument with file name.

#include <stdio.h>  
void main(int argc, char *argv[] )  {  
  
   printf("Program name is: %s\n", argv[0]);  
   
   if(argc < 2){  
      printf("No argument passed through command line.\n");  
   }  
   else{  
      printf("First argument is: %s\n", argv[1]);  
   }  
}  

Run this program as follows in Linux:

./program hello 

Run this program as follows in Windows from command line:

program.exe hello  

Output:

Program name is: program
First argument is: hello

If you pass many arguments, it will print only one.

./program hello c how r u  

Output:

Program name is: program
First argument is: hello

But if you pass many arguments within double quote, all arguments will be treated as a single argument only.

./program "hello c how r u"  

Output:

Program name is: program
First argument is: hello c how r u

You can write your program to print all the arguments. In this program, we are printing only argv[1], that is why it is printing only one argument.

Advantages of Command-Line Arguments in C

There are several advantages mentioned below :

  • 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.

Conclusion

In this tutorial, we understand the concept of Command-Line Arguments through definition, syntax, and working of Command-Line Arguments through programming examples and their outputs and the advantages of using Command Line Arguments in our program to control the program from outside than hard-coding the values inside the program.

C Language
Command Argument
C Command
Programming
0 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 0 VIEWS SHARE
Was this blog helpful?
techiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plan
You must be Logged in to comment
Code Block
Techiio-logo

Techiio is on the journey to build an ocean of technical knowledge, scouring the emerging stars in process and proffering them to the corporate world.

Follow us on:

Subscribe to get latest updates

You can unsubscribe anytime from getting updates from us
Developed and maintained by Wikiance
Developed and maintained by Wikiance