Programming in C—Command Line Arguments.

Hello Everyone

images.jpeg

Image Source

I hope you all are fine and safe inside your homes. This is a weird time ongoing in the whole world and I hope it will get over soon. As during this time, everyone is locked into their homes. I want to share the C programming language with you. I hope you guys like it, so let's happen today's topic.

Command Line Arguments

It is attainable to pass some values from the program line to your C programs after its execution. These values are known as Command line arguments and many times they're necessary for your program particularly once you wish to control your program from outside rather than arduous committal to writing those values within the code. The program line arguments area unit handled victimization main() operate arguments wherever argcrefers to the quantity of arguments passed, and argv[] could be a pointer array that points to every argument passed to the program. Following could be a simple example that checks if there's any argument equipped from the command line and take action consequently.
Program:

#include <stdio.h>
int main( int argc, char *argv[] ) {
 if( argc == 2 ) {
 printf("One argument passed. Argument is %s\n", argv[1]);
 }
 else if( argc > 2 ) {
 printf("More than one arguments passed.\n");
 }
 else {
 printf("No argument Passed.\n");
 }
}

When Above code is compiled and executed with single argument, output is:

[root@vinu ~]# ./a.out one
One argument passed. Argument is one

When Above code is executed with two arguments, output is:

[root@vinu ~]# ./a.out one two
More than one arguments passed.

When above code is executed without passing any arguments, the output is:

[root@vinu ~]# ./a.out
No argument Passed.

It ought to be noted that argv[0] holds the name of the program itself and argv[1] could be a pointer to the primary program line argument equipped, and *argv[n]is the last argument. If no arguments area unit equipped, argc are one, and if you pass one argument then argc is ready at two. You pass all the program line arguments separated by an area, but if argument itself features a area then you'll pass such arguments by swing them inside quotation mark " " or single quotes ' '.
Program to copy data from one file to another using Command Line Argument:

#include<stdio.h>
#include<string.h>
int main(int argc,char*argv[])
{ 
FILE *sptr,*dptr;
char ch;
if(argc!=3)
{
 printf("Command Line Error. Need Two argument");
 return;
}
sptr=fopen(argv[1],"r");
dptr=fopen(argv[2],"w");
if(sptr==NULL || dptr==NULL)
{
   printf("Error in opening file!");
   return;
}
while((ch=fgetc(sptr))!=EOF)
{
 fputc(ch,dptr);
}
printf("File Copied Successfully.");
fclose(sptr);
fclose(dptr);
}

Output:

[root@vinu ~]# vi sample.txt 
[root@vinu ~]# vi filecopy.c 
[root@vinu ~]# ./a.out sample.txt samplecopy.txt 
File Copied Successfully.[root@vinu ~]#

After the execution of above program, content of sample.txt is copied to samplecopy.txt file.

Logo.png


Gif by @doze

Logo.png

Thank you.

I hope you guys liked my post.

Keep Supporting.

STAY TUNED FOR NEXT POST

UPVOTECOMMENTRESTEEM
IF YOULIKEDMY POST

Logo.png

Created by @zord189

Stay Home, Stay Safe

@peerzadazeeshan

PEACE✌️✌️



0
0
0.000
0 comments