vswprintf() function in C/C++

This vswprintf() function writes the wide string to the wide string buffer. A maximum of (len-1) wide characters are written to buffer which is followed by a null wide character.

Syntax:

int vswprintf( wchar_t* ws, size_t len, const wchar_t* format, va_list arg )

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

Note: All format specifiers have the same meaning as in printf therefore, %lc shall be used to write a wide character ( and not %c ), as well as %ls shall be used for wide strings ( and not %s ).



Return value: The function returns two value as below:

Below programs illustrate the above function:
Program 1 :

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to illustrate the
// vswprintf() function
#include <bits/stdc++.h>
using namespace std;
   
// function to check the number
// of wide characters written
void find ( wchar_t* ws, size_t len, 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 );
   
    vswprintf ( ws, len, format, arg );
   
    va_end ( arg );
}
   
// Driver code
int main ()
{
    // buffer with size 60 
    wchar_t ws[60];
      
    // initializing the string as latin characters
    wchar_t str[] = L"\u0025 \u0026 \u0027 \u0028 \u0029";
      
    // print the letters
    find(ws, 60, L"Some Latin letters : %ls\n", str);
    wprintf(L" %ls ", ws);
   
    return 0;
}
chevron_right

Output:
Some Latin letters : % & ' ( )

Program 2 :

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to illustrate the
// vswprintf() function
// When the size of the buffer is smaller
// than the total length of the string written 
#include <bits/stdc++.h>
using namespace std;
  
// function to check the number
// of wide characters written
void find ( wchar_t* ws, size_t len, 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 );
  
    vswprintf ( ws, len, format, arg );
  
    va_end ( arg );
}
  
// Driver code
int main ()
{
    // initializing the string as english characters
    wchar_t str[] = L"Geek for geeks";
  
    // buffer with size 20 
    wchar_t ws[20];
      
        find(ws, 20, L"GFG is : %ls\n", str);
    wprintf(L"%ls", ws);
  
    return 0;
}
chevron_right

Output:
GFG is : Geek for g

Note: If the resulting string would be longer than n-1 wide characters, the remaining characters are discarded and not stored.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.





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 :
C
Practice Tags :