简单讲述scanf()与scanf_s()的区别
scanf() 函数 :
   scanf() 函数是格式化输入函数,它从标准输入设备(键盘) 读取输入的信息。
   其调用格式为:scanf(“<格式化字符串>”,<地址表>)。
scanf_s()函数:
   scanf_s() 的功能虽然与scanf() 相同,但却比 scanf() 安全,因为 scanf_s() 是针对“ scanf()在读取字符串时不检查边界,可能会造成内存泄露”这个问题设计的。
   scanf_s()用于读取字符串时,必须提供一个数字以表明最多读取多少位字符,以防止溢出。
实例:
   (统计输入字符串中原因字母出现的个数)(调试环境:visual studio 2010  C++)
   | 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 
 |  #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");
 }
 
 |