vscanf() Function
The vscanf()
function in C stdio.h reads formatted input from the standard input and stores the parsed values into a variable argument list. It works similarly to scanf()
but uses a variable argument list, allowing it to process a flexible number of input arguments. This function is particularly useful when creating wrapper functions that handle variable numbers of arguments.
Syntax of vscanf()
int vscanf(const char *format, va_list arg);
Parameters
Parameter | Description |
---|---|
format | A C string containing the format specifiers, following the same rules as in scanf() . |
arg | A variable argument list (of type va_list ) that has been initialized using va_start . |
It is important to remember that vscanf()
internally retrieves each argument from the variable argument list as if using va_arg
, which modifies the state of the list. Therefore, ensure that the variable argument list is properly initialized with va_start
before the call and released with va_end
after the call.
Return Value
On success, vscanf()
returns the number of input items successfully matched and assigned. This number may be less than expected due to matching failures, reading errors, or reaching the end-of-file. If an input failure occurs before any conversion, EOF is returned. Additionally, in case of an encoding error while processing wide characters, errno
is set to EILSEQ
.
Examples for vscanf()
Example 1: Basic Usage of vscanf() via a Wrapper Function
This example demonstrates how to create a wrapper function that utilizes vscanf()
to read an integer and a floating-point number from standard input.
Program
#include <stdio.h>
#include <stdarg.h>
int myVScan(const char *format, ...) {
va_list args;
int result;
va_start(args, format);
result = vscanf(format, args);
va_end(args);
return result;
}
int main() {
int num;
float f;
printf("Enter an integer and a float: ");
myVScan("%d %f", &num, &f);
printf("You entered: %d and %f\n", num, f);
return 0;
}
Explanation:
- The function
myVScan()
is defined as a wrapper forvscanf()
, initializing ava_list
and callingvscanf()
with the given format and arguments. - In
main()
, two variables (num
andf
) are declared to store an integer and a float respectively. - The user is prompted to enter an integer and a float, which are read by
myVScan()
. - The entered values are then printed using
printf()
.
Program Output:
Enter an integer and a float: 42 3.14
You entered: 42 and 3.140000
Example 2: Reading a String Using vscanf()
This example illustrates how to read a string from the standard input using a wrapper function that calls vscanf()
.
Program
#include <stdio.h>
#include <stdarg.h>
int myVScanString(const char *format, ...) {
va_list args;
int result;
va_start(args, format);
result = vscanf(format, args);
va_end(args);
return result;
}
int main() {
char str[50];
printf("Enter a string: ");
myVScanString("%49s", str);
printf("You entered: %s\n", str);
return 0;
}
Explanation:
- A wrapper function
myVScanString()
is defined to encapsulate the call tovscanf()
. - In the
main()
function, a character arraystr
is declared to hold the input string. - The user is prompted to enter a string, which is then read into
str
using the wrapper function. - The input string is subsequently printed to confirm the correct operation of the function.
Program Output:
Enter a string: HelloWorld
You entered: HelloWorld
Example 3: Handling Input Error with vscanf()
This example demonstrates how to handle a situation where the input does not match the expected format, using the return value from vscanf()
to detect errors.
Program
#include <stdio.h>
#include <stdarg.h>
int myVScan(const char *format, ...) {
va_list args;
int result;
va_start(args, format);
result = vscanf(format, args);
va_end(args);
return result;
}
int main() {
int num;
printf("Enter an integer: ");
int ret = myVScan("%d", &num);
if(ret == 1) {
printf("Valid integer entered: %d\n", num);
} else {
printf("Input error or matching failure occurred.\n");
}
return 0;
}
Explanation:
- The wrapper function
myVScan()
is reused to read an integer from standard input. - The return value of
vscanf()
is stored inret
to check if the input was successfully matched. - If the return value equals 1, it indicates that the integer was read correctly; otherwise, an error message is displayed.
Program Output:
Enter an integer: abc
Input error or matching failure occurred.