vprintf() – I/O Function

vprintf() – I/O Function

How is “vprintf()” used in C++?

Explanation

vprintf() is an I/O function that writes the contents of the format string to the stdout with a pointer to a list of arguments replacing the argument list. The total number of characters are returned by this function, but on failure a negative number is returned.

Syntax:

int vprintf ( const char * format, va_list arg );

The following table lists the type specifier used with vprintf()

Type Description
c Character
d or i Signed decimal integer
e Scientific notation using e character
E Scientific notation using E character
f Decimal floating point
g Use the shorter of %e or %f
G Use the shorter of %E or %f
o Signed octal
s String of characters
u Unsigned decimal integer
x Unsigned hexadecimal integer
X Unsigned hexadecimal integer(capital)
p Pointer address
% A % followed by another % character will write % to the stream.

Example :

#include
#include
void Form (char * format, …)
{
va_list args;
va_start (args, format);
vprintf (format, args);
va_end (args);
}
int main ()
{
Form (“Function with %d variable argument.n”,1);
Form (“Function with %d variable %s.n”,2,”arguments”);
return 0;
}

Result :

Function with 1 variable argument.
Function with 2 variable arguments.

In the above example vprintf() is used to call a function with different number of arguments.





Leave a Reply

Your email address will not be published. Required fields are marked *