Command-line arguments are a way to pass information or inputs to a C program when you run it from the command line or terminal. These arguments provide additional flexibility and let users control the program's behavior without changing the code.
In C, command-line arguments are handled through the main
function, which takes two parameters:
int main(int argc, char *argv[])
Here’s what these parameters mean:
argc
(Argument Count): This integer represents the number of command-line arguments, including the program name.argv
(Argument Vector): This array of strings (char *argv[]
) holds each command-line argument as a string.argv[0]
is always the program's name, and the remaining elements are the actual arguments passed by the user.
Example: Printing Command-Line Arguments
Let’s start with a basic example where we simply print all command-line arguments.
#include <stdio.h>
int main(int argc, char *argv[]) {
printf("Number of arguments: %d\n", argc);
for (int i = 0; i < argc; i++) {
printf("Argument %d: %s\n", i, argv[i]);
}
return 0;
}
Explanation:
- Printing Argument Count:
argc
gives the count of arguments, which we print. - Printing Each Argument: We loop through
argv
, printing each argument.argv[0]
is the program's name, andargv[1]
,argv[2]
, etc., are the arguments provided by the user.
Running the Program
If we compile this program as example
, we could run it with arguments like this:
./example hello world 123
Expected Output:
Number of arguments: 4
Argument 0: ./example
Argument 1: hello
Argument 2: world
Argument 3: 123
Important Points
- Data Type: Command-line arguments are passed as strings, so you may need to convert them (e.g.,
atoi
,atof
). - Error Handling: Always check if the right number of arguments is provided to avoid errors.
- Applications: Command-line arguments are useful for tasks like specifying filenames, setting configuration options, or providing user inputs.
Command-line arguments give users control and make programs more versatile.
Sample Program to add two numbers passed as command line arguments
#include <stdio.h>
#include <stdlib.h>
int main(int argc,char *argv[])
{
if(argc!=3)
{printf("Invalid number of arguments...\n");
exit(0);
}
else
{
printf("Sum=%d",atoi(argv[1])+atoi(argv[2]));
}
}
Note:if the default executable is used then use ./a.out 2 3 for adding two numbers
The function atoi() will convert string to integer
Program to display contents of several files passed as arguments.
#include <stdio.h>
#include <stdlib.h>
int main(int argc,char *argv[])
{
int i=1
int c;
int nargs=0;
FILE *fp;
If(argc==1)
{
printf(“No input file to display);
exit(1);
}
nargs=argc-1;
while(nargs>0)
{
nargs--;
printf(“Displaying the content of file %s”,argv[i]);
p=fopen(argv[i],”r”);
if(fp==NULL)
{
printf(“cannot open file %s”,argv[i]);
continue;
}
c=getc(fp);
while(c!=EOF)
{
putchar(c);
c=getc(fp);
}
fclose(fp);
printf(“\n\n************end of file %s\n\n”,argv[i]);
i++;
}
return 0;
}
Simulation of copy command where the two file names are passed as arguments
#include <stdio.h>
#include <stdlib.h>
int main(int argc,char *argv[])
{
FILE *in,*out;
int c;
if (argc !=3)
{
printf(“Invalid number of arguments”);
exit(0);
}
in=fopen(argv[1],”r”);
if(in==NULL)
{
printf(“couldn’t open the source file..”);
return 0;
}
out=fopen(argv[2],”w”);
if(out==NULL)
{
printf(“couldn’t open the destination file..”);
return 0;
}
while(!feof(in))
{
c=fgetc(in);
if(!feof(in))
fputc(c,out);
}
fclose(in);
fclose(out);
return 0;
}
Keep sharing such a useful information..!! Great blog. Computer programming courses
ReplyDeleteI have read all your articles. I wonder why you can write them. They are easy to understand and accessible. They help me much too. I'm thankful if you can upload more posts later. Thanks for your sharing.
ReplyDeleteHướng dẫn đăng ký B30 VinaPhone, Đăng ký Mimax sinh viên VinaPhone, Đăng ký Mimax50 VinaPhone, Đăng ký 3G VinaPhone 1 ngày chỉ 5K,7K,10K
recent sales notification popup 2018
ReplyDeletepop sales
Say, you got a nice post.Much thanks again. Really Cool
I found best article.thank you for sharing useful info.
ReplyDeleteDiscount master app
Discount master app
For space outside the milk tea shop closer to nature, outdoor space should use speakers with greater capacity, products such as garden speakers, imitation stone speakers, are products with designs The most natural to create interesting feeling when enjoying. Because it must work in the weather conditions heat so the shell is made very firmly with high durability, the quality of the speaker is imported genuine. Good sound, making good clear sound even when used in wide space
ReplyDeleteGái gọi Mỹ Đình - Đình Thôn, gái gọi Nguyễn Khánh Toàn, gái gọi hà nội, gái gọi Phố Cổ - Quán Sứ