Header for scanf_s function
Asked Answered
M

1

6

While answering this question I compiled the code on Ideone and got this error

implicit declaration of function ‘scanf_s’ [-Wimplicit-function-declaration]

Isn't stdio.h is the header for scanf_s?

Miscall answered 16/9, 2013 at 20:25 Comment(3)
Seems to be in this code hereGlasshouse
The platform you're compiling on requires support for it before you can use it (obviously). It stands to reason what you using does not have said-support.Pus
Isn't scanf_s a non-standard Microsoft-ism ?Siglos
D
10

scanf_s is Microsoft-specific. Header is stdio.h but not in GCC.

Used to Read formatted data from the standard input stream. These versions of scanf,scanf_s, _scanf_l, wscanf, _wscanf_l have security enhancements

Where as Ideone uses GCC because of this only you got undefined reference to scanf_s

Mostly You can found this function in windows based compilers like
Visual Studio 2008 and Microsoft .NET 2010

Here and Here You found Interesting info about scanf_s

int scanf_s(
   const char *format [,
   argument]... 
);

According to the MSDN help:

The scanf_s function reads data from the standard input stream stdin and writes the data into the location given by argument. Each argument must be a pointer to a variable of a type that corresponds to a type specifier in format. If copying takes place between strings that overlap, the behavior is undefined.

Unlike scanf, scanf_s requires the buffer size to be specified for all input parameters of type c, C, s, S, or [. The buffer size is passed as an additional parameter immediately following the pointer to the buffer or variable. For example, if reading a string, the buffer size for that string is passed as follows:

char s[10];

scanf_s("%9s", s, 10);

The buffer size includes the terminating null. A width specification field may be used to ensure that the token read in will fit into the buffer. If no width specification field is used, and the token read is too big to fit in the buffer, nothing will be written to that buffer.

In the case of characters, one may read a single character as follows:

char c;

scanf_s("%c", &c, 1);  

When reading multiple characters for non-null terminated strings, integers are used as the width specification and the buffer size.

char c[4];

scanf_s("%4c", &c, 4); // not null terminated
Ditchwater answered 16/9, 2013 at 20:36 Comment(3)
Technically, it is an optional feature of C11, defined in Annex K, and enabled by defining __STDC_WANT_LIBC_EXT1__ (which is only available if the implementation defines __STDC_LIBC_EXT1__). In practice, you're right that Microsoft is the only platform where the functions are available (and probably without needing the various defines, too). See Do you use the TR 24731 'safe' functions? for more information.Calypso
char c[4]; scanf_s("%4c", &c, 4); // not null terminated ===> shouldn't &c -> c ??Mockery
As of 2024, the 'Pelles C Compiler' appears to support __STDC_LIB_EXT1__ 'out of the box' without any special defines.Whipperin

© 2022 - 2024 — McMap. All rights reserved.