简体   繁体   English

使用 scanf_s 读取字符

[英]Reading a character with scanf_s

I was just messing around with C and ran into this small problem.我只是在摆弄 C 并遇到了这个小问题。 As you can see from my output I getting '╠' this character.从我的输出中可以看出,我得到了 '╠' 这个字符。

#include <stdio.h>

int main(void)
{
    char c;

    printf("Do you want to be X's or O's?\n");
    scanf_s("%c", &c);
    printf("You chose %c\n", c);

}

See program output查看程序输出

You are misusing scanf_s() .您正在滥用scanf_s() Microsoft compilers may warn you to use their secure extensions (aka c11 annex k). Microsoft 编译器可能会警告您使用他们的安全扩展(又名 c11 附件 k)。 But, be careful if you do so.但是,如果您这样做,请小心。 scanf_s() is not a direct replacement for scanf() . scanf_s()不是scanf()的直接替代品。

In this case you have to pass the size of the output buffer as an extra argument.在这种情况下,您必须将输出缓冲区的大小作为额外参数传递。

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

Having to put a 1 as the size of a single character may seem a bit pedantic.必须将 1 作为单个字符的大小可能看起来有点迂腐。 That's because %c can read any number of character.那是因为%c可以读取任意数量的字符。 %c is just an alias for %1c (a single character). %c只是%1c (单个字符)的别名。

By knowing the buffer size scanf_s() is designed to prevent buffer overflow (a security risk).通过了解缓冲区大小scanf_s()旨在防止缓冲区溢出(安全风险)。

Although, how much these functions really help is debatable.尽管如此,这些功能究竟有多大帮助是有争议的。 See: Field Experience With Annex K .请参阅:附件 K 的现场经验

According to msdn : 根据 msdn

Unlike scanf and wscanf, scanf_s and wscanf_s require the buffer size to be specified for all input parameters of type c, C, s, S, or string control sets that are enclosed in [].与 scanf 和 wscanf 不同,scanf_s 和 wscanf_s 要求为 c、C、s、S 类型的所有输入参数或包含在 [] 中的字符串控制集指定缓冲区大小。 The buffer size in characters is passed as an additional parameter immediately following the pointer to the buffer or variable.以字符为单位的缓冲区大小作为附加参数传递,紧跟在指向缓冲区或变量的指针之后。

In the case of characters, a single character may be read as follows:在字符的情况下,单个字符可能读作如下:

char c;字符 c;

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

With scanf_s you must supply a length [1] :使用scanf_s你必须提供一个长度 [1] :

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

In the case of scanf_s think of %c to be a special shortcut for %1c , which makes this more clear.scanf_s的情况下,将%c视为%1c的特殊快捷方式,这使这一点更加清晰。

MSDNAA states [1]: MSDNAA 指出 [1]:

Unlike scanf and wscanf, scanf_s and wscanf_s require the buffer size to be specified for all input parameters of type c , C , s , S [...].与 scanf 和 wscanf 不同, scanf_s 和 wscanf_s 要求为cCsS [...] 类型的所有输入参数指定缓冲区大小。

[1] https://msdn.microsoft.com/en-us/library/w40768et.aspx [1] https://msdn.microsoft.com/en-us/library/w40768et.aspx

The documentation of scanf_s says that: scanf_s的文档说:

In the case of characters, a single character may be read as follows:在字符的情况下,单个字符可能读作如下:

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

So following should work ( See live demo here )所以以下应该有效(请参阅此处的现场演示)

#include <stdio.h>
int main(void)
{
  char i;
  printf("Do you want to be X's or O's?\n");
  scanf_s("%c",&i,1);
  printf("You chose %c\n", i);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM