scanf() 函数 :
scanf() 函数是格式化输入函数,它从标准输入设备(键盘) 读取输入的信息。
其调用格式为:scanf("<格式化字符串>",<地址表>)。
scanf_s()函数:
scanf_s() 的功能虽然与scanf() 相同,但却比 scanf() 安全,因为 scanf_s() 是针对“ scanf()在读取字符串时不检查边界,可能会造成内存泄露”这个问题设计的。
scanf_s()用于读取字符串时,必须提供一个数字以表明最多读取多少位字符,以防止溢出。
实例:(统计输入字符串中原因字母出现的个数)(调试环境:visual studio 2010 C++)
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<CountVowel.h>
int CountVowel(char str[])
{
int counter = 0;
int i;
for (i = 0; str[i] != '\0' ; ++i )
{ switch(str[i])
{ case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
++counter;
}
}
return counter;
}
void main()
{
char buffer[128];
printf("Please input a string:\n");
scanf_s("%s" , buffer,128);
printf("%d vowels appear in your string.\n",CountVowel(buffer));
system("pause");
}