Command line arguments in C

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:

  1. argc (Argument Count): This integer represents the number of command-line arguments, including the program name.
  2. 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:

  1. Printing Argument Count: argc gives the count of arguments, which we print.
  2. Printing Each Argument: We loop through argv, printing each argument. argv[0] is the program's name, and argv[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

  1. Data Type: Command-line arguments are passed as strings, so you may need to convert them (e.g., atoi, atof).
  2. Error Handling: Always check if the right number of arguments is provided to avoid errors.
  3. 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;
}

Comments

Popular posts from this blog

Programming in C GXEST204 - KTU 2024 scheme syllabus notes pdf ------- Dr Binu V P

Structure of a C Program

Single and Multi Dimensional Arrays