vwprintf() function in C/C++

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 :

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Output:

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

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Output:

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


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.