Every program must have a function main( ). Till now, we know that main( ) function takes no arguments. But the empty parenthesis in the main( ) may contain special arguments that allow parameters to be passed to the main( ) from operating system.
Two arguments are passed to the main( ) function:Each string in this array will represent a parameter that is passed to main( ). Execution of the program is normally initiated by specifying the name of program at the operating system level. The program name is interpreted as an operating system command. Hence the Line in which it appears is referred to as command Line.
In order to pass one or more than one parameters to the program when the program execution is initiated, the parameters must follow the program name on command line.
WAP for copy file using command line arguments#include<stdio.h> #include<conio.h> main(int argc, char *argv[]) { FILE *source,*dest; int c; clrscr(); if(argc!=3) { printf("\nWrong no of arguments "); exit(1); } if((source=fopen(argv[1],"r"))==NULL) { printf("\nCan't open file "); exit(1); } if((dest=fopen(argv[2],"w"))==NULL) { printf("\nCan't open file "); exit(1); } while((c=fgetc(source))!=EOF) fputc(c,dest); fclose(source); fclose(dest); printf("\ndone"); getch(); }WAP for command line arguments
#include<stdio.h> #include<conio.h> main(int arg, char *ar[]) { int n,i; clrscr(); for(i=0;i<arg;i++) printf("argv[%d] = %s\n",i,ar[i]); getch(); }