scanf函数探讨(discussion on scanf function)

scanf函数探讨(discussion on scanf function)

ID:15791275

大小:39.00 KB

页数:17页

时间:2018-08-05

scanf函数探讨(discussion on scanf function)_第1页
scanf函数探讨(discussion on scanf function)_第2页
scanf函数探讨(discussion on scanf function)_第3页
scanf函数探讨(discussion on scanf function)_第4页
scanf函数探讨(discussion on scanf function)_第5页
资源描述:

《scanf函数探讨(discussion on scanf function)》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库

1、scanf函数探讨(Discussiononscanffunction)Discussiononscanffunction:1.blanksymbolproblem#include(main){Inta;Printf("inputthedata");Scanf(%d,&a);//heremorethanacarriagereturnPrintf("%d",a);Return0;}Asaresult,twonumbersareenteredbeforetheendoftheprogram,nottheexpectedone.Why?Reason

2、:whenwhitespaceends,scanfskipswhitespacetoreadthenextcharacter,soyoumustenteranumberagain.Herethewhitespaceincludingspace,tab,newline,carriagereturnandformfeed.Soifyouusescanf("%d","&a"),thesameproblemwilloccur.Solution:thiskindofmistakeismostlyinputcarelessly,justpaymoreattentiontoit.Thi

3、sproblemisnotgood,check,compilenoproblem,ablankisnoteasytosee.Whenyourprogramappearsaboveproblems,checkityourself.2.bufferissuesThisisaveryeasyplacetogowrong,andImissitmanytimes.#include(main){Intn=5;Charc[n];For(inti=0;I

4、CThenthecyclewillendinadvanceReason:afterenteringaandthefirstcarriagereturn,theAandthecarriagereturnareleftinthebuffer.ThefirstscanfreadstheA,butthereisaleftintheinputbuffer,andthesecondscanfreadsthe.ThenenterBandsecondcarriagereturns.Similarly,thethirdscanfreadstheB,andthefourthscanf

5、readsthesecondcarriagereturncharacters.ThefifthreadstheC.Sothefivescanfwereexecutedanddidn'tendearly.Onlysomescanfreadthecarriagereturn.Solution:changetheprogramtojustthis:For(I=0;I

6、tdio.h>(main){Charc[5];Gets(C);Printf(C);Return0;}Butnote:thisfunctionautomaticallyconvertsyourlasthitreturntocharacter''.Ifyourinputexceedsthesizeofthearray,anerrorwilloccur.Theparameterinputtypemismatchforthe3.scanf()functionThisiswhatIsawontheCSDNforum,whichissometimespuzzling.#inclu

7、de(main){Inta=123;Charc='t';Printf("input");Scanf("%d%c","&a","&c");Scanf("%d%c","&a","&c");Scanf("%d%c","&a","&c");Printf("%d%c","a","C");Return0;}Whenyouenteraenter,youwillskipthefollowing2scanfstatements,andoutputdirectlytoOnehundredandtwenty-three

当前文档最多预览五页,下载文档查看全文

此文档下载收益归作者所有

当前文档最多预览五页,下载文档查看全文
温馨提示:
1. 部分包含数学公式或PPT动画的文件,查看预览时可能会显示错乱或异常,文件下载后无此问题,请放心下载。
2. 本文档由用户上传,版权归属用户,天天文库负责整理代发布。如果您对本文档版权有争议请及时联系客服。
3. 下载前请仔细阅读文档内容,确认文档内容符合您的需求后进行下载,若出现内容与标题不符可向本站投诉处理。
4. 下载文档时可能由于网络波动等原因无法下载或下载错误,付费完成后未能成功下载的用户请联系客服处理。