0

What's the difference between scanf and scanf_s in C. I'm using visual studio 13 and it keeps on giving me errors when i try to use scanf.

nicael
  • 18,550
  • 13
  • 57
  • 90

1 Answers1

0

Both read formatted data from the standard input stream. The '_s' are Microsoft's 'secure' versions.

These versions of scanf, _scanf_l, wscanf, _wscanf_l have security enhancements, as described in Security Features in the CRT:

Many old CRT functions have newer, more secure versions. If a secure function exists, the older, less secure version is marked as deprecated and the new version has the _s ("secure") suffix.

In this context, "deprecated" just means that a function's use is not recommended; it does not indicate that the function is scheduled to be removed from the CRT.

The secure functions do not prevent or correct security errors; rather, they catch errors when they occur. They perform additional checks for error conditions, and in the case of an error, they invoke an error handler (see Parameter Validation).

For example, the strcpy function has no way of telling if the string that it is copying is too big for its destination buffer. However, its secure counterpart, strcpy_s, takes the size of the buffer as a parameter, so it can determine if a buffer overrun will occur. If you use strcpy_s to copy eleven characters into a ten-character buffer, that is an error on your part; strcpy_s cannot correct your mistake, but it can detect your error and inform you by invoking the invalid parameter handler.

Ref

Mitch Wheat
  • 295,962
  • 43
  • 465
  • 541