Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Header for scanf_s function

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?

like image 881
haccks Avatar asked Mar 22 '23 09:03

haccks


1 Answers

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
like image 196
Gangadhar Avatar answered Apr 01 '23 07:04

Gangadhar