Sie sind auf Seite 1von 6

8/4/2016

CProgrammingInputOutput(I/O):printf()andscanf()

CProgrammingInputOutput(I/O):printf()
andscanf()
This tutorial focuses on two inbuild functions printf() and scanf() to perform I/O task
in C programming. Also, you will learn how you write a valid program in C.

Cprogramminghasseveralinbuildlibraryfunctionstoperforminputandoutputtasks.
TwocommonlyusedfunctionsforI/O(Input/Output)areprintf()andscanf().
Thescanf()functionreadsformattedinputfromstandardinput(keyboard)whereasthe
printf()functionsendsformattedoutputtothestandardoutput(screen).
http://www.programiz.com/cprogramming/cinputoutput

1/6

8/4/2016

CProgrammingInputOutput(I/O):printf()andscanf()

Example#1:COutput
#include<stdio.h>//Thisisneededtorunprintf()function.
intmain()
{
printf("CProgramming");//displaysthecontentinsidequotation
return0;
}

Output
CProgramming

Howthisprogramworks?
AllvalidCprogrammustcontainthemain()function.Thecodeexecutionbeginsfrom
thestartofmain()function.
Theprintf()isalibraryfunctiontosendformattedoutputtothescreen.Theprintf()
functionisdeclaredin"stdio.h"headerfile.
Here,stdio.hisaheaderfile(standardinputoutputheaderfile)and#includeisa
preprocessordirectivetopastethecodefromtheheaderfilewhennecessary.Whenthe
compilerencountersprintf()functionanddoesn'tfindstdio.hheaderfile,compiler
showserror.
Thereturn0statementisthe"Exitstatus"oftheprogram.Insimpleterms,program
ends.

Example#2:CIntegerOutput
#include<stdio.h>
intmain()
{
inttestInteger=5;
printf("Number=%d",testInteger);
return0;
}

Output
Number=5

http://www.programiz.com/cprogramming/cinputoutput

2/6

8/4/2016

CProgrammingInputOutput(I/O):printf()andscanf()

Insidethequotationofprintf()function,thereisaformatstring"%d"(forinteger).Ifthe
formatstringmatchestheargument(testIntegerinthiscase),itisdisplayedonthescreen.

Example#3:CIntegerInput/Output
#include<stdio.h>
intmain()
{
inttestInteger;
printf("Enteraninteger:");
scanf("%d",&testInteger);
printf("Number=%d",testInteger);
return0;
}

Output
Enteraninteger:4
Number=4

Thescanf()functionreadsformattedinputfromthekeyboard.Whenuserentersaninteger,
itisstoredinvariabletestInteger.Notethe'&'signbeforetestInteger&testIntegergets
theaddressoftestIntegerandthevalueisstoredinthataddress.

Example#3:CFloatsInput/Output
#include<stdio.h>
intmain()
{
floatf;
printf("Enteranumber:");
//%fformatstringisusedincaseoffloats
scanf("%f",&f);
printf("Value=%f",f);
return0;
}

Output
Enteranumber:23.45
Value=23.450000

http://www.programiz.com/cprogramming/cinputoutput

3/6

8/4/2016

CProgrammingInputOutput(I/O):printf()andscanf()

Theformatstring"%f"isusedtoreadanddisplayformattedincaseoffloats.

Example#4:CCharacterI/O
#include<stdio.h>
intmain()
{
charvar1;
printf("Enteracharacter:");
scanf("%c",&var1);
printf("Youentered%c.",var1);
return0;
}

Output
Enteracharacter:g
Youenteredg.

Formatstring%cisusedincaseofcharactertypes.

LittlebitonASCIIcode
Whenacharacterisenteredintheaboveprogram,thecharacteritselfisnotstored.Instead
anumericvalue(ASCIIvalue)isstored.Andwhenwedisplayedthatvalueusing"%c"text
format,theenteredcharacterisdisplayed.

Example#5:CASCIICode
#include<stdio.h>
intmain()
{
charvar1;
printf("Enteracharacter:");
scanf("%c",&var1);
//When%ctextformatisused,characterisdisplayedincaseofcharactertypes
printf("Youentered%c.\n",var1);
//When%dtextformatisused,integerisdisplayedincaseofcharactertypes
printf("ASCIIvalueof%cis%d.",var1,var1);
return0;
}

Output
http://www.programiz.com/cprogramming/cinputoutput

4/6

8/4/2016

CProgrammingInputOutput(I/O):printf()andscanf()

Enteracharacter:g
Youenteredg.
ASCIIvalueofgis103.

TheASCIIvalueofcharacter'g'is103.When,'g'isentered,103isstoredinvariable

YoucandisplayacharacterifyouknowASCIIcodeofthatcharacter.Thisisshownby
followingexample.

Example#6:CASCIICode
#include<stdio.h>
intmain()
{
intvar1=69;
printf("CharacterhavingASCIIvalue69is%c.",var1);
return0;
}

Output
CharacterhavingASCIIvalue69isE.

ClickheretolearnmoreaboutthecompleteASCIIreference.

MoreonInput/OutputoffloatsandIntegers
IntegerandfloatscanbedisplayedindifferentformatsinCprogramming.

Example#7:I/OofFloatsandIntegers

http://www.programiz.com/cprogramming/cinputoutput

5/6

8/4/2016

CProgrammingInputOutput(I/O):printf()andscanf()

#include<stdio.h>
intmain()
{
intinteger=9876;
floatdecimal=987.6543;
//Printsthenumberrightjustifiedwithin6columns
printf("4digitintegerrightjustifiedto6column:%6d\n",integer);

//Triestoprintnumberrightjustifiedto3digitsbutthenumberisnotrightadju
printf("4digitintegerrightjustifiedto3column:%3d\n",integer);
//Roundstotwodigitplaces
printf("Floatingpointnumberroundedto2digits:%.2f\n",decimal);
//Roundsto0digitplaces
printf("Floatingpointnumberroundedto0digits:%.f\n",987.6543);
//Printsthenumberinexponentialnotation(scientificnotation)
printf("Floatingpointnumberinexponentialform:%e\n",987.6543);
return0;
}

Output
4digitintegerrightjustifiedto6column:9876
4digitintegerrightjustifiedto3column:9876
Floatingpointnumberroundedto2digits:987.65
Floatingpointnumberroundedto0digits:988
Floatingpointnumberinexponentialform:9.876543e+02

Checkouttheseexamplestolearnmore:
C"Hello,World!"Program
CProgramtoPrintanIntegerEnteredbytheUser
CProgramtoAddTwoIntegers

PreviousPage

NextPage

CopyrightbyProgramiz|Allrightsreserved|PrivacyPolicy

http://www.programiz.com/cprogramming/cinputoutput

6/6

Das könnte Ihnen auch gefallen