Command Line Arguments in C
C language offers various types of functionalities and features to programmers. Programmers find C useful for solving several problems. It is very easy to implement because of its simplicity and easily understandable syntaxes.
But in C, we can also give command-line arguments to the program from the terminal.
Let us learn more about the command Line Arguments in C.
What are Command Line Arguments in C?
Command line arguments are the arguments which the user gives from the operating system’s command line during the time of execution. Earlier, we used main() functions without arguments. These command line arguments are handled by the main() function.
If you want to pass command line arguments then you will have to define the main() function with two arguments. The first argument defines the number of command line arguments and the second argument is the list of command line arguments.
Syntax:-
returnType_Function (*pointer_name) ( list_of_arguments)
In the above syntax, we have to specify the return type of the function such as int, void, double etc. Then, give the name to the function pointer and then list the arguments.
Syntax:-
void ( *pointerToFunction ) ( int );
In the above syntax, pointerToFunction is the pointer to a function. It takes an integer argument and the return type is void.
Example:-
int main(int argc, char *argv[] ) { // body; }
- argc:- It is known as argument count. It is int. It stores the number of the command line arguments passed by a user from the terminal and also stores the name of the program. The value of argc must not be negative.
- argv:- It is a pointer array and it points to every argument which is being passed to the program. Argv[0] denotes the name of the program.
- Till argv[argc-1], every argument is a command line argument.
Working of Function Pointer in C:-
Let’s see how a function pointer works with some examples.
Example:-
#include <stdio.h> int cal_sum(int n1, int n2) { return n1+n2; } int main() { int(*functionPointer)(int, int) ; functionPointer = cal_sum ; //Calling using the function pointer! int res1 = functionPointer(3,69) ; //Calling function in a normal way! int res2 = cal_sum(5,69) ; printf ( "Through function pointer: %d\n" ,res1) ; printf ( "Through function name: %d\n" ,res2) ; return 0 ; }
Output:-
Through function name: 74
In the above example, we have declared a method named cal_sum which takes two parameters of int data type and adds them. We also declared a function pointer named functionPointer which takes two inputs from the user.
We got two outputs. One is through calling the function pointer and the second one is through calling the function name.
Example:-
#include <stdio.h> void subs(int n1,int n2) { printf ("The subtraction between n1 and n2 is %d!\n", n1-n2); } int main() { void (*funcpoint[])(int,int) = {subs} ; int n1=4,n2=2; printf("TechVidvan Tutorial: Doing subtraction using Function pointer!\n"); (*funcpoint[0])(n1,n2) ; return 0 ; }
Output:-
The subtraction between n1 and n2 is 2!
In the above example, we created a function named subs separately above the main function. We also declared a function pointer with two integer types n1 and n2.
Example:- Command Line Arguments
#include <stdio.h> void main(int argc, char *argv[] ){ printf("TechVidvan Tutorial: Example of Command Line Arguments in C!\n"); printf("Program name is: %s\n",argv[0]); printf("Arguments passes: %d\n", argc); if(argc < 2){ printf("You did not pass any arguments\n"); } else{ printf("First argument: %s\n", argv[1]); } }
Input:-
./a.out Hi
Output:-
Program name is: ./a.out
Arguments passes: 2
First argument: Hi
Properties of Command-Line arguments in C:-
- argv[0] prints the name of the program.
- argv[1] prints the first argument which is provided by the user.
- argv[argc] is a null pointer.
- Always passed to the main() function.
- Command Line Arguments are passed by the user from the terminal.
- It is used to control programs from the outside.
Example:- Command Line Arguments in C
#include<stdio.h> int main(int argc,char* argv[]) { int i; printf("Program Name is: %s\n",argv[0]); printf("TechVidvan Tutorial: Using Command Line Arguments in C!\n\n"); if(argc==1) printf("You did not provide any arguments!\n"); if(argc>=2) { printf("Arguments Passed: %d.\n\n",argc); printf("!!!!!!!!Below are the arguments which you passed!!!!!!!!\n"); for(i=0;i<argc;i++) printf("argv[%d]: %s\n",i,argv[i]); } return 0; }
Output in Different Scenarios:-
- Without Argument:- When you did not pass any arguments through the command line then it prints the following output.
Input:-
./a.out
Output:-
TechVidvan Tutorial: Using Command Line Arguments in C!You did not provide any arguments!
- Single Argument:- When you pass a single argument by space but inside double quotes then it will produce the following output.
Input:-
./a.out “TechVidvan Tutorial”
Output:-
TechVidvan Tutorial: Using Command Line Arguments in C!Arguments Passed: 2.!!!!!!!!Below are the arguments which you passed!!!!!!!!
argv[0]: ./a.out
argv[1]: TechVidvan Tutorial
- Three Arguments:- When you pass three arguments to the program through the terminal then it will produce the following output.
Input:-
./a.out How You doing
Output:-
TechVidvan Tutorial: Using Command Line Arguments in C!Arguments Passed: 4.!!!!!!!!Below are the arguments which you passed!!!!!!!!
argv[0]: ./a.out
argv[1]: How
argv[2]: You
argv[3]: doing
Single Argument:- When you pass a single argument by space but inside single quotes then it will produce the following output.
Input:-
./a.out ‘TechVidvan Tutorial’
Output:-
TechVidvan Tutorial: Using Command Line Arguments in C!Arguments Passed: 2.!!!!!!!!Below are the arguments which you passed!!!!!!!!
argv[0]: ./a.out
argv[1]: TechVidvan Tutorial
Usage of Command-Line Arguments in C:-
- It allows the use of standard input and output so that we can chain commands using shell.
- It is used to simplify the installation and use of simple programs.
Read Data from user:-
We use scanf() function to read data from the user. Following is the program to get data from the user using the scanf() function.
Example:- scanf() function
#include <stdio.h> int main() { int x; scanf("%d", &x); printf("TechVidvan Tutorial: Reading data from the user using scanf()!\n"); printf("Value of x is: %d\n",x); return 0; }
Output:-
Value of x is: 20
Summary
In this tutorial, we learnt about what command-line arguments are and how to use them in the C programming language. We also discussed the working of function pointers. And we also showed how you can use command-line arguments in your program code.
Command-line arguments are very helpful to any programmer. With command-line arguments, you can give data from the terminal of your operating system.