+ 4
Âż What's the difference between scanf & scanf_s ?
Visual Studio gave me an error for using scanf
2 Answers
+ 10
It is a function that belongs specifically to the Microsoft compiler.
scanf originally just reads whatever console input you type and assign it to a type of variable.
If you have an array called first_name[5] and you use scanf for "Alex", there is no problem. If you have the same array and assign "Alexander", you can see it exceeds the 5 slots that the array contains, so C will still write it on memory that doesn't belong to the array and it might or might not crash the program, depending if something tries to access and write on that memory slot that doesn't belongs to first_name. This is where scanf_s comes in.
scanf_s has an argument(parameter) where you can specify the buffer size and actually control the limit of the input so you don't crash the whole building.
Source: https://stackoverflow.com/a/21439969/9134576
+ 7
I agree with Zlytherin's answer, however the _s functions do not specifically belong to the Microsoft compiler. They are part of the C11 standard. "Many traditional functions in the C standard library copy strings to arrays that are provided by the programmer as pointer arguments. There is no way for these functions to test whether the given destination array is large enough to accommodate the result. The programmer alone is responsible for ensuring that no data is written past the end of the array, where it could modify adjacent objects in memory. This is a significant threat to the reliability and security of a program, and can cause it to crash". C functions with the suffix _s (for "secure") "take an additional argument which specifies the size of the destination array. The secure functions use this information to ensure that the results they produce do not exceed the array's bounds" (source: "C in a nutshell", Prinz/Crawford, O'Reilly, 2nd edition, ISBN 978-1-491-90475-6, p. 287).