Chapter-4: Managing Input and Output Operation
Chapter-4: Managing Input and Output Operation
Managing input
and
Output operation
Reading, processing and writing of
data are three essential functions of a
computer program.
num1 num2
But if input : 31426 50
31 426
num1 num2
Scanf (%d%*d%d”,&a,&b);
Printf(“%6d”,9876)
9 8 7 6
Printf(“%2d”,9876)
9 8 7 6
Printf(“%-6d”,9876)
9 8 7 6
Printf(“%06d”,9876)
0 0 9 8 7 6
The output of real number may be displayed
in decimal notation using the format %w.pf
If y = 98.7654
Format Output
9 8 . Printf(“%7.4f”,y)
7 6 5 4
9 Printf(“%7.2f”,y)
8 . 7 7
9 8 .Printf(“%-7.2f”,y)
7 7
9 8 . 7 Printf(“%f”,y)
6 5 4
Printing of a single character
A single character can be displayed in
a desired position using the format %wc.
The character will be displayed right-
justified in the field of w columns.
We can make the display left-justified
by placing a minus sign before w.
The default value for w is 1.
Printing of strings
The format specification for outputting
string is of the form %w.ps.
To print string “NEW DELHI 110001”,
containing 16 characters.
%20s
1 10 2
0
To print string “NEW DELHI 110001”,
containing 16 characters.
%20s
N E W D E L H I 1 1 0 0 0 1
%20.10s
N E W D E L H I
Difference between
%[a-z]
%[^a-z]
sizeof operator
when used with operand, it returns
the no. of bytes the operand
occupies…
i.e. sizeof(int) is returns 2
sizeof(char) returns 1 ..on 16bit
processor.
Q. float x; printf(“%d”,sizeof(x));
what will be the output? Why?
Precedence of arithmetic
operator
High priority * / %
Low priority + -
The basic evaluation includes left to right
passes…..
During first pass higher priority opr is
applied as they encounter….during
second pass from left to right the low
priority operators are applied as they
are encountered.
example
i.e
x = 9-12/3+3*2-1;
pass 1: x = 9 – 4 + 3*2 –1;
x = 9 – 4 + 6 –1;
Pass 2:
x= 5 +6 –1;
x=11-1;
x=10;
Use of parenthesis to override
normal precedence
X= 9 – 12 / (3+3) *(2-1)
Pass 0: x=9 – 12 / 6 * (2-1)
x=9 – 12 / 6 * 1;
Pass 1: x=9 – 2 * 1;
x=9 – 2;
Pass 2: x=7
Thus, when parenthesis are used, the
expressions within parenthesis assume
highest priority.
Associativity
The operator having same precedence
are evaluated either from left to right
or right to left…..depending of the
level … .known as associativity.
There are two ways for type conversion.