Skip to main content

Command Line Arguments

So far we have using main with an empty pair of brackets. But like any other function it is possible to pass argument or parameters to main when it starts executing i.e. at runtime. These arguments are called command line arguments because they are passed to main() from the command line during execution. 
main(int argc, char * argv[]) 
{ ------- 
------- 
 int argc: Argument count which is the number of command line arguments that are passed to function main(). 
  char *argv[]: argument vector it is an array of pointer each pointing to a command line argument. 
  i. The subscripts for argv[] are 0 to argc – 1. 
  ii. argv[0] is the name of the executable file of the program (./a.out).
It is not necessary to use the words argc and argv. Any other identifiers can be used. The arguments can have to be separated by white spaces.  
Example : Consider a program“Display.c” which displays the command line arguments on the screen. If the command given is given as Display Argument 10 abcd 
The output will be : Argument 10 abcd 
For this example, argc = 4 and the arguments will be stored as shown below. argv[0] is the name of the program. argv[1] to argv[3] store the actual command line arguments which are passed. 
Note: In Linux, the command will be: #./a.out Argument10 abcd


C Program using Command Line Arguments Display command line arguments 


#include<stdio.h> 
main(int argc, char *argv[]) 
int i; 
for(i=0; i<argc; i++) 
printf(“%s\t”, argv[i]); 

Display all command line arguments in reverse order. 

#include<stdio.h> 
main(int argc, char *argv[]) 
while(- - argc>=0) 
printf(“%s”,argv[argc]); 
}


Program which replaces all occurrences of a character in a string by another character 


#include<stdio.h> 
main(int argc, char *argv[]) 
{ int i; 
char ch1, ch2, str[80]; 
if(argc !=4) 
{ printf(“invalid number of arguments”); 
printf(“usage - <progname><string><character><character>”); 
exit(0); 
strcpy(str, argv[1]); 
ch1=argv[2] [0]; 
ch2=argv[3] [0]; 
for(i =0; str[i]!=’\0’;i++) 
if(str[i] ==ch1) 
str[i]=ch2; 
printf(“the replace string is %s”, str); 
}

Advantages of command line arguments

Arguments can be supplied during runtime. Therefore, the program can accept different arguments at different times. 

 There is no need to change the source code to work with different input to the program. 
 
  By using command line arguments, the program can be run with different file names every time since the code in the program will refer to them using argv[]. 
  
   There’s no need to recompile the program since the source code is not changed.


Comments

Trending⚡

Happy birthday Hardik Pandya | In C programming

  Happy birthday Hardik Pandya . Now you are  28 years old. Great achievement you have. Let's we want to talk more about Hardik pandya. He is great cricketer. Pandya is awesome. In this Blog Post we are going to wish pandya " Happy birthday using C program". Let's tune with us till end. Now we have to wish pandya, so we are going to use printf () function printing message to pandya as " Happy birthday Hardik pandya Now you are 28 years old". Hardik pandya was born on 11 October in 1993. Now we are going to declare a variable called as current_age = 2021 - 1993. It calculate current age Of Hardik pandya. See the "Happy birthday pandya" using c programming. If you liked this Blog Post then don't forget to share with your computer science learning friends. Once again " Happy birthday Hardik Pandya sir". Read also Happy Rakshabandhan wish using C program Friendship day 2021 greetings in C

What is programming explained in simple words

Hi my dear friends today in this blog post we are going to know what programming is? In simple words I will explain to you programming. Nowadays we are watching real life use of programming. How computers learn to speak, talk and do the specified complex task for us. We are all keen to know what is exactly programming? Programming is the process of creating instructions that a computer can understand and execute. These instructions, also known as code, are written in a programming language that is specific to the task at hand. The history of programming can be traced back to the mid-20th century, with the development of the first electronic computers. The first programming languages were known as machine languages, which were specific to a particular type of computer. As computers became more sophisticated, high-level programming languages were developed, such as FORTRAN and COBOL, which were easier for humans to read and write. These languages allow programmers to write code t

check number is prime or odd or even using c program

Here is the c program to check if the user entered number is prime ,even and odd. These few lines of code solve three problems. In the above program we used integer type num variable for storing user entered numbers. Then we used the IF condition statement. That's all. IF condition for even number In the First IF statement we have a logic. If the number is divided by two then the reminder should be 0 then the number is an even number else not an even number. That simple logic is implemented in the first if statement. IF condition for odd number In the second IF statement we Implemented odd number logic. We identify odd numbers just by making little change in even number logic. If the number is divided by two then the reminder should not be a zero. Then the number is odd. That's simple logic used to identify whether a number is odd or not an odd number. IF condition for prime number In the third IF condition we implemented the logic of the prime number. In this IF

Graph Data Structure

Graph A graph can be defined as a group of vertices and edges that are used to connect these vertices. A graph can be seen as a cyclic tree, where the vertices (Nodes) maintain any complex relationship among them instead of having parent child relationship. A graph G can be defined as an ordered set G(V, E) where V(G) represents the set of vertices and E(G) represents the set of edges which are used to connect these vertices. A Graph G(V, E) with 5 vertices (A, B, C, D, E) and six edges ((A,B), (B,C), (C,E), (E,D), (D,B), (D,A)) is shown in the following figure. Directed and undirected graph Graph terminology Graph Representation Directed Graph Adjancency Matrix Graph Traversal Depth first search algorithm Directed and undirected graph A graph can be directed or undirected. However, in an undirected graph, edges are not associated with the directions with them. An undirected graph does not have any edges in directions. If an edge exists between ver

How to write programs in Bhai language

Bhai Language Bhai language is fun Programming language , with this language you can makes jokes in hindi. Bhai language written in typescript. It's very funny , easy and amazing language. Keywords of this language written in Hindi . Starting and ending of the program Start program with keyword " hi bhai " and end with " bye bhai ". It's compulsory to add this keyword before starting and end on the program. You write your programming logic inside this hi bhai and bye bhai . How to declare variables in Bhai language We use " bhai ye hai variable_name" keyword for declaring variables. In javascript we use var keyword for declaring variables but here you have to use " bhai ye hai " keyword. If you are declaring string then use " " double quotes. You can use Boolean variable like sahi and galat for true and false . How to print output in Bhai language You have to use " bol bhai " keyword for