MANAGING INPUT AND OUTPUT OPERATIONS IN C MRS.SOWMYA JYOTHI.pdf

2,078 views 26 slides Mar 31, 2022
Slide 1
Slide 1 of 26
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26

About This Presentation

MANAGING INPUT AND OUTPUT OPERATIONS IN C
REFERENCES: PROGRAMMING IN C BY BALAGURUSWAMY
MRS.SOWMYA JYOTHI,SDMCBM,MANGALORE


Slide Content

MANAGING INPUT AND OUTPUT OPERATIONS
REFERENCE: PROGRAMMING IN C BY BALAGURUSWAMY
MRS. SOWMYA JYOTHI, SDMCBM, MANGALORE

Theoperationswhichtookplaceinordertotakedataandmake
thedisplayoftheprocessedinformationareknownasinput&
outputoperations.
InClanguage,ingeneral,userusescanfandprintffunctionsfor
inputandoutputpurposerespectively.Theproperusesofthese
functionsaretermedmanagementofinput&outputoperation.
TherearealsomanyfunctionswhichareusedinCprogramming.
Thereexistsseveralfunctionsin‘C’languagethatcancarryout
inputoutputoperations.Thesefunctionsarecollectivelyknown
asstandardInput/OutputLibrary.Eachprogramthatuses
standardinput/outputfunctionmustcontainthestatement.

Reading a character:
Thebasicoperationdoneininputoutputistoreadacharactersfromthe
standardinputdevicesuchasthekeyboardandtooutputorwritingitto
theoutputunitusuallythescreen.
Thegetcharfunctioncanbeusedtoreadacharacterfromthestandard
inputdevice.Thiscanalsobedonewiththehelpofthescanffunction.
The getcharhas the following form.
variable name = getchar( );
variable name is a valid ‘C’ variable, that has been declared already and
that possess the type char.

# include < stdio.h>
void main ( )
{
char ch;
printf(“Type one character:”) ;
ch= getchar() ;
printf(” The character you typed is = %c”, ch) ;
}

Writing a character:-
The putcharfunction which in analogusto getcharfunction can be
used for writing characters one at a time to the output terminal.
The general form is
putchar(variable name);
Where variable is a valid C type variable that has already been declared
Ex:-putchar(ch);
Displays the value stored in variable C to the standard screen.

#include < stdio.h>
void main ( )
{
char in;
printf(” please enter one character”);
in = getchar( ) ;
putchar(in);
}

Character test functions
C supports some character test functions that are contained in the file ctype.hand therefore the
statement. #include<ctype.h> must be included in the program.
FunctionTest
isalnum(c)is c an alphanumeric character
isalpha(c)is c an alphabeticcharacter
isdigit(c)is c a digit
islower(c)is c a lowercase letter
isprint(c)is c a printable character
isupper(c)Is c an uppercase letter
ispunct(c)Is c a punctuation mark
isspace(c)Is c a white space character

Formatted Input:
Theformattedinputreferstoinputdatathathasbeenarrangedina
particularformat.Inputvaluesaregenerallytakenbyusingthescanf
function.
Thescanffunctionhasthegeneralform:-
scanf(“controlstring”,arg1,arg2,arg3………….argn);
Theformatfieldisspecifiedbythecontrolstringandtheargumentsarg1,
arg2,…...,argnspecifiestheaddressoflocationwhereaddressistobe
stored.
Thecontrolstringspecifiesthefieldformatwhichincludesformat
specificationsandoptionalnumberspecifyingfieldwidthandthe
conversioncharacter%andalsoblanks,tabsandnewlines.
TheBlankstabsandnewlinesareignoredbycompiler.The
conversioncharacter%isfollowedbythetypeofdatathatistobeassigned
tovariableoftheassignment.Thefieldwidthspecifierisoptional.

Inputting Integer Numbers:-
The general format for reading a integer number is
% wd
Here percent sign (%) denotes that a specifier for conversion follows
and
w is an integer number which specifies the width of the fieldof the number
that is being read. The data type character d indicates that the number should
be read in integer mode.

Example : scanf(“%3d %4d”, &sum1, &sum2);
Ifthevaluesinputare175and1342herevalue175isassignedtosum1
and1342tosum2.
Supposetheinputdatawasfollows1342and175.
Thenumber134willbeassignedtosum1andsum2hasthevalue2
becauseof%3dthenumber1342willbecutto134andtheremainingpartis
assignedtosecondvariablesum2.
Iffloatingpointnumbersareassignedthenthedecimalorfractionalpartis
skippedbythecomputer.
To read the long integer data type we can use conversion specifier% ld& % hd
for short integer.

Inputting real numbers:
Unlike integer numbers, field specifications are not to be used
while representing a real number therefore real numbers are specified
in a straight forward manner using % f specifier.
The general format of specifying a real number input is
scanf(“% f “, &variable);
Example: scanf(“%f %f % f”, &a, &b, &c);
With the input data 321.76, 4.321, 678. The values 321.76 is assigned
to a , 4.321 to b & 678 to C.
If the number input is a double data type then the format specifier
should be % lf instead of %f.

