提问人:kadu 提问时间:10/9/2023 更新时间:10/9/2023 访问量:46
scanf_s导致 Code::Blocks C 程序中没有输出和意外调试退出的问题
Issue with scanf_s causing no output and unexpected debugging exit in Code::Blocks C program
问:
我在 Code::Blocks 中编写 C 程序时遇到了问题。尽管该程序运行没有错误,但当我使用 scanf_s(“%c %c %c”, &a, &b, &c);语句,并且在调试期间,它总是在 scanf_s 语句处意外退出。如何解决此问题?
#include <stdio.h>
int main() {
char a;
char b;
char c;
scanf_s("%c%c%c",&a,1,&b,1,&c,1);
printf("%c %c %c\n", a, b, c);
return 0;
}
在此处输入图像描述我是 C 编程的新手,我试图将语句修改为 scanf_s(“%c%c%c”, &a, 1, &b, 1, &c, 1);,这导致程序成功执行。您能否解释一下为什么此更改解决了该问题?
答:
1赞
chux - Reinstate Monica
#1
"%c"
期望 2 个参数,一个指针 () 和大小 (scanf_s()
char *
int
)更好的代码会检查结果值。
OP 可能需要一个前导空格来使用空格。
考虑用作读取用户输入行的替代方法。
fgets()
if (scanf_s(" %c%c%c",&a,1,&b,1,&c,1) != 3) {
printf("Oops\n");
} else {
printf("%c %c %c\n", a, b, c);
}
评论
scanf_s
_s
scanf
Unlike scanf and wscanf, scanf_s and wscanf_s require you to specify buffer sizes for some parameters. Specify the sizes for all c, C, s, S, or string control set [] parameters
c
_s
的 scanf
(和系列)引用也说该格式需要一对参数,第二个参数是大小。如果不将正确和预期的参数传递给函数,则不应期望它们起作用。%c
scanf_s
scanf
_s