随笔--C语言--scanf_s函数使用

#include <stdio.h>

int main()
{
	int i;
	double dl = 9832.55;
	char s[4];//性别
	char name[20];//姓名

	int n = scanf_s("%s%d%lf%s", &s, sizeof(s) - 1, &i, &dl, &name, sizeof(name) - 1);
	//返回值n代表输入成功的个数,如这句代码,如果全部输入正确,则返回4;
	// &s后面紧跟sizeof(s) - 1,这表示输入的s字符串的长度不能超过"sizeof(s) - 1",比如当前代码表示s最多输入3个字符,如果输多了,会无效,scanf_s的返回值会是0
	if (n < 4)
	{
		printf("本次输入有错误,部分数据没有正确输入!");
	}
	return 0;
}




猜你喜欢

转载自blog.csdn.net/Johnisjohn/article/details/79873103