What is scanf_s in C?

Defined in the stdio.h header file, scanf_s reads data from stdin, formats it according to the format string, and stores the result into the destinations specified by the additional arguments. What makes scanf_s different from the normal scanf is that it performs extra run-time checks on the arguments before they are executed.

Syntax

Following is the declaration syntax for the scanf_s function:

Parameters

The scanf_s function takes in 1 mandatory argument and then a list of additional arguments:

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

  • ...: These are the optional arguments that correspond to the specifiers used in the format string. These arguments are pointers to storage spaces (variables) and must have the same type as their corresponding specifier. This means e.g. if there’s a %s specifier in the format string, there should be a character pointer in the additional arguments.

Return value

scanf_s can return one of the two things:

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

  • If there is a failure during the assignment of arguments or while interpreting an input or if any run-time constraints are violated, EOF (End Of File) is returned.

Example

Following is an example of how the scanf_s function is used to take input from stdin and store the data in it to several other variables.

scanf_s reads from the input string sequentially. Hence, the list of arguments must be given in the order they are specified in the format string.

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

#define __STDC_WANT_LIB_EXT1__ 1
#include <stdio.h>
int main ()
{
// variables in which to store data
char name [15];
char unit [15];
int amount;
// taking input and storing in variables
// a sampleinput string would be:
// "Faraz owns 500 acres of land"
#ifdef __STDC_LIB_EXT1__ // only use swscanf_s if __STDC_LIB_EXT1__ is already defined
scanf_s ("%s %*s %d %s",name,&amount,unit);
#endif
// use normal scanf_s
scanf ("%s %*s %d %s",name,&amount,unit);
// print out new string using the extracted values
printf ("% d %s of land is owned by %s\n",amount, unit, name);
return 0;
}

Enter the input below

All bounds-checked functions (with “_s” suffix in their names) including the scanf_s function are only guaranteed to work if __STDC_LIB_EXT1__ is pre-defined by the implementation and if the user defines __STDC_WANT_LIB_EXT1__ to int 1 before including stdio.h header file.

In this example, we require an input with the specific format matching our format string, i.e., with the first word being a string, the second can be anything since it is ignored, the third an int, and another string after that. An example input would be “Faraz owns 500 acres of land”. We then use the scanf_s function to read from this input and store parts of the string corresponding to the amount of land, units of measurement, and the owner’s name. We then used this to print a new string. Note how we did not need the word “owns” in the input string. So even though we denoted it by %*s in the format string, it is ignored since we did not give any string pointer argument.

Free Resources

Copyright ©2024 Educative, Inc. All rights reserved