P14 Program Arguments
-
Upload
abhishek-ek -
Category
Documents
-
view
220 -
download
0
Transcript of P14 Program Arguments
-
8/3/2019 P14 Program Arguments
1/12
-
8/3/2019 P14 Program Arguments
2/12
PROGRAM ARGUMENTSPROGRAM ARGUMENTS
Command line argumentsArguments that we pass on to main() at the
command prompt
Variable number of argumentsHow to write functions that accepts the variable
number of arguments
-
8/3/2019 P14 Program Arguments
3/12
Command Line ArgumentsCommand Line Arguments
Argc and argv - arguments handled by
main().
Argv-char array of pointers to strings Argc-number of strings to which argv
points,it is an integer
General form of main() with command linearguments.
main ( int argc, char *argv[ ] )
-
8/3/2019 P14 Program Arguments
4/12
Command line argumentsCommand line arguments--ExampleExample
Main(int argc,char *argv[])
{
If(argc!=2)
Printf(invalid input format);Else
Printf(Length of the string is:%d,strlen(argv[1]));
}
>./a.out command /*argv[0]=./a.out argv[1]=command*/
Length of the string is:7
>./a.out
Invalid input format
-
8/3/2019 P14 Program Arguments
5/12
Variable number of argumentsVariable number of arguments
Some functions in the standard library acceptsvariable number of arguments.
Printf() and scanf() are the examples.
Let us discuss variable number of argumentswith an example.
double average(double v1,double v2 )Function is used to compute the average
Denotes that the function has a variable number ofarguments
Above statement demonstrates the prototype of function withvariable number of arguments.
-
8/3/2019 P14 Program Arguments
6/12
Variable number of argumentsVariable number of arguments
Must have atleast one fixed argument,inour example it is 2.
Now, how to reference the argumentswhen writing the function.
Stdarg.h header provides three macros toaccess the arguments.
Va_startVa_arg
Va_end
-
8/3/2019 P14 Program Arguments
7/12
Va_start() macroVa_start() macro
General form:
void va_start(va_list parg, last_fixed_arg);
Variable argument start accepts twoarguments.Pointer parg of type va_list
Name of the last fixed parameter
Va_list is defined in stdarg.h and it is usedto store information required by theroutines
-
8/3/2019 P14 Program Arguments
8/12
Va_start() macroVa_start() macro
Va_start macro for an average function
can be like this.
double average(double v1, double v2, ...){
va_list parg; /* Pointer for variable argument list */
/* More code to go here... */
va_start( parg, v2);
/* More code to go her. . . */
}
-
8/3/2019 P14 Program Arguments
9/12
ExampleExample
double average( double v1, double v2,...)
{
va_list parg; /* Pointer for variable argument list */
double sum = v1+v2; /* Accumulate sum of the arguments */
double value = 0; /* Argument value */
int count = 2; /* Count of number of arguments */
va_start(parg,v2); /* Initialize argument pointer */
while((value = va_arg(parg, double)) != 0.0)
{
sum += value;
count++;
}
va_end(parg); /* End variable argument process */
return sum/count;
}
-
8/3/2019 P14 Program Arguments
10/12
ExampleExample
#include
#include
double average(double v1 , double v2,...); /* Function prototype */
int main(void)
{
double Val1 = 10.5, Val2 = 2.5;int num1 = 6, num2 = 5;
long num3 = 12, num4 = 20;
printf("\n Average = %lf", average(Val1, 3.5, Val2, 4.5, 0.0));
printf("\n Average = %lf", average(1.0, 2.0, 0.0));
printf("\n Average = %lf\n", average( (double)num2, Val2,(double)num1,
(double)num4,(double)num3, 0.0));return 0;
}
-
8/3/2019 P14 Program Arguments
11/12
I request Electronics and communication
ENGINEERING students to visit my blogfor
more
abhishek1ek.blogspot.com
awhengineering.blogspot.com
-
8/3/2019 P14 Program Arguments
12/12