scanf()与scanf_s()
来源:互联网 发布:php t double arrow 编辑:程序博客网 时间:2024/05/17 02:39
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语言,把一些心得记录下来,以供自己以后参考。
#include<string.h>
#include<CountVowel.h>
{
int counter = 0;
int i;
for (i = 0; str[i] != '\0' ; ++i )
{ switch(str[i])
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)代替。 */
}
0 0
- scanf()与scanf_s()
- scanf与scanf_s比较
- printf,scanf与printf_s,scanf_s
- scanf() 与 scanf_s() 的区别
- scanf与scanf_s的区别
- scanf() 与 scanf_s() 的区别
- scanf()函数与scanf_s()函数的区别
- vs编译器中scanf_s与scanf的区别。
- scanf_s和scanf区别
- VS2015 scanf() scanf_s
- scanf和scanf_s
- C语言常见错误杂谈(一)scanf()、scanf_s()与错误 C4996与解决方法
- 关于scanf与scanf_s的区别,以及用scanf编译出错并且提示找不到可执行文件.exe的解决办法。
- C语言中 scanf_s和 scanf 区别
- scanf_s
- scanf_s
- scanf和scanf_s、strcpy和strcpy_s、strncpy、gets
- 关于scanf和scanf_s的注意事项及问题
- 此为传送门,关于学习html、css、js、apache、php、mysql的学习
- Spring MVC 乱码问题
- (二)MySQL InnoDB锁模型
- Android的内存优化
- Xutils 使用get请求时,连续请求返回数据相同问题的解决
- scanf()与scanf_s()
- STL算法:prev_permutation和next_permutation的使用
- django cookies 和 session
- arguments caller callee
- 基因检测-剪切联配(1)
- hadoop本地库不一致的解决方案
- iOS中SQLite的简单实用
- python异常处理
- jQuery中使用ajax向后台传递数据