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); /* 这里必须要有128,以表明最多读取128个字符,如果写成scanf_s("%s",buffer),程序将无法执行到底,且编译器会提示“Unhandled exception at 0xfefefefe in array.exe:0xC0000005: Access tion.” 。当然在安全性要求不高的情况下,不 一定非要用scanf_s()函数,可用scanf("%s",buffer)代替。 */ printf("%d vowels appear in your string.\n",CountVowel(buffer)); system("pause"); } 初学C语言,把一些心得记录下来,以供自己以后参考。