vscanf() — Read Formatted Data
Format
#include <stdarg.h>
#include <stdio.h>
int vscanf(const char *format, va_list arg_ptr);
Language Level
ANSI
Threadsafe
Yes
Locale Sensitive
The behavior of this function might be affected by the LC_CTYPE and LC_NUMERIC categories of the current locale. The behavior might also be affected by the LC_UNI_CTYPE category of the current locale if LOCALETYPE(*LOCALEUCS2) or LOCALETYPE(*LOCALEUTF) is specified on the compilation command. For more information, see Understanding CCSIDs and Locales.
Description
The vscanf()
function
reads data from stdin
into
locations specified by a variable number of arguments. The vscanf()
function
works just like the scanf()
function,
except that arg_ptr points to a list of
arguments whose number can vary from call to call in the program.
These arguments should be initialized by va_start for
each call. In contrast, the scanf()
function
can have a list of arguments, but the number of arguments in that
list is fixed when you compile the program.
Each argument must be a pointer to a variable with a type
that corresponds to a type specifier in format-string. The format has
the same form and function as the format string for the scanf()
function.
Return Value
The vscanf()
function
returns the number of fields that were successfully converted and
assigned. The return value does not include fields that were read
but not assigned. The return value is EOF for an attempt to read at
end-of-file if no conversion was performed. A return value of 0 means
that no fields were assigned.
Example
vscanf()
function
to read an integer, a floating-point value, a character, and a string
from stdin
and
then displays these values. #include <stdio.h>
#include <stdarg.h>
int vread(char *fmt, ...)
{
int rc;
va_list arg_ptr;
va_start(arg_ptr, fmt);
rc = vscanf(fmt, arg_ptr);
va_end(arg_ptr);
return(rc);
}
int main(void)
{
int i, rc;
float fp;
char c, s[81];
printf("Enter an integer, a real number, a character "
"and a string : \n");
rc = vread("%d %f %c %s", &i, &fp, &c, s);
if (rc != 4)
printf("Not all fields are assigned\n");
else
{
printf("integer = %d\n", i);
printf("real number = %f\n", fp);
printf("character = %c\n", c);
printf("string = %s\n",s);
}
}
/***************** If input is: 12 2.5 a yes, *******************
************** then output should be similar to: ****************
Enter an integer, a real number, a character and a string :
integer = 12
real number = 2.500000
character = a
string = yes
*/
Related Information
- fscanf() — Read Formatted Data
- fwprintf() — Format Data as Wide Characters and Write to a Stream
- fwscanf() — Read Data from Stream Using Wide Character
- scanf() — Read Data
- sscanf() — Read Data
- swprintf() — Format and Write Wide Characters to Buffer
- swscanf() — Read Wide Character Data
- vfscanf() — Read Formatted Data
- vfwscanf() — Read Formatted Wide Character Data
- vscanf() — Read Formatted Data
- vsscanf() — Read Formatted Data
- vswscanf() — Read Formatted Wide Character Data
- vwscanf() — Read Formatted Wide Character Data
- wprintf() — Format Data as Wide Characters and Print
- wscanf() — Read Data Using Wide-Character Format String
- <wchar.h>