欢迎来到天天文库
浏览记录
ID:15791275
大小:39.00 KB
页数:17页
时间:2018-08-05
《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;I4、CThenthecyclewillendinadvanceReason:afterenteringaandthefirstcarriagereturn,theAandthecarriagereturnareleftinthebuffer.ThefirstscanfreadstheA,butthereisaleftintheinputbuffer,andthesecondscanfreadsthe.ThenenterBandsecondcarriagereturns.Similarly,thethirdscanfreadstheB,andthefourthscanf5、readsthesecondcarriagereturncharacters.ThefifthreadstheC.Sothefivescanfwereexecutedanddidn'tendearly.Onlysomescanfreadthecarriagereturn.Solution:changetheprogramtojustthis:For(I=0;I6、tdio.h>(main){Charc[5];Gets(C);Printf(C);Return0;}Butnote:thisfunctionautomaticallyconvertsyourlasthitreturntocharacter' '.Ifyourinputexceedsthesizeofthearray,anerrorwilloccur.Theparameterinputtypemismatchforthe3.scanf()functionThisiswhatIsawontheCSDNforum,whichissometimespuzzling.#inclu7、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
4、CThenthecyclewillendinadvanceReason:afterenteringaandthefirstcarriagereturn,theAandthecarriagereturnareleftinthebuffer.ThefirstscanfreadstheA,butthereisaleftintheinputbuffer,andthesecondscanfreadsthe.ThenenterBandsecondcarriagereturns.Similarly,thethirdscanfreadstheB,andthefourthscanf
5、readsthesecondcarriagereturncharacters.ThefifthreadstheC.Sothefivescanfwereexecutedanddidn'tendearly.Onlysomescanfreadthecarriagereturn.Solution:changetheprogramtojustthis:For(I=0;I6、tdio.h>(main){Charc[5];Gets(C);Printf(C);Return0;}Butnote:thisfunctionautomaticallyconvertsyourlasthitreturntocharacter' '.Ifyourinputexceedsthesizeofthearray,anerrorwilloccur.Theparameterinputtypemismatchforthe3.scanf()functionThisiswhatIsawontheCSDNforum,whichissometimespuzzling.#inclu7、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
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
此文档下载收益归作者所有