资源描述:
《InputandOutputinC输入和输出C》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、Lecture3:I/OinCprintf()scanf()Input/OutputinCChasnobuilt-instatementsforinputoroutput.Alibraryoffunctionsissuppliedtoperformtheseoperations.TheI/Olibraryfunctionsarelistedthe“header”file.Youdonotneedtomemorizethem,justbefamiliarwiththem.StreamsAllinputandoutputisperformedw
2、ithstreams.A"stream"isasequenceofcharactersorganizedintolines.Eachlineconsistsofzeroormorecharactersandendswiththe"newline"character.ANSICstandardsspecifythatthesystemmustsupportlinesthatareatleast254charactersinlength(includingthenewlinecharacter).TypesofStreamsinCStandardinputstr
3、eamiscalled"stdin"andisnormallyconnectedtothekeyboardStandardoutputstreamiscalled"stdout"andisnormallyconnectedtothedisplayscreen.Standarderrorstreamiscalled"stderr"andisalsonormallyconnectedtothescreen.FormattedOutputwithprintfTheprintf()functionprintsoutputtostdout,accordingtofor
4、matandotherargumentspassedtoprintf()..Thesyntaxis:printf(“format”,var1,var2,…);Thestringformatconsistsoftwotypesofitems-charactersthatwillbeprintedtothescreen,andformatcommandsthatdefinehowtheotherargumentstoprintf()aredisplayed.Basically,youspecifyaformatstringthathastextinit,aswe
5、llas"special"charactersthatmaptotheotherargumentsofprintf().Forexample:charname[20]="Bob";intage=21;printf("Hello%s,youare%dyearsold",name,age);HelloBob,youare21yearsoldFormattedOutputwithprintfFormatConversionSpecifiers:d--displaysadecimal(base10)integerl--usedwithotherspecifier
6、stoindicatea"long"e--displaysafloatingpointvalueinexponentialnotationf--displaysafloatingpointvalueg--displaysanumberineither"e"or"f"formatc--displaysasinglecharacters--displaysastringofcharactersi--signedintegeru--unsignedintegerx--unsignedhexadecimal,withlowercaseletterso--octal%
7、--a'%'signFormattedinputwithscanf();Thescanf()functionreadsinputfromstdin,accordingtothegivenformat,andstoresthedataintheotherarguments.Thesyntaxis:scanf(“format”,&var1,&var2,…);Itworksalotlikeprintf().Theformatstringconsistsofcontrolcharacters,whitespacecharacters,andnon-whitespac
8、echaracters..Example:float