标头scanf_s功能 [英] Header for scanf_s function

查看:996
本文介绍了标头scanf_s功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在回答这个问题,我编上的 Ideone 并得到这个错误

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

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

不是 stdio.h中 scanf_s

推荐答案

scanf_s 是微软特有的。标题是 stdio.h中,但不是在GCC。

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

用于从标准输入流中读取格式的数据。这些版本的scanf函数, scanf_s,_scanf_l,wscanf,_wscanf_l有安全增强功能

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

在哪里为 Ideone使用GCC ,因为这只是你有未定义的引用scanf_s

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

晴你可以发现这个功能在Windows中一样结果基于编译器
  Visual Studio 2008中的Microsoft .NET 2010

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

这里和在这里,你发现了大约scanf_s有趣的信息

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

根据MSDN的帮助:

According to the MSDN help:

该scanf_s函数从标准输入流的标准输入数据,并将数据写入由参数给出的位置。每个参数必须是一个指针,对应于格式的类型说明符的类型的变量。如果复制操作重叠的字符串之间发生的行为是不确定的。

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.

不同于scanf的,scanf_s需要键入c,C,S,S或[的所有输入参数中指定的缓冲区大小。缓冲区大小作为紧接在指针​​缓存器或可变以下内容的附加参数传递。例如,如果读出的字符串,该字符串的缓冲区大小传递如下:

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

这篇关于标头scanf_s功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