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⚡

Understanding link.click() in JavaScript

Hey! Today i am going to share with you how to use link.click() function in javascript As a JavaScript developer, you may come across the need to programmatically click a link on a web page. The link.click() method is a commonly used way to do this, and it is important to understand how it works and when to use it. What is link.click()? link.click() is a method that can be used to simulate a click on a link element in JavaScript. It is typically used when you want to trigger a link click event programmatically, rather than requiring the user to physically click the link. How to use link.click() Using link.click() is relatively straightforward. First, you need to select the link element you want to click using a DOM selector such as getElementById() or querySelector(). Then, you can call the click() method on the link element to simulate a click. Here is an example: // select the link element const myLink = document.getElementById('my-link'); // simulate a cl...

Policy vs Innovation Matrix - India

Policy vs Innovation Matrix Policy vs Innovation Matrix that shows the structural difference between policy intent and actual innovation output — and how India’s model is transforming to match global innovation systems . From Paper Policies to Product Power 🧱 Traditional Indian Model (Old System) Policy Layer Reality Innovation Outcome Policy Vision Strong national policies Vision without execution pipelines Funding Govt-dominated funding Limited capital availability Risk Culture Risk-averse systems No deep-tech experimentation Research Academic publications Low commercialization Industry Role Minimal R&D involvement Weak private innovation Infrastructure Limited labs & compute Research bottlenecks Talent High-quality brains Brain drain Commercialisation Weak tech transfer Research stays in journals Scale Fragmented schemes No national scale impact Speed Slow approvals Innovation delays Result: ➡ Policy existed ➡ Innovation did not scale ➡ Research stayed inside institutions ...

Quiz tells you what type of wife you want

What Type of Wife are You Looking For? Attend this quiz and know your wife expections Quiz reveals type of wife you expect, lets answer carefully... Personality Questions: 1. What type of personality are you looking for in a wife? Quiet and reserved Outgoing and social Intelligent and witty 2. What type of sense of humor are you looking for in a wife? Dry and sarcastic Witty and clever Playful and silly 3. What type of interests are you looking for in a wife? Intellectual and educational Creative and artistic Athletic and outdoorsy Values Questions: 4. What type of family values are you looking for in a wife? Traditional and conservative Open-minded and progressive Balanced and equal 5. What type of political views...

Pointer const and Constant Pointer

A constant pointer is a pointer that cannot change the address which it stores.   Syntax: datatype *const pointer_name;  Example:  int a=10,b=20;  int *const ptr;  ptr=&a;  ptr=&b; //here the address in ptr cannot be changed so not allowed . Pointer to constant  Address in the pointer can be changed but the value of the data element which the pointer points cannot be changed.  Syntax: const datatype * pointer_name;  Example:  int a=10,b=20;  const int *ptr=&a;  *ptr=100; //here the value which ptr points to cannot changed so not allowed ptr=&b; //allowed Pointers and Const  The previous two can be combined to form a constant pointer to constant  Syntax: const datatype *const pointer_name; Example:  const int * const ptr;  int a=10,b=20;  const int * const ptr=&a;  *ptr=100; //not allowed  ptr=&b;//not allowed  Here the value which ptr points to cannot be changed an...

India’s Landmark Era of Research, Innovation & Deep-Tech Growth

India’s research and development (R&D) ecosystem is undergoing a historic transformation. The country is shifting from a primarily public-funded research model to a private-sector-driven, innovation-led economy , backed by massive strategic investments. This transformation directly supports the national vision of Viksit Bharat @ 2047 , with Investment and Innovation identified as two of the four core growth engines in the 2025–26 Union Budget . The focus is clear: ➡ Build global innovation leadership ➡ Strengthen deep-tech infrastructure ➡ Attract private capital into strategic sectors ➡ Create a strong research-to-commercialisation pipeline 🧠 Strategic Institutional Foundations Anusandhan National Research Foundation (ANRF) Operational since 2024 Target: ₹50,000 crore mobilisation over 5 years Purpose: Bridge academia–industry collaboration Provide strategic national direction in science & technology Enable interdisciplinary research to solve complex societal challenges R...

The 175 Billion Parameter Question: 5 Surprising Lessons from GPT-3

The 175 Billion Parameter Question: 5 Surprising Lessons from GPT-3 Is scale alone enough to transform artificial intelligence? When GPT-3 launched with 175 billion parameters , it didn’t just break records — it reshaped how we think about intelligence itself. The End of Specialized AI: A Paradigm Shift For nearly a decade, artificial intelligence advanced through specialization. Engineers built narrow systems: one for translation, another for summarization, another for classification. Each required curated datasets and task-specific fine-tuning. This approach worked — but it was fragile. Unlike humans, who can understand new tasks from a single instruction, traditional AI systems required thousands of labeled examples. GPT-3 changed that equation. By scaling a single autoregressive model to 175 billion parameters, researchers demonstrated that size itself could unlock general-purpose adaptability. Instead of retraining for each task, GPT-3 adapts...