C - scanf_s() function

Introduction

scanf_s() or scanf() function reads data from stdin and converts them to one or more values according to the format specifiers in a format control string.

The prototype of the scanf_s() function is as follows:

int scanf_s(const char * restrict format, . . . );

scanf() function has a similar prototype.

The difference between scanf() and scanf_s() is

  • the latter requires two arguments for each input data controlled by c, s, and [ specifiers,
  • the former just requires one argument for these.

The format control string is basically a description of how scanf_s() should convert the incoming character stream to the values required.

Following the format control string, you can have one or more optional arguments.

For each input read with specifications c, s, or [ specifiers, the first corresponding argument is a pointer to the memory where the input is to be stored, and the second argument corresponding is the number of bytes pointed to by the previous argument.

The scanf_s() reads from stdin until it comes to the end of the format control string or until an error condition happens.