VS中输入字符串和输出字符串问题

**web

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(),而这个函数的用法不能像上面那么用,正确的用法应该是:svg

#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这个怎么实现一劳永逸,请参考个人另外一篇博客。code