////************输入scanf函数不能输入斜杠N否则会报错*********
VS中输入字符串和输出字符串问题
**
因为自己刚开始用VS,不是很习惯,今天发现一个问题,就是我想实现输入一段字符串,然后在将它输出来,发现没有输出,反而是听了一会儿,然后闪退了,同样的代码放到Dev C++运行却很好使,我的代码如下:
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
int main(void)
{
char a[1000];
int i;
scanf_s("%s", a);
printf("%s", a);
system("pause");
return 0;
}
**************************************************重点
**********************************************
找了挺久的问题,最后发现,因为VS自己加入了安全输入函数,scanf_s(),而这个函数的用法不能像上面那么用,正确的用法应该是:
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
int main(void)
{
char a[1000];
int i;
scanf_s("%s", a, sizeof(a)); //需要加一个传入参数
printf("%s", a);
system("pause");
return 0;
}
这时候运行就正常了,不想这么麻烦的话可以不用scanf_s(),而是用scanf(),代码如下:
#define _CRT_SECURE_NO_WARNINGS 1
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
int main(void)
{
char a[1000];
int i;
scanf("%s", a);
printf("%s", a);
system("pause");
return 0;
}
这样也能成功,至于#define _CRT_SECURE_NO_WARNINGS 1这个怎么实现一劳永逸,请参考我的另一篇博客。
————————————————
版权声明:本文为优快云博主「菜鸟养成小记」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.youkuaiyun.com/qq_21793157/article/details/90524717
文章讲述了在VisualStudio(VS)中使用scanf_s函数输入字符串时遇到的问题,由于VS的安全特性,需要额外传递数组大小作为参数,否则程序会出错。解决方案是提供正确的参数或改用scanf函数。通过#define_CRT_SECURE_NO_WARNINGS可以禁用相关警告。
4392





