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

  1. Keep sharing such a useful information..!! Great blog. Computer programming courses

    ReplyDelete
  2. I 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.
    Hướ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

    ReplyDelete
  3. recent sales notification popup 2018
    pop sales
    Say, you got a nice post.Much thanks again. Really Cool

    ReplyDelete
  4. 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
    Gá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ứ

    ReplyDelete

Post a Comment

Popular posts from this blog

KTU Mandatory C programs for Laboratory and Solutions

LIST OF LAB EXPERIMENTS 1. Familiarization of Hardware Components of a Computer 2. Familiarization of Linux environment – Programming in C with Linux 3. Familiarization of console I/O and operators in C     i) Display “Hello World”     ii) Read two numbers, add them and display their sum     iii) Read the radius of a circle, calculate its area and display it 4. Evaluate the arithmetic expression ((a -b / c * d + e) * (f +g))   and display its solution. Read the values of the variables from the user through console 5. Read 3 integer values, find the largest among them. 6. Read a Natural Number and check whether the number is prime or not 7. Read a Natural Number and check whether the number is Armstrong or not 8. Read n integers, store them in an array and find their sum and average 9. Read n integers, store them in an array and search for an element in the    array using an algorithm for Linear Search 10.Read n integers, store them in an array and sort the elements in t

PROGRAMMING IN C KTU EST 102 THEORY AND LAB NOTES

PROGRAMMING IN C  KTU  EST 102  THEORY AND LAB   COMMON FOR ALL BRANCHES About Me Syllabus Theory Syllabus Lab Model Question Paper University Question Papers and evaluation scheme Introduction( Lab) Introduction to C programming Linux History and GNU How to create a bootable ubuntu USB stick Installing  Linux Install Linux within  Windows Virtual Box and WSL Linux Basic Features and Architecture Basic Linux Commands Beginning C Programming Compiling C programs using gcc in Linux Debugging C program using gdb Module 1: Basics of computer hardware and software          Module-1 Reading Material Basics of Computer Architecture Hardware and Software System Software and Application Software  Programming Languages ( High level, Low level and Machine Language) and Translators ( Compiler, Interpreter, Assembler) Algorithm, Flowcharts and Pseudo code Program Development Structured Programming Basics of hardware ( video) Know about Motherboard(video) Know Universal Serial Bus ( USB) - video Lea

Input Output-printf() and scanf()

printf() and scanf() function in C   printf() and scanf() functions are inbuilt library functions in C programming language which are available in C library by default.These functions are declared and related macros are defined in “stdio.h” which is a header file in C language. We have to include “ stdio.h ” file as shown in below to make use of these printf() and scanf() library functions in C program. #include <stdio.h>   printf() function in C   In C programming language, printf() function is used to print the “character, string, float, integer, octal and hexadecimal values” onto the output screen. Here’s a quick summary of the available printf format specifiers:   %c           character %d           decimal (integer) number (base 10) %ld         Long integer %e           exponential floating-point number %f           floating-point number %lf          double number %i            integer (base 10) %o         octal number (base 8) %s         a string of characters %u