Open In App

vwprintf() function in C/C++

Last Updated : 14 Sep, 2018
Improve
Improve
Like Article
Like
Save
Share
Report

The vwprintf() function in C++ is used to write a formatted wide string to stdout. It prints formatted data from variable argument list to stdout. Internally, the function retrieves arguments from the list identified by arg as if va_arg was used on it, and thus the state of arg is likely altered by the call. The wide string format may contain format specifiers starting with % which are replaced by the values of variables that are passed as a list vlist.
It is defined in header file <cwchar.h>
Syntax :

int vwprintf( const wchar_t* format, va_list vlist )

Parameter : The function accepts four mandatory parameters which are described below:

  • format: specifies a pointer to a null terminated wide string that is written to stdout
  • arg: specifies a value identifying a variable arguments list initialized with va_start

Return value : The function returns two value as below:

  • On success, the total number of characters written is returned.
  • A negative number is returned if some error is occurred.

Below programs illustrate the above function:
Program 1 :




// C++ program to illustrate the
// vwprintf() function
// for some english letter
  
#include <bits/stdc++.h>
using namespace std;
  
// function to print formatted
// data from variable argument list to stdout
void write(const wchar_t* format, ...)
{
    // hold the variable argument
    va_list arg;
  
    // A function that invokes va_start
    // shall also invoke va_end before it returns.
    va_start(arg, format);
  
    vwprintf(format, arg);
    va_end(arg);
}
  
// Driver code
int main()
{
    setlocale(LC_ALL, "en_US.UTF-8");
  
    wchar_t buffer[5][10] = { L"First", L"Second", L"Third",
                              L"Fourth", L"Fifth" };
    int k = 0;
  
    // print letters by calling write function
    wprintf(L"Some English Letters\n");
    for (wchar_t i = L'A'; i <= L'E'; i++) {
        write(L"%ls : %lc\n", buffer[k], i);
        k++;
    }
  
    return 0;
}


Output:

Some English Letters
First : A
Second : B
Third : C
Fourth : D
Fifth : E




// C++ program to illustrate the
// vwprintf() function
// for some Latin letters
#include <bits/stdc++.h>
using namespace std;
  
// function to print formatted
// data from variable argument list to stdout
void write(const wchar_t* format, ...)
{
    // hold the variable argument
    va_list arg;
  
    // A function that invokes va_start
    // shall also invoke va_end before it returns.
    va_start(arg, format);
  
    vwprintf(format, arg);
    va_end(arg);
}
  
// Driver code
int main()
{
    setlocale(LC_ALL, "en_US.UTF-8");
  
    wchar_t buffer[5][10] = { L"First", L"Second", L"Third",
                              L"Fourth", L"Fifth" };
    int k = 0;
  
    // print letters by calling write function
    wprintf(L"Some Latin Letters\n");
    for (wchar_t i = L'\u0021'; i <= L'\u0025'; i++) {
        write(L"%ls : %lc\n", buffer[k], i);
        k++;
    }
  
    return 0;
}


Output:

Some Latin Letters
First : !
Second : "
Third : #
Fourth : $
Fifth : %


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads