variable argument list
Page 1 of 1 • Share •
variable argument list
Write a function which accepts variable number of arguments and print all of them ..
If u done with this write a macro for the same...
If u done with this write a macro for the same...
Pulkit- Posts: 2
Join date: 2008-08-10
Re: variable argument list
Pulkit wrote:Write a function which accepts variable number of arguments and print all of them ..
If u done with this write a macro for the same...
Hiii Pullu...
int main(int argc, char argv[])
{
for(int i=1; i<argc; i++) printf("%s\n",argv[i]);
}
//Does It Suffice.. ?? Or You Are asking 4 something else..

Dee2306- Posts: 7
Join date: 2008-08-12
Age: 21
use CSTDARG
the header file cstdarg provides for such situation,,
here is sample code............
#include <cstdarg>
#include <iostream>
using namespace std;
double average ( int num, ... )
{
va_list arguments; // A place to store the list of arguments
double sum = 0;
va_start ( arguments, num ); // Initializing arguments to store all values after num
for ( int x = 0; x < num; x++ ) // Loop until all numbers are added
sum += va_arg ( arguments, double ); // Adds the next value in argument list to sum.
va_end ( arguments ); // Cleans up the list
return sum / num; // Returns some number (typecast prevents truncation)
}
int main()
{
cout<< average ( 3, 12.2, 22.3, 4.5 ) <<endl;
cout<< average ( 5, 3.3, 2.2, 1.1, 5.5, 3.3 ) <<endl;
}
more@http://www.cprogramming.com/tutorial/lesson17.html
here is sample code............
#include <cstdarg>
#include <iostream>
using namespace std;
double average ( int num, ... )
{
va_list arguments; // A place to store the list of arguments
double sum = 0;
va_start ( arguments, num ); // Initializing arguments to store all values after num
for ( int x = 0; x < num; x++ ) // Loop until all numbers are added
sum += va_arg ( arguments, double ); // Adds the next value in argument list to sum.
va_end ( arguments ); // Cleans up the list
return sum / num; // Returns some number (typecast prevents truncation)
}
int main()
{
cout<< average ( 3, 12.2, 22.3, 4.5 ) <<endl;
cout<< average ( 5, 3.3, 2.2, 1.1, 5.5, 3.3 ) <<endl;
}
more@http://www.cprogramming.com/tutorial/lesson17.html
prani- Posts: 15
Join date: 2008-08-12
Permissions of this forum:
You cannot reply to topics in this forum





