由scanf引发的问题
时间:2010-10-13 来源:shenhailuanma
int main(void) |
zx@zhangxu:~/2/test$ ./test |
再以字符串为例:
int main(void) |
zx@zhangxu:~/2/test$ ./test |
int main(void) |
zx@zhangxu:~/2/test$ ./test |
对于遗留的'\n'处理:
int main(void) |
zx@zhangxu:~/2/test$ ./test |
对于这个问题,也顺便测试了一下getchar,gets,fgets这几个函数。
getchar()等价于getc(stdin)从stdin读一个字符。
gets(标准输入读)从stdin读一行字符串,而且它是不接收'\n',也不会遗留'\n'。gets与fgets的一个不太注意区别是:“gets并不将换行符存入缓冲区”,这一特点在一下的实例中可以体会到。
int main(void) |
zx@zhangxu:~/2/test$ ./test |
fgets(指定流读):
原型:char *fgets(char *buf,int n,FILE *fp);
返回值:若成功则返回buf,若已到达文件结尾或出错返回NULL。
int main(void) |
zx@zhangxu:~/2/test$ ./test |

<img preview="http://pic.pdowncc.com/uploadimg/ico/2025/0523/1747993424374100.png" width="32" height="32" src="http://pic.pdowncc.com/uploadimg/ico/2025/0523/1747993424374100.png" alt="弓箭勇者最新版" />
飞行射击 下载
<img preview="http://pic.pdowncc.com/uploadimg/ico/2025/0523/1747993424374100.png" width="32" height="32" src="http://pic.pdowncc.com/uploadimg/ico/2025/0523/1747993424374100.png" alt="弓箭勇者最新版" />
飞行射击 下载
<img preview="http://pic.pdowncc.com/uploadimg/ico/2025/0523/1747993424374100.png" width="32" height="32" src="http://pic.pdowncc.com/uploadimg/ico/2025/0523/1747993424374100.png" alt="弓箭勇者最新版" />
飞行射击 下载- 4 <img preview="http://pic.pdowncc.com/uploadimg/ico/2025/0523/1747993424374100.png" width="32" height="32" src="http://pic.pdowncc.com/uploadimg/ico/2025/0523/1747993424374100.png" alt="弓箭勇者最新版" />下载73.78MB · 95℃
1970-01-01
- 5 <img preview="http://pic.pdowncc.com/uploadimg/ico/2025/0523/1747993424374100.png" width="32" height="32" src="http://pic.pdowncc.com/uploadimg/ico/2025/0523/1747993424374100.png" alt="弓箭勇者最新版" />下载43.7 MB · 94℃
1970-01-01
- 6 <img preview="http://pic.pdowncc.com/uploadimg/ico/2025/0523/1747993424374100.png" width="32" height="32" src="http://pic.pdowncc.com/uploadimg/ico/2025/0523/1747993424374100.png" alt="弓箭勇者最新版" />下载110.69MB · 95℃
1970-01-01