Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

c

What is vscanf in C?

Faraz Karim

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

The vscanf function requires both the stdio.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.

Syntax

The following is the syntax to declare the vscanf function:

Parameters

The vscanf_s function takes in 2 mandatory arguments:

  • format: The pointer to the format string that may include format specifiers like %s.

  • 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

vscanf 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

The following is an example where 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, that handles initializing and storage of the arglist. An example of the required input would 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 run.

#include <stdio.h>
#include <stdarg.h>
// function to automatically initialize the list of arguments
void get_store ( const char * 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 vscanf
vscanf (format, arglist);
// releasing the arglish variable using the va_end macro
va_end (arglist);
}
int main ()
{
// variables to store data into
int magnitude;
char 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 (" %d %s ", &magnitude, unit);
//printing out the variables to check data in it
printf ("magnitude: %d\nunit: %s\n", magnitude, unit);
return 0;
}

Enter the input below

RELATED TAGS

c

CONTRIBUTOR

Faraz Karim
Copyright ©2022 Educative, Inc. All rights reserved

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring