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?

3
  • 2
    Seems to be in this code here
    – smac89
    Sep 16, 2013 at 20:32
  • 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.
    – WhozCraig
    Sep 16, 2013 at 20:35
  • 1
    Isn't scanf_s a non-standard Microsoft-ism ?
    – Paul R
    Sep 16, 2013 at 20:35

1 Answer 1

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
3
  • 9
    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. Sep 17, 2013 at 6:10
  • 1
    char c[4]; scanf_s("%4c", &c, 4); // not null terminated ===> shouldn't &c -> c ??
    – kwagjj
    Jun 15, 2015 at 12:08
  • As of 2024, the 'Pelles C Compiler' appears to support __STDC_LIB_EXT1__ 'out of the box' without any special defines.
    – chikega
    Apr 7 at 15:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.