Skip to main content

Command-line Arguments in C


The parameters passed to the program when the program is invoked are known as command line arguments. These parameters are the additional information like filename or other kind of input to the program. By passing command line arguments there is no need for the user to provide the input while executing the program.
These command line arguments can be processed by using the arguments available in the main function. The main allows two parameters namely: argc and argv. The argc represents the argument counter which contains the number of arguments passed in the command line. The argv is a character pointer array which points to the arguments passed in the command line.
To know the number of command line arguments, we can use the argc parameter of the main function and to access the individual arguments, we can use the argv array. The first element in the argv array is always the program name. So the first argument can be accessed by using argv[1] and so on. The prototype of main function will be as shown below:
int main(int argc,char *argv[])
This declaration states that
main returns an integer value ( used to determine if the program terminates successfully)
argc is the number of command line argument including the command itself.
i.e; argc must be at least 1.
argv is an array of command-line arguments.argv[0] contains the program’s name.
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

Files in C

A file is a repository of data that is stored in a permanent storage media, mainly in secondary memory. In order to use the files we should learn how to read information from a file and how to write information into a file. A very important concept in C is the stream.The stream is a common logical interface to the various devices( files).A stream is linked to a file while using an open operation. A stream is disassociated from a file while using a close operation. The current location, also referred to as the current position, is the location in a file where the next fie access will occur.There are two types of streams text and binary. The following are some difference between text and binary files ·Text file is human readable because everything is stored in terms of text. In binary file everything is written in terms of 0 and 1, therefore binary file is not human readable. ·A newline(\n) character is converted into the carriage return-linefeed combination before being written to the d…

KTU C programming question paper and evaluation scheme

APJ ABDUL KALAM TECHNOLOGICAL UNIVERSITY SECOND SEMESTER B.TECH DEGREE EXAMINATION, MAY 2017 CS 100 COMPUTER PROGRAMMING (CS, IT) SCHEME OF EVALUATION
PART A 1 An identifier is a sequence of characters invented by the programmer or to identify or name a specific object. The sequence of characters may be letters, digits, and special character ‘_’known as an underscore Rules: i)Identifiers should start with alphabets. ii)Identifiers are case sensitive iii)A numeric digit should not be the first character iv)Identifier name should not be a keyword v)Identifier may be of any reasonable length 1mark






2mark 2 Associativity defines the direction, left to right or right to left in which operator act upon its operands Unary operators have associativity is from right to left. For examplein the expression &--x, pre decrement works first and then address of operator works Direction + example<

Strings in C

Strings in C Strings are defined as an array of characters. The difference between a character array and a string is the string is terminated with a special character ‘\0’. Declaration of strings: Declaring a string is as simple as declaring a one dimensional array. Below is the basic syntax for declaring a string. charstr_name[size]; In the above syntax str_name is any name given to the string variable and size is used define the length of the string, i.e the number of characters strings will store. Please keep in mind that there is an extra terminating character which is the Null character (‘\0’) used to indicate termination of string which differs strings from normal character arrays. Initializing a String: A string can be initialized in different ways. We will explain this with the help of an example. Below is an example to declare a string with name as str and initialize it with “cek”. 1. charstr[] = "cek"; 2. charstr[50] = "cek"; 3. charstr[] = {'c','…