C Lang Module1 Part2
C Lang Module1 Part2
.
Questions: Define operator. List the different type s of operators 1n
.
• I operators available . C.
What are the•spec1a in C? E am.
• t • C xp1
List the bitwise opera ors m and explain any tw f th .th l
• th Operation of left hift d . . o o em w1 examp es.
Explain e s an nght shift operators in C with examples.
Precedence is used to determine the order in which different operators in an expression are
evaluated.
Associatively is used t~ detennine the order in which operators with same precedence are
evaluated in an ex~ress1don.
Example: y =a+b c -
• is having higher priori~ than '+~ ~~ ' -'• So first b•c is evaluated, then among '+' and ' -'
first plus is evaluated, smce a~s~1at1v1ty for + and - is left to right even though they are
having same precedence. Now - 1s evaluated and then assigned to y.
That is: Y= a+b*c-d
(precedence among+ ,-,*) --+ = 5+4*3-2
(Associativity among+,-) --+ = 5+12-2
= 17-2
= IS
Questions: Define precedence and associativity in operators and explain with example.
Define operators. Explain the operators hierarchy with respect to precedence and
associativity.
Priority Associativity
precedence Operator symbol I Operator name
Bitwise complement ~
Sizeof
3 * I % left to right
4 +- left to right
29
. . C & Data Structures Basic
s: ~O d
llle,1
Programn11ng in
left to right
:::. -- .
':;
Decrement
5.13. Problems on IJlcrement and
.rJ
ing rules:
We need to remember the follow
ty.
• Brackets are given first priori ,
tfix is giv en pri ori ty. on.
• Then pos
tfix firs t but pos tfix ope ration is done at last step of evaluati
• Consider the pos '
fix is given priori ty and follow right to left associativity. p of
• Then pre
in the exp res sion then find answer at the last but one ste
• First substitute vaJues
evaJuation.
• At last the post increment or
decrement must be performed.
3. X 3
4. X =3
k- 2
k 2
y= x+ +- k- -
y==3 ++ -2 - -
y = -- X + k- -
y==3- 2=1 Y: ::- -3 +2 --
y::=2+ 2 = 4
:. y = 1
:. y= 4
x= 4
k= 1
X-2
Ir-I
30
ics: Mod~le•I
Programming in C & Data Structures Bas
3. --i•j ++ • + • --j
4.1+
If i=4 ,j=3 if i=4, j=3
--4 *3 ++ 4+ +* --3
3* 3= 9 4 • 2 =·s
But i = 3 But i =5
j=4 j=2
.
increment or decrement must be perfonned
Ifa = 5 4. x = - - a • a + +
3. x = a - - + ++a x= 4 • %
X $+ 6 4
(---5)
6 (++5) x= 4* 4= 16
X = 6+ 6 = 12
• but a = 5 (4++)
but a = 5 (6- - )
& LBS
Type 4: Same variable at both side RBS
Ifa = 5
l.
'a= ++ a •-- a
a="' • ·5
5 (- - 6)
a=S • 5=2S
t do not perform post incrern...,.
. ~rnent bu ~,.t<>hA..
+a++ st Ul'-''" ~ra ·
a:::: - - a (consider P" t10ri
2. - 4+,9 '
a-
(-5) 4
_ 4+4 . ++ that is 8++ ==9. Therefore a ::: 9
a- • rerna1·rung a , •
a == 8 . crement as
But su·11 past an .
3. a--a--+++a
a == f + 6
6 (++S) . . __ that is J2- = 11. Therefore a== J 1.
a=== 12 d rement is rema1nmg 3 ,
But still Post ec
4. a=· -a •a++
a=4+.,8
(-5) 4
- 16 . . ++ that is 16++ =17. Therefore a= 17.
a - ·11 post increment is remauung a '
But stl
3. ~onsider expression: x = __ a• d I e _ c+ + • b
rf a =3, d=7, e-2, c=5, 1r4 then
x = - - 3 * 7 I 2 - 5+ + • 4
= - - 3 *112 - S++* 4 consider ost •
=- -3 *1/2-5++*4 consider Pre. mcrement first but do not perform action
= 2 • 112 - 5 •4 • I h · P mcrement perform action
. ' avmg same prio ·ty b .
•• associativity. n ut higher than than-, usina left to right
= 1412 - S •4, • , I havm·g same pnonty
· . b t h.gh •
associativity. u 1 er than -,
urcs Basics: Module-I
Programming in C & Data S1ruct
s
6. Programming Example
two numbers
le I: Finding sum , multiplication, division, subtraction, mod of
Eump
operations • t
sub_div_mod / • Basic arithmetic
Algorithm To_find-_sum_mutt_ Flowchart
read two num ber a and b
sum = a+b
mult = a• b
sub = a-b
div = a/ b
modu =a % b Read a and b
c Program
#include <stdio.h>
#include<conio.h> sum = a+b
void main() mult =a X b
sub = a-b
{ =a / b
; div
int a,b,sum,mult,sub,div,modu
modu =a ¾ b
clrscr( );
b\n");
printf{"e~ter the values of a and
scanf("%d%d",&a,&b);
sum=a+b; print sum ,mult,
mult=a*b; sub, div, modu
sub=a-b;
div=a/b;
. , . . \n d. · o/cd\n
modu=a¾b; is ¾d\n mult is ¾d\n sub is o/ou iv is o stop
• tlr"sum d'tv,modu)·,
modu''is %d\n",sum,mult,sub,
pnn
getch( );
}
Output:
enter the values of a and b
8
4
sum is 12
sub is 4
mu] is 32
div is 2
rem is 0
•
Fin •
ding sum and average of three numbers
Example 2:
_avg
Algorithm: to_find _sum _and
Read 3 numbers a , b aod c
sum= a+b+c
33
----....----
~ ~ - ~ ~ - min g
in C& Data Structures 8 .
progralll asics: ~
Q«j~l
tt
e~ sum/ 3.0
a,•crag e
print sUlll, a,•crag Flowchart
CPro&rldl • )
#include 4td~o.h
.
#inc1ude«oruo,h)
void main()
Read a
{ • tab sum;
,n ' '
Ooat a,g; th c:c ,·atucs a band c\n '')·'
Print~"Ent~;,,o/d" &a &b,&C); sum == a+b+c
scan1a..o
\ '
~~ou ,o ' '
sum~a-+ b+c; avg= sum /3 .Os
avg"' sum/ 3.0; •' )·
print~~sum is ¾dlnf\n~~m,g,).
printQ~ avg is¾ ,a, '
gctch( );
print sum,
}
Output:
Enter three values ab and c
4
8
12
Sum is 24
Avg is 8.000000
Examplt3:.Swapping of two numbers without
temporary variable
'
Algorithm: swap_a_and_b_without _tem
p
Read a and b let a=S, b=7
a =a+b a=S+7= 12 here 5 stored in a replaced by
b= a-b 12
b= 12-7=5 here 7 stored in b replaced
a=a-b by 5
a=12-5=7 here 12 stored in a replaced
write out a and b by 7
C Program
#include <stdio.h> Flowchart
#include<conio.h>
void main()
{
int a,b;
printf("Enter the values of a and b '')'
Read a and b
scanf("%d%d ",&a,&b); '
a=a+b·,
a= a+b; b=a-b; a-a-b
b=a-b;
a a-b·,
printf{"after swapping a iso/od\tb is¾t\n" '
print aand b
getch( ); b) .
' a, '
34
,.
}I
I
i
Programming •m C & Dala Structures Basics: Module-I
I
}
output:
Enter the values of a and b
12
21 12
After swapping a is 21 b is
• g temporary variable
o numbers usin
Example 4: Swapping of tw
_with temp
A)gorithm: s,vap_a_and_b -
Reada and b
temp= a
a= b
t,= temp
print a and b
Flowchart
C Program
#include <stdio.h>
#include<conio.h>
void main()
{
int a,b,temp; Read a and b
a and b ");
printf("Enter the values of
scanf("%d%d ",&a,&b);
temp a; temp=a
a= b; a=b
b= temp;_ b=temp
% d \tb is %d\n", a,b) ;
printf( "after swapping a is
getch( );
} print a and b
Output:
Enter the values of a and b
12
21
12
After swapping a is 21 b is
r) of the circle
d circumference (perimete
Example S: Finding area an
Algorithm: Area_of_circle
Read radius r
initialize pi value
area= pi • r • r
circ = 2*pi*r
.
write out area and circ .
, 3S
.,.
.•
Flowchart
crrocra111
#include <5tdio.h"
#include«onio,b)
void main()
{ float area, r,pi, cirC;
pi::: 3. 142; di value \n'');
rintft''Enter the ra us pi=3.142
~"3/,f',&r); area=pi •r•r
area::: pi•r•r; circ=2*pi•r
circ:::2•pi•r; . o1. err·c""''"erence is¾f', area, circ);
"fl
Output:
Enter the radius value
2.45
area is 18.859855
circumference is 1S.395800
Eumple 6: finding area and perimeter of triangle
>Jcoritbm: Area_of_triangle
read three sides of triangle say ab and c
calculate perimeter
s=(a+b+cY 2.0.
~ = ~s(s-a)(s-b)(s-c) Flowchart
pnnt area and perimeter
C Procr•m :-
#include <stdio.h>
#include<conio.h>
#include <math.h>
void main() Read a, b,c
{
float a,b,c, s, area·
printf(''Enter the 'val
scanf("%1"/of%f',& ~s of a,b and c \n");
s= (a+b+c
area= sqrt)/2.0
(• a, b,&c); - s=(a+b+c)/2.0
printf("perune ter 1s ¾f
_s (s-at(s d
-b)•(s-c)·' Area= sqrt(s*(s-at
} getch( ); an area is% f\n",s,area) ; ----= (s-b)*(s-c)
-_:_ _~"_ !___ _j
s da fr .
E11mple 8: To find years, month ' ys om gJven no of days.
th da s
Algorithm: To_find_year_mon - y
Read number of days
year= days/ 365
ad =days % 365
months= ad / 30
Jeftdays =ad % 30
ys.
Write out year, months, Jeftda
C Program
#include <stdio.h>
#include<conio.h>
void main()
{
tdays;
int days, year, ad, months, lef
printf ( ''enter the days");
scanf (4'%d",&days);
year= days /365;
ad=days % 365;
months =ad /30;
Jeftdays =ad%30; hs, left days);
nt£'" ye ar= % d \nm onths=%d\nleftdays = %d\n",year,mont
pri
getch( );
}
ve statements are given in the
asics) using condition and ite~ti
Note: Remaining programs (B
next module.
simple Interest: SI = (p*t*r) I 100
Example 9: C program to find
#include <stdio.h>
#include<conio.h>
void main()
{
float SI, p,t,r;
amount ");
printf(" \n Enter the principle
scanf (" %f ', p); .
");
printf ( " enter the tim ration
e du
scanf("%f', & t):
st");
printf ("enter the rate of intere
scanf("¾ f', & r);
SI - (p•t•r)/100
37
Prott~m min
-· -- - g in C&Data Structur
es nas•
. rest:: 0;0f\ SI)
,~,: ~
;
pn.nt"1, ~simple ,nte
gctchl ); \
()utput: .ncip\e
£ntct the pn amount
15 .34 . dur.ition
En\ct \he tune
20 ,\. r1tc ofintcrcst
[n \~ u1 C ,_.
read a,b
• C& Data Structu~es Basics: Module-I
Programming m
false
is a>b?
true