If I didn't have access to the internet, but knew that I wanted to use the scanf_s() function to take input from the keyboard (stdin), how would I know where to declare the buffer?
At the moment, when I step into the scanf_s() function in Visual Studio, I see this:
#if __STDC_WANT_SECURE_LIB__
_Check_return_opt_
_CRT_STDIO_INLINE int __CRTDECL scanf_s(
_In_z_ _Scanf_s_format_string_ char const* const _Format,
...)
#if defined _NO_CRT_STDIO_INLINE // SCANF
;
So, I can see that scanf_s() takes 1 defined argument (char const * _Format) which, in this case, could be some placeholder such as be "%s", but nothing for the second argument - the memory address of the buffer to store the result of the scanning.
scanf_stakes a variable list of arguments. The first argument is mandatory, a string with the conversion specifications, the following arguments depend on what conversion specifications are present in the format string.For each conversion specification
%s,%cand%[,scanf_sexpects 2 arguments for the conversion: a pointer tochar(char *) and a number passed with typersize_tthat specifies the size of the array pointed to by thechar *argument.Here is an example:
Note however that support for
scanf_sis far from universal. Microsoft platforms usually support it, but with varying semantics, while linux and Mac C libraries usually don't. Read this question to understand Why didn't gcc implement _s functions?