Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
4 views

C Programming Command Line Argument

The document discusses the use of command-line arguments in C programming, specifically focusing on the main() function which can accept up to three string arguments. It provides several examples demonstrating how to handle command-line inputs, including calculating factorials and reading/writing student data from files. Each example includes the code and expected output when executed from the command line.

Uploaded by

ABHISHEK GOUTAM
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

C Programming Command Line Argument

The document discusses the use of command-line arguments in C programming, specifically focusing on the main() function which can accept up to three string arguments. It provides several examples demonstrating how to handle command-line inputs, including calculating factorials and reading/writing student data from files. Each example includes the code and expected output when executed from the command line.

Uploaded by

ABHISHEK GOUTAM
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

' $

PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 1

Command-line Arguments

& %
Lect 30 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 2

int main()

So far we have used the function main() without


any argument. But it can take string arguments
from the command line when we execute an
executable module e.g. ./a.out. The formal
parameters of main() may be zero to three.

& %
Lect 30 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 3

Example 1

#include <stdio.h>
int main(int aNum, char *aList[],
char *envL[])
{ // commLine1.c
int i;

printf("Arg. Num.: %d\n", aNum);


for(i=0;i<aNum;++i) printf("\t%s\n",aList[i]);
for(i=0; envL[i] != ’\0’; ++i)
printf("%s\n", envL[i]);
return 0;

& %
}
Lect 30 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 4

Running

$ ./a.out aaaaa bb ccccc dddd eeeee f gg


hhhhhhh
Arg. Num.: 9
a.out
aaaaa
bb
ccccc
dddd
eeeee
f
gg
hhhhhhh
SSH AGENT PID=2914

& %
HOSTNAME=goutam

Lect 30 Goutam Biswas


' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 5

Example 2

#include <stdio.h>
#include <stdlib.h>
int main(int aNum, char *aList[]) // commLine2.c
{
int i, n, fact=1;
if(aNum<2){ printf("No argument\n"); return 0; }
n = atoi(aList[1]);
for(i=1; i<=n; ++i) fact *= i;
printf("%d! = %d\n", n, fact);
return 0;
}
& %
Lect 30 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 6

Running

$ ./a.out 0
0! = 1
$ ./a.out 1
1! = 1
$ ./a.out 5
5! = 120

& %
Lect 30 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 7

Example 3

#include <stdio.h>
#include <stdlib.h>
#define MAXNO 100
#define ROLL 9
#define NAME 51
struct studData {
char rollNo[ROLL] ;
char name[NAME] ;
float cgpa ;
};
int main(int aNum, char *aList[]) // commLine3.c

& %
{
Lect 30 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 8

int noOfStdnt, i ;
struct studData data[MAXNO] ;
FILE *fpO, *fpI ;

if(aNum < 3) {
printf("Improper number of arguments\n");
return 0;
}
fpI = fopen(aList[1], "r");
fpO = fopen(aList[2], "w");
fscanf(fpI, "%d", &noOfStdnt);
for(i=0; i<noOfStdnt; ++i) {
fscanf(fpI, "%s", data[i].rollNo);
fscanf(fpI, " %[^0-9]", data[i].name);
& %
Lect 30 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 9

fscanf(fpI, "%f", &data[i].cgpa);


}

for(i=0; i<noOfStdnt; ++i) {


fprintf(fpO, "%s ", data[i].rollNo);
fprintf(fpO, " %s", data[i].name);
fprintf(fpO, " %5.2f", data[i].cgpa);
putc(’\n’, fpO);
}
fclose(fpI); fclose(fpO);
return 0;
}

& %
Lect 30 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 10

Running

$ a.out openDat outDat

& %
Lect 30 Goutam Biswas

You might also like