Inputting character strings:
Single character or strings can be input by using the character
specifiers.
The general format is % wcor %ws
Where c and s represents character and string respectively and w
represents the field width.
The address operator need not be specified while we input
strings.
The specification %s terminates reading at the encounter of a
blank space.

Example : scanf(“%c %15s”, &ch, name):
Some versions of scanfsupport the following conversion specifications
for strings:-
%[characters]
%[^characters]
The specification %[characters] means that only the characters
specified within the brackets are permissible in the output string. If
the input string contains any other character, the string will be
terminated at the first encounter of such a character.
The specification %[^characters] does not permit the characters
specified after the circumflex(^) in the input string.

Reading mixed Data types:-
scanfstatement can also be used to input a data line
containing mixed mode data.
For example:
scanf(“%d %c %f %s”, &count, &code, &ratio, name};

Formatted Output:-
Printffunction is used to produce the outputs in such a way that they are
understandable and are in an easy to use form.
The general form of printfstatement is
printf(“control string”, &arg1,&arg2,……….argn);
Control string consists of 3 types of items:
•Characters that will be printed on the screen as they appear.
•Format specifications that define the output format for display of each item.
•Escape sequence characters such as \n, \t and \b.
The arguments arg1, arg2,……argnare the variables whose values are
formatted and printed according to the specifications of the control string.

A simple format specification has the following form:
% w.ptype-specifier
Where w is an integer number that specifies the total
number of columns for the output value and
p is another integer number that specifies the number of digits
to the right of the decimal point or the number of characters to
be printed from a string.
A newline can be introduced by the help of a newline character.
For example:-printf(“Enter 2 numbers”);

For example
#include < stdio.h>
main ( )
{
printf(“Hello!”);
printf(“Welcome to the world of Engineering!”);
}
Output:
Hello! Welcome to the world of Engineering.
For Example :
printf(“Hello!\n”);
OR
printf(“\n Welcome to the world of Engineering”);

Output of integer numbers:-
The format specification for printing an integer number is
%wd
Where
w specifies the minimum field width for the output.
d specifies that the value to be printed is an integer.

For example:
printf(“%d”,9876);
printf(“%6d”, 9876)
printf(“%2d”, 9876);
printf(“%-6d”, 9876);
printf(“%06d”, 9876);
Itispossibletoforcetheprintingtobeleft-justifiedbyplacinga
minussigndirectlyafterthe%character.
Itisalsopossibletoplacezeroesbeforethefieldwidthas
showninthelastexample.

Output of Real Numbers:-
The output of a real number may be displayed in decimal notation using
the following format specification:
% w.pf
The integer w indicates the minimum number of positions that are to
be used for the display of the value and the integer p indicates the number
of digits to be displayed after the decimal point.
The value, when displayed, is rounded to p decimal places and printed right-
justified in the field of w columns. Leading blanks and trailing zeros will
appear as necessary. The default precision is 6 decimal places. The negative
numbers will be printed with the minus sign.
y=98.7654

printf(“%7.2f”, y);
printf(“%-7.2f”, y);
printf(“%f”, y);
printf(“%10.2e”,y);
printf(“%11.4e”,-y);
printf(“%-10.2e”,y);
printf(“%e”,y);
printf(“%7.4f”, y)

Printingasinglecharacter:-
Asinglecharactercanbedisplayedinadesiredpositionusing
theformat:
%wc
Thecharacterwillbedisplayedright-justifiedinthefieldofw
columns.Wecanmakethedisplayleft-justifiedbyplacingaminus
signbeforetheintegerw.
Thedefaultvalueforwis1.

PrintingofStrings:-
Theformatspecificationforoutputtingstringsissimilarto
thatofrealnumbersthatis
%w.ps
Wherewspecifiesthefieldwidthfordisplayandpinstructsthat
onlythefirstpcharactersofthestringaretobedisplayed.The
displayisright-justified.
Forexample:Printingthestring“NEWDELHI110001”.
1 23 4 5 6 7 8 9 0 1 2 3 4 5
6 7 8 9 0

%s
%20s
%20.10s
%.5s
%-20.10s
%5s

Mixed Data Output:-
Itispermittedtomixdatatypesinoneprintfstatement.
Forexample:-
printf(%d%f%s%c”,a,b,c,d);
printfusesitscontrolstringtodecidehowmanyvariables
tobeprintedandwhattheirtypesare.Therefore,the
formatspecificationsshouldmatchthevariablesin
number,order,andtype.

SpecifierMeaning
%c –Print a character
%d –Print a Integer
%i–Print a Integer
%e –Print float value in exponential form.
%f –Print float value
%g –Print using %e or %f whichever is smaller
%o –Print actual value
%s –Print a string
%x –Print a hexadecimal integer (Unsigned) using lower case a –F
%X –Print a hexadecimal integer (Unsigned) using upper case A –F
%a –Print a unsigned integer.
%p –Print a pointer value
%hx–hex short
%lo –octal long
%ld–long