0% found this document useful (0 votes)
9 views

C Lang Module1 Part2

c language textbook engineering 1st year

Uploaded by

suryaparkerstarc
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

C Lang Module1 Part2

c language textbook engineering 1st year

Uploaded by

suryaparkerstarc
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Programming in C & Data Structures Basics: Module-I

.
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.

SJl. Precedence and Associativity

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.

Table 5: Operators hierarchy or Precedence - Associativity Table for C operators

Priority Associativity
precedence Operator symbol I Operator name

( ) [ ] a++ a- Brackets, Post


l left to right
increment /decrement
Preincrement / decrement ++a, --a
Unary plus / minus + -
2 Logical negation ! right to left

Bitwise complement ~
Sizeof

3 * I % left to right

4 +- left to right

5 << >> left shift right shift left to right


< < - left to right
6
> > =

29
. . C & Data Structures Basic
s: ~O d
llle,1
Programn11ng in

left to right
:::. -- .
':;

7 &Bitwise AND Jeft to right --


I\ Bitwise OR --
8 left to right
I Inclusive OR
-
9 && Logical AND left to right
-
II Logjcal OR
JO al
? . Temarr condition
..
== Assignment right to left

short hand assignment right to left


11
(+-, -==, ¾ / ,*==,<<,>>,"'==,/=
=,&-), left to right
comma

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.

h RHS & LHS of e1pression


Type 1: Different variables at bot
1. X =3 2. y = - - k * + + x
k=2 y= -- 2* ++ 3
y= x+ + - --k y = I *4 = 4
y=3++ - - -2 :. y =4
y= 3 -1 k= I
y=2 x= 4
:.y=2
x= 4
k= J

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

Type 2: Only expression (no assig nment) With different variables


l. j=4 2. j=4 , k=2
j=3 ++ k¾ •-j
i+ +- j--
++ 2% --4
4+ + -3 -- 3% 3= 0
4-3 =1 But k=3
But i =5 j=3
j=2

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

Type 3: Same variables only at right


side

Note: Variable can hold only single valu


- .
e at a time. That is when a value is increme
nted or
ion wherever it occurred changes.
decremented then its value i_n entire express
ifa = 5
1. x = +_+ a * - ·--~a 2. x = - - a + a + +
' x =~ • s x= 4 +-! "
5 · {:_6) (---5) 4
x= 5* 5 x= 4+ 4
x= 25 x= 8
but a= 5 but a = a++ = 4+:+ = 5
/
rity
as post increment is having highest prio
1/

abo ve examp le 2, con sider a++ first


Note: In the ent, the post
do not per form post increm ent ope ration. At the last step, after the assignm
but 1

.
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

Type S: l\Ustd type


. - •x-b/a++*c where x=8, a=4, b=3, c-2. .
t. Consider expression z= 3 .d po.st increment first but do not perfonn action
z =-4 • 8 . 3/A++ • 2 cons1 er
3 3
z=3•8-3/3*2
z =24-3 / 3 • 2
z=24-1 • 2
z=24-2
z=22
But a =a++ = 3++ = 4.

2. Consider expression a=--a•x-b/a++*c where x=8, a 4, b-3, c 2.


a = --4 • 8 - 3/A++ • 2 consider post increment first but do not perform action
3 3
a=3 • 8-3/3 *2
a=24-3 /3 • 2
a=24-1 • 2
a=24-2
a=22
But a= a++= 22++ = 23.

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

== 1- ~4 • having higher priority than_


== 7-20
== - 13

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

print f("arei IS ft 1•• "'"''


getch( );
print area and
}

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

Output print s and area


Enter values a, b and C
4.I 5.2 6.3
stop
36
ures Basics: Module-I
Progra,nming in C & Data Struct

perimeter is 7.800000 and area lS 10.609)46
m
E1ample 7: similar to above exa p1es area of rectangle and its perimeter can be obtained
using below fonnulas.
eight
Are_a of rectangle== breadth•h
Perimeter of rectangle =2(b
readth + height)

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 ,_.

\\.l 5616\ 600


S,•mp\C·n
l •c
\ rcsl = •
.
Note: for the abo,·c pr to v.tite a\gonthm
ogram try and flowchart.
£.11mp\c \0: Addition
al program
~include< stdio.h>
r,nc\uJc<conio.h'>
,oid main()
l
int a, b;
scanfl\'(1/od¾d", &a,&
b)~
printfC~/od'\ l a>b))~
pdntfl'~/od'\ ( a<b))~
printfl\~/od'\ ( a!=b));
printfl'"°/od'', ( a=b)
);
getch( )~
)

ln the above progra


m expressions a>b,
gi\'en in integer so sa a<b,
y true as l and fa\se a!-b, a b are evaluated and valu
below for different ,·a as 0, then the outp e
\ues of a and b. ut for above proble has to be
m will be as
lf a=4, b=4 then ou
tput is
0 If a=4, b=3 then ou
tput is If a=4,
0 l b=S then output is
0 0 0
l l l
0 l
·E1ample 11: Flow 0
cahrt to find largest
of two numbers

read a,b
• C& Data Structu~es Basics: Module-I
Programming m

false

is a>b?

true

print "a is big" print "b is big"

bers is given in Model question


Example 12: Flowchart for finding biggest of three num
papers section.

You might also like