scanf()与scanf_s()
2015-10-27 16:59
337 查看
SCANF()和SCANF_S()区别
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语言,把一些心得记录下来,以供自己以后参考。
相关文章推荐
- Xutils 使用get请求时,连续请求返回数据相同问题的解决
- Android的内存优化
- vmware中linux无法动态获取dhcp解决方法
- Spring MVC 乱码问题
- QListWidget滚动条自动下滚
- HashMap实现原理
- 005.self 用来访问成员变量
- 2015年10月26日作业
- 视频H.263与H.264的比较
- 此为传送门,关于学习html、css、js、apache、php、mysql的学习
- AndroidUI的组成部分GridView
- IE 下a标签在 position:absolute 后无法点击的问题
- 策略模式 -- 大话设计模式
- apache flume sources
- 度量快速开发平台自定义控件与自定义事件的使用方法
- iOS 9 新特性之实现 3D Touch 就是 So easy -- OC版
- fill和fill_n函数的应用
- 006.self 作为方法的返回值
- 【iOS】使用 Reachability 第三方库 检测网络
- Android studio 创建百度地图应用