What is vwscanf in C?

What it does

The vwscanf function requires both the wchar.h and stdarg.h header files to work. It is used to read data from the stdin and store it in different elements of a variable argument list according to the specified format wide string.

Syntax

The syntax to declare the vwscanf function is as follows:

Parameters

The vwscanf_s function takes in 2 mandatory arguments:

  • format: The pointer to the format string which may include format specifiers like %ls.

  • arglist: A variable list of arguments of type va_list. This data type is defined in the stdarg.h header file.

An object of type va_list needs to be initialized by the va_start macro and needs to be released after use through the va_end macro

Return value

vwscanf can return one of the two things:

  • Upon successful execution, it returns the number of items filled in the list of variable arguments given in the parameters.

  • If there is a failure during the assignment of arguments or while interpreting an input, or EOF (End Of File) is returned.

Example

In the following example, we take a value as input followed by its unit and display it on stdout differently. For this example, we constructed a different function, get_store, which handles initializing and storage of the arglist. An example for the input could be “500 meters”:

NOTE: To use standard input for the following code, type your input into the text box below the code widget and click on run.

#include <stdio.h>
#include <stdarg.h>
#include <wchar.h>
// function to automatically initialize the list of arguments
void get_store ( const wchar_t * format, ... )
{
// the arglist holds list of all arguments fed to this function
va_list arglist;
// initializing the arglish using the vs_start macro
va_start (arglist, format);
// putting the arguments into the arglist using vwscanf
vwscanf (format, arglist);
// releasing the arglish variable using the va_end macro
va_end (arglist);
}
int main ()
{
// variables to store data into
int magnitude;
wchar_t unit[40];
// sending format string and arguments to the get_store function
// this format string requires an input with first an integer folllowed by a string
get_store (L" %d %ls ", &magnitude, unit);
//printing out the variables to check data in it
wprintf (L"The entered magnitude is %d and the units are %ls\n", magnitude, unit);
return 0;
}

Enter the input below

Copyright ©2024 Educative, Inc. All rights reserved