Professional Documents
Culture Documents
Yashvant Kanetkar
Yashvant Kanetkar
.) C
is simple
.) C
is fast
+ C is small
Historical Development of C
1960 International Committee
Mach~neOriented
, Ex. Assembly,
KlClT I
C I Lecture 1
In The Beqinnina
...
Alphabets, Dlglts
Words, Numbers
Constants. Variables,
Alphabets, Diaits,
KlClT I C i Lecture I
Character1
String
1I
Structure
Union
Enurn
etc.
lnteqer Constants
Rules:
/-
1. No decimal point.
ic
3. No comma or
spaces
4. Valid Range: -32768 to +32767
Real Constants
EX. 427.62
+24.297
-0.00254
Rules:
1. Must contain a decimal point.
2. May be +ve or -ve. Default: +ve
3. No comma or spaces.
4. Valid Range: - 3 . 4 ~t030 to +3.4 x
KlClT I C 1 Lecture 1
@&=J
many types?
As many as the types of constants.
HOW
7
Memory
y=4
z=x+y
print z
memory
KlClT I C I Lecture 1
1
I
--
Integer Constant
3.0
Real Constant
$3' ---t Character Constant
a -7
b - ?
c + ?
float b
b = 3.0
char c
c = '3'
$h
pt.3
@*tdbin$h t
@a,
Ex, pop98
si-int
sl-int
Usl-~nt
No commas or spaces
I-p
KICIT I C I Lecture 1
C Keywords
.)
How many?
What are Keywords?
I)
real b
11db
character c
~ h &c
char
float = 3.1
Where Uo We Stand?
P'
91
~ o n s t m t sVariables.
,
Keywords
51
Statements or Instructions
KlClT I C I Lecture I
float p, r, si
int n
p = 4000.50
n=3
r=15.5
sl=p*n*rll00
float p, r, sI
int n
p = 1000.50
n=3
r=15.5
sl=p'n+r1100
printf ( "%fl, si )
KlClT I C I Lecture I
*fim
Terminoloay Matters
()
Parentheses
{ ) Braces
[ 1 Brackets
General Form of ~ r i n t f (
printf ( "format string", list of variables )
'I
b
can
. contain
, <+
-
%i integer
4,L
i.?!
%c character
%f float
)
tlw
awd ulric,bb in
mdQ/r d dC Q I & & ~ -
I I amStatement Terminators
p,
a boy
I go to school
noat
int n
r, si
float p, r, si ;
int n ;
c
t
a
t
y
~t
erminato)
: colon
; semicolon
KlCIT I C I Lecture 1
9
Q
floatp,r,si; intn;
What I o Execute
,-main(
n=3:
r=15.5;
si=p+n'rIlOO:
printf "%fl,
si ) ;
maln( )
e{
float p, r, si ;
Int n ; .
p=IOOO.SO;n = 3 ; r = 1 5 . 5 ;
si=p*n'rllOO;
prlntf "OhP', si ) ;
I!)
KlClf I C I Lecture I
& &'
Multiline comments
......*I
n * r 1100 ; I*
r .............................
...............................
'I
Nested comments
A
I' Calculation of simple interest *I
main( )
{
float p, r, sl ; int n ;
prtntf ( "Enter values of p, n and r
scanf j "%f %i%t",
sl=p'n*r1100;
printf "%f", sl ) ;
) ,
KlClT I C I Lecture I
");
Fractional Form
Exponential Form
==a
-3
-.,
CI
4.2762:
E&T
-
->
Exponent
Mantissa
Character Constants
'3'
'm'
'.
'Nagpu-
I>cj
C Variables
Integer
Polnter
Character
Strlng
Structure
Unlon '
Enum
etc.
KlClT I C I Lecture I
.k Turbo C
k QuickC
* Microsoft C
j,
Artech C
Jr Zortech C
tattlce C
.rt Watcom C
3r GreenLeaf C
* Vitamin C
1
J
l ntesrated Develooment
Environment --cde~k?m
n
r*
4p
KlClT I C I Lecture 2
ommands
Editin
Cursor Movement
Daletlon
+ Up Arrow
4 Down Arrow
+ Rlght Arrow
+Len Arrow
Backspace
Ctrl+T
CtrltY
Home
End
PgUp
PgDn
b o b
*+ ir\.ln\ Some
~ hrub.
k
More Commands
~lsinhciid
-6'
;?$
Perm
* '
Save As
Exit
Tempora
.-]
CtrltFg Compile
AlttF5
OutputAIi +F3 CIosm
wlndow
RLt t X
e~i*
lnterchanclina Contents of
Two ~ariab1.s
maln( )
~ h e + h f m d+&SW
Int c.d:
. .
KICIT I C I Lecture 2
(9j
~P,D/
~wmh
,
f i b o b +Fvr~v
q 1i ~ h t
lnterchanaina Contents of
Two Variables
e
mar( )
Inlc,d,t;
printf ( "Enter values of c and d" 1 ;
rcsntl "XI%I",LC,&d ) ;
t-c;
~1~~
dwt:
Sum of Diaits
main( )
Int n ;
printf ( "Enter a flve dlnlt no." ) ;
.....0.
KIClT I C I Lecture 2
mmm
.-
BBLl
c
d
t
c-d;
Calculations
:
2691 - Division
I
- Modular division
2691 3 % 10 1
7
3
26913 1 10
I O ~ Z 6 ~ 1 3 / 2 6 9 ~
s =dl +dZ+d3+d4+d5;
printf ( "%I", s ) ;
0"-'
&*
i modulus i R e a v Useful
*
KiClT I C I Lecture 2
diw&bk by
Ior ib$.
<F
Statements or Indructlons
C Statements I Instructiong
1s same as
int a = 5 ;
*inta=S,b=lO, c=a+ba5Y*2:
Here order Is Important
KlClT I C I Lecture 2
Is This Ok?
It. tnt a, b, e, d ;
a=b=c=d=5;
&??I
C~3tatementsllnstructions
k Type Declaration Instructions
* Arithmetic Instructions
Ex.
s=dl+d2*d3;
si=j*n"r1100;
c=5.019'f-32;
+, -,', I , %
Arlthrnetic Operators
For exponentlatlon Use pow( )
KlClT I C I Lecture 2
Integer mode Al
* RmtmoQo Al
+ Mixad modo AJ
Ex. i n t m = 3 , b * 4 , c ;
c-agb+5%8-b+l4;
Onerandl
float
Omrand2
float
Tty This
Int a ;
a=512;
ao5.012; 4 Q
a=512.0; - 2
a = 5.0 12.0 ; +
ar215;
-0
a82.015; --*D
a 2 1 5.0 ; ---+ @
a = 2.0 I 5.0 ; L 0
KlClT I C I Lecture 2
Result
float
Try This
float a ;
float c, f
= 21 2.0 ;
KlClT I C I Lecture 2
r" G % P - @ b ~ - ihn t
vanicJda
C Instructions
Arithmetic lnstrudlonr
- Output
- Input
printi( )
prlntf ("format atrlr~g",llrt of varlmbl~s) ;
KlClT I C I Lecture 2
Numberina Svstems
Declrnal Octal Hsxadsclmal
(0.S)
(0.7)
0
I
(0.W.A-F)
0
Binary
(0.1)
0
10
1Wo
IW
1W
...
FF
100
....
Conversr on^
Oeel 473
4~1O2+7*10~+3*IOP~473
krrr 11 d e c l m a L l * ~ g ~ + l ~ l ~=~17
!
Bloaryl? - $ * 2 1 + 1 ' 2 0
\L*
\*a' + \ A % '
More Conversions
It=~ a i n d P / v .
KlClT I C I Lecture 2
=3
-.-
q0d = &JpJmd P ~ ~ v ~ v R L P ~
% O =& & q w l i v M
Xr=
gB y
\n Newllne
Irr \t -Tab
$r prlntf ( '%d\n%d\t%dt', 10,20,30 )
v Other Characters
KlClT I C I Lecture 2
- hkaq.bhd
p r ~ n t f l Makes
)
It Handy
$r
Rs. %f',
$1) ;
-3
C Instructions
KlClT I C I Lecture 2
* Control Instructions
Control Instructions
+
+
......
......
prlrltf (
);
scanf (
);
a = .... ;
b = ....;
prlntf (: .......);
:
CI Is the default CI I
I 1 ~ i pSequence
KlClT I C I Lecture 3
{
lnt qty ;
float prlce ;
printf ( "Enter quantity and prfce" ) ;
scanf ( "%d%f", &qty, &price ) ;
main( )
{
dis = 0 ;
Tips.
+ if, else Keywords
+ General form:
if ( condltlon )
statement1 ;
else
statement2 ;
+ Cond. is usuallv built uslng <,
a==-
KlClT I C I Lecture 3
(@
Would This Work?
Int a = 5, b = 4, c, d ;
Jprlntf
( "Xd", a + b ) ; /
J prlntf ( "%d", a r = b ) :&
J
/~=a'b;
j ' p r l n t f ( "Y,d", c j
Tip:
.
)
.
&
.----
Int a ;
printf ( "%d", a )
Tip: Unless specifleally initialised a
variable contains abarbage value.
main( )
{
1
Tip: else block is optional
KICIT 1 C I Lecture 3
( - -'
float bs, hra, ca, da, 0s ;
prlntl( "Entrr bask walary" ;
*cant ( "W',&bs ) ;
ca=bs*IZ1100;
else
If block
hn-br'lSI100;
ea 200 ;
ga bs + hta da +
printf ( "Grolr salary
balongs to
= Ra. sf',~r ) :
dam
6 )
else
'
Y )
dam
gs=bs+d*+hra+ca;
Rs Xf", gs )
KlClT I C I Lecture 3
Int y :
prlntf ( "Enter year" ) ;
scanf ( "%d", By J ;
Laap
Leap
rna\n( )
(
Nwted If-else
...
!f(y~.~oa--aj
lf(y%40Om-0)
prlntf ( "Lmap" 1 ;
11*1
69
olio
prlnff ( "Not Leap* ) ;
Int y ;
prlntf ( "Enter year" ) ;
.. .
KlClT I C I Lecture 3
;ax>
11111
dMonday
8t $11
<' Monday
151111
C~Manday
221 11 1
Monday
281 11 1
Mandry
...
...
...
i ~ i t i v ~C@
e
30
--
%-OAoo+'&
7 $7
't-C%-\))~oo 3
h.
~00iod
t
2000,
cy-Q&
main( )
{
normaldays = ( y I) '365 ;
,, ,,
1
-32768 to +32767
KlClT I C I Lecture 3
c? float a ;
;
float a ;
Soln: normaldays = ( y
Soln: normaldays = ( y 11 ) 365 ; 4
-Soln: long Int y :
six
O-A
u
(J
int i ;
long int j ;
prlntf ( "%d % d l sizeof ( I ), sizeof ( j ) ) ;
-L\w
WU~U bib
rwm9
w&&J
Keyword
3 %
-3 %
3 %
-3
4
4
4
-
L
4
2
3
4ea
int a = 5 , b = S , c = S ;
i((a==t]==c)
prlntf ( -Hello':)
KlClT I C I Lecture 3
e h
cOLei~ be.
-F\O
rn
'U Joe
WI
-ad ? v r n rkm'b
& !- p h d d J
b=30;
printf ( "%dm,b ) ;
nh.
int a = 10 ;
if ( a = 5 )
printf ( "Hi" ) ;
6
9
if(a=5)+
if(=)+
if(5)
main()
:--
rrse
I f ( per>= 5 0 )
prlntf ( "Second dlvlslon" ) ;
;Ise
if ( per >= 40 )
printf { "Third dlvislon" ) ;
else
printf { "Fall" ) :
1 )
KlClT I C I Lecture 4
sdo4em.d.
Lo&
Ak "d
main( J
I
Int rnl, m2,m3, m4. m5,per ;
prlntf ( "Enler marks In five mubjeets") ;
scant ( "%dY.d%dY*d%d", Lml, &m2, am3. bml, h m 5 ) ;
p#ra(mZ+mZ * m 3 + m 4 + m 5 ) / 5 ;
If(per*-W )
prlntf ( " F l n t dlvlslon" ) ;
If ( p a r > - 5 0 )
prlntf ( "Second dtvlslon" ) ;
prlnti ( "Thlrd dlvlslon" 1 :
prlntf ( "Fail" ) :
I----
*=
111m
prlntf ( "Fall" ) i
c;>
KlClT I C I Lecture 4
Is This
Loqical Operators
Useful in
'iP
C'
'
checking?
Ranges
;h
I Problem
gl...,m~,
else
printf ( "Not In
if ( age r 30 1
-pint! ( "lnsured" ) ;
else
..
1
printf ( "Not I n s u r e d )
;Ir
if age > 25 )
prinlf ( " l n s u r e d ) ;
Int age ;
chars, ms ;
prlntf ( "Enter age, sex, marital status"
scanf ( "%d%c%c", &a e, as, &ins )
if(Ims9.'m'lll(ms
11 [me= = 'u' &&
s = 'f
prlntf ( "Insured") ;
else
prlntf ( " ~ o itn s u r e d ) ;
KlClT I
)XI
C I Lecture 4
88 ape > 25 ))
QL
mh o m
ecltEhtw ~t b ~ b i &
11534 +3bt+k b b b e b .
False
,
True
False
6
False
False
True
False
True
True
False
False
False
hula3
1
I
0
0
OR
0
\
Arlthmellc
Arithmsllc
+c
=.
C-1
<= >=
Relational
[=
Exchancinq Blocks
rnaln( )
main( )
I
inta=3, b = 4 ;
inta=3,b=4;
if ( a < = b )
printf ( "A" ) ;
if(arb)
printf ( "B" ) ;
else
printf ( "A" ) ;
else
printf ( "0" j ;
1
Output:
KlClT I C I Lecture 4
Output:
main( )
main( )
ff(a<=b)
printf ( "A" ) ;
i f ( l a > b)
printf ( "A" ) :
else
else
:.
net&=
pr~ntf( "B" ) ;
printf ( "B" ) ;
output:
output:
:.ButptCt 0 .
anta=3,b=4;
lf(lIa>b))
printf ( " A ) ;
else
prinif ( "B" ) ;
1
output:
main( )
I
inta=3,bE4;
it(!(ac=b))
printf ( "0") ;
Inta=3, b = 4 ;
if ( a > b )
printt ("B");
else
else
printf ( "A"
1
Output:
1;
fl
KlClT / C I Lecture 4
printf ( "A" 1 ;
1
Output:
I
Int a
3, b
= - 4, c ,d ;
cwla;
dmlb;
printf ( "%d %d %d %d", c, d , a, b ) ;
main( )
Int a
= 3, b = 4, c :
e m : L ~ o l U e wdJ
main(
{
Int a = 3, b = 4, c ;
c=!all(b=7);
printf ( "%d %d", b, c ) ;
KlClT I C I Lecture 4
inta=3,b=4;
if(a<=b)
prlntf ( "A" ) ;
else
printf ( " B ) ;
Output:
..
..
HOW
would
If ( a < = b )
prlntf ( "A" ) ;
YOU
Convert
a c= b 7 printf ( " A ) ;
+I
KlClT I C I Lecture 4
I Terminator]
3 S0ln: a c= b 3 printf,( "A" ) : printf ( " " 1 ;
aabtgnmd
< - b ? p r i n l l [ l a ~ ) (: . = a ) ;
bnO
statement
main( )
Int a = 3, b ;
~1<~5?(b=IO):(b=20);
prlntf ( "%dl1,b ) ;
KlClT I C I Lecture 4
fl
int a = 1 0 , b = 2 0 ;
float c = 3.14, d = 6.28 ;
printf "%d %d %f %f ",a, b, c, d ) ;
print! ( "%d %d %f %d ", a, b, c, d ) ;
printf ( " " / ~%d
f %f %f ",a, b, c, d 1 ;
CIS^ 7 8 1 h D , Q O O / -i.g2?e ~
int i = 65, j = 90 ;
char ch = 'A', dh = 'Z';
printf ( "%d %d %c %c", i, j, ch, dh ) ;
int 1 = 65 ;
Re~resentation
float a = 3.14
char ch = 'A'
7 ii
I
B ~ n a r yEqv.
KlClT I
Binary Eqv.
C I Lecture 5
ASCII code
ASCII Codes
inary Is Difficult
.-
I
'
:
Character
Decimal
Of 0000D1
01000010
01000011
i E
"
01000100
01000~01
""
68
'
6i
..
int I
pr~ntf( "%cT7.i ) ; 4
printf ( " % d U , ch ) ;
KlClT I C I Lecture 5
14
+ 65
Characters to be Kenreranted
.-
+ 26
+2
+fO
+ 32
+ 34
Capital letters ( A- Z )
Srn.lh.*.
1.11.~ (
1)
cb
Digits ( 0 - 9 )
Spwlai Symbol8 ( +
1 . : ; etc. ]
Non-prhkblr cham control chars
Graphlc charactem
+ 128
256
00000000 To l l t l l l l l
9+ b b ,,kt h r n( e . ~b
l e
.)
,,A p m b a
ehcye
i
/ ~ ~ a n d l s ~ ~ s m
0 nd
md
l lsa l~0 m a n d l a
Int I = 35 ;
prinlf ( "Wf". I ) ;
i
0sandI1 I ~ s n n d t s l
KlClT I C I Lecture 5
Sur~rised?
rain'
'
float a = 0.7 ;
if ( a c 0.7 )
printf ( "A" ) ;
else
printf { "B" ) ;
=
04d=A .
Binary of A Float
KlClT I C I Lecture 5
CdlD c & )
-+K q c n u I )
mdm
---3
Communication
(n-oem
main( )
~ l s c&)
a
* ,b&
>calsurn(
I
)
<
Passinq Values
main( )
&w&@I
iM
lnt a, b, c, r ;
s=a+b+c:
prlntf ( "Kd", s ) ;
s,
Arguments
prlntf ( YO^", s ) ;
I
KlClT I C I Lecture 8
d
o w t z & h &ky)
=-
Returninq Values
r = calsum ( a, b, c ) ;
prinv ( "%dm,r )
t:
Int ss ;
ss=x+y*x;
return ( ss ) ;
return ( 80 ) ;
---=
3
Are These Calls OK?
calsum ( a, 25, d ) ;.
calsum ( ID
+ 2 , 2 5 x 3, d 1 ;
int ss :
u=x+y+z;
return ( 8s 1 ;
1
Nested calls are legal.
Call wlthln 417 expression Is legal.
Ivalue at a tlme
Int a = 1 0 1 b = 2 0 . c - 3 0 ;
int 8 , p :
3 s, p = sumprod { a, b, c ) ;
prlnn ( ll%d%dy*.r. p ) ;
sumprod ( Int
*, int y, Int x )
Int
IS,pp ;
ss=x+y+z;
pp'x'y'z:
return I ss,PP 1
;x
KlClT I C I Lecture 8
sqaw&
/&clmati~r
=\me
>
j 5 ( = . ~
Ye25
~ = d ,
y=n5%? r = d ,
) = vahQ
2-r
9c&~su~~'[~s,~~,Q)
'
main( )
{
10, b = 20. c 30 :
int s. p :
I sumprod ( a, b, c ) ;
p sumprod ( a, b, c ) ;
prlnn ( "%d~/.d",5, p ) ;
Int a
int ss,pp ;
::':':':.-
,.
return ( ss 1 ;
turn ( PP ;
durn0-b
hrr,
(~1;i); $g
c!mBPh
s sumprod ( a, b. c, I ) :
printf { "%d%dn, s, p ) ;
roturn ( IS ) ;
else
return ( pp ) ;
time
,=
,I
hw
~r)be
&-'d m
&t&.
II
A Better Wav
int IS,pp ;
pp-x'y'r;
return ( pp ;
KICIT I C I Lecture 8
no.
4 -J?4
1;
N ~ H O V O JS k m d ~ d
calsurn ( x, y, z )
Alvl~-tm
Kornighan B.
I Int x, y, z ;
I
)
'
Int y ;
prlntf ( "Enter year" ) ;
scanf ( "Y.d", &y ) ;
romanize ( y ) ;
}
romanlze ( Int yy J
int n. i ;
n n yy11000:
tor(I.1 ; I < = n : l + + )
prlntf ( "m" ) :
Int y ;
prlntf ( "Enter year" ) ;
scanf ( "%d", By ) ;
y = romanlse ( y, 1000, 'rn' ) ;
n=yylI;
lor(l=l ;Ic=n;I++)
prlnlf ( "%c", ch )
return(yy%]);
KlClT I C I Lecture 9
...
y=romanlro(y,50,r1w):
y=mrnsnlsa(y,~~,~x~)~-
%03
Int n, I ;
n-yytj ;
for(Ia1 ;I<mn;i++)
prlnff ( "xc",
ch ) ;
Advanced Features of
Functions
4 Returning a non-integer value
Recursion
main( )
float a, b, c ;%square
(float) ;
a=squan(2.0);
Function
b = square ( 2.5 ) ;
Prototype
c-squam ( 1 . 5 ) :
printl ( " Sf 161 W ", a, b, c, 1 :
2.0
2.5 \ - 5 + a * D&\aa
(bmd b
C-
d*OF r.34 2
9
s
flaat y :
yax-x;
bcl Jalnll~P- n
9,mn
\ ~ ! w ~ n ~ f i r
float y ;
Y RSqUaro [ a ) ;
prlntf ( "%1". y ) ;
Int a = 10 ;
prinn ( *ma = %don
" Ia ) ;
a=fO;
prlntl ( "\n a = % d , I1 ;
1
f(
9 d y
Int r
10 ;
printl ( k l \ ~ a = % d o . , a J'O
);
f0;
A \\3
Returned value is Ignored.
KlClT I C I Lecture 9
olution
main( )
Int a = l O ;
prlntf ( 'Ina = X d " ,a ) ;
f( 1 ;
prlntf ( " h a = % d " , a ) ;
+ Returning a non-integer v a l u e d
+ Call by Value I Call by Reference
+ Recursion
KICIT I C I Lecture 9
-x
/--
a m : ----
wad
LO&
variable, constant
"
. . .. .
-.
* ( a t ) ;V
)m$
1
prinni l * i n v a ~ u c o f i =YO^
7n
1 1
Jmal;-tadddish&hj.
-- ,
-r
'
-- , --. ,
-9 6 ~ 0 5119,
,
BY).hrnJ6o1o.
r---
vnlue
"j
"-'x.Lq
&s
variable
*&variable
-.
J
"
id *k;
KlClT I C I Lecture 9
J&
c*k=v&
of 4bolo
0xB8000000
whlle ( 1 )
I s = Ox88000000 ;
for ( I = O : ler1999; I + + )
{
( .(s+I)
Z=
'A'
*(s+L) a *(s+i) + 32 ;
else
(
. 'a' && *(*+I) cif *(s+l) =
*(s+g E *(s+/) 32 ;
KlClT I C I Lecture 9
'2' )
I-
I
1
KICIT I C I Lecture 10
?in
(1
v l I I J
+a
c = random ( 79 ) ;
mund
I-
, ..
Are Thev Same
char h r s
' ;
char far % ;
E]
for ( I = 1 ; I C* 3999 ; i += 2
*(s+l~-color;
KlClT l
C I Lecture 10
pr-;m
e Caps Lock Un
,
main( )
{
~oggle
Kmys
Capr lock.
Num lock
Scroll lock
Inwrt
kbrOx417 ;
v&
'~u
h et
on't Do Delete
chat far 'kb :
kbaOx417;
prtntl { "PI. press delete key");
*kb = l 2 ;
rthdav Josh1 7 6 5 4 3 2 1
<--
I
Our Program
Del-Ctrl
KlClT I C I Lecture 10
Alt + Del
['\a+
vim
D&
rtD &ah&.
h C M *+QJ
;-I
m'.
1-c.
m
W
[ 0
0
1
1
8
0
0
P
]
Lcm
prlnbl("%d'AdW',sltmof(I),sluof(a),rl~f(ch
2 1-1 I
1);
...
[~ontlnumd
dky+Sinm
]
4
0
0
0
1
w
1
prlnH('9hd %d%d",slxooi(J), slzoof( b ) , rlzoof(dh) ) ;
pr/ntf ( "%d w
KQ-2-
\1292,
c
main( )
int i * 25 ; int *j ;
float a = 3.34 ;float *b ;
char far "kb ;
kboOx417;
j-&i;
KlClT I C I Lecture 10
dteIadn)l&o &
@w
+bock
M
' E!&
Pointer Sizes
main( )
{
= &I;
= 8.;
Int far *m ;
m = 0x413 ;
KlClT I C I Lecture t 0
fi)'
he btomvdios
C B n w i -~- O r R % c ~ @ o o
C~~XINJ&Eh a UMUS
.--
?in(
Int a n l o , b = 2 0 ;
,o,~,o
pdntf ( "KdYod", a, b ) ;
/L C
rwapv(a, b 1 ;
10
prinn ( "%d%dn. a, b ) ;
&& &
1
J
110]150]110]
int t ;
\O
t * x ; x = y ; y - t ; TO
prlnti ( "%d%dW,x, y ) ;
pi-'pJB
pqwm
k.s=W
Q=\O
int a = I O , b = 2 0 ;
prlntf ( "~/ld%d",a, b ) ; 17
'
swapr ( &a, &b 1 ;
'v = t ;
KlClT I C I Lecture 10
n heLSQ be u pk. : .
char 'n[ ] = {
I
"Sanjay", "Amol",
"Slvaramkrishnan",
1:
Selection
..
1)
~O~(I=O;IC=~;I++)
printf ( "fds", n[ i ] ) ;
yarn(
Calendar
1/8/1~9-?
1HI1 Mon
...Calendar
Int
int 1, a ; inl firstday ;
..
totaldays
= normaldays t
leapdays ;
120;
for(i=O; 1<rm:2 ;i++)
s = s +days[ i 1 ;
totaldays += s :
flrstdey = totaldays X 7
i / i ~999
i to 311711999
...Lalendar
+,
inain( )
.. ..
tolaldaya = normaldays
+ leapdays ;
i f ( ( y % d D O = = o ) 11 ( y % i a o i = O & b y % 4 = = 0 )
days[l1=29:
---7
.-
s=o:
for(i=O: l < = m - 2 :I++)
s = s + days[ I ] ;
totaldays += s ;
tirstday = tolaldavs 'b 7 :
..
August 1999
2 0 4 - - 2 8 i --32- & 38- 4 44.c 50-(: 56
I
I
I
I
i
l
l
Mon--.T ue., Wed
C
....Calendar
I r-rharf =
'm~nfhsi
26
20
32
/ "January'. "February,, , , 1 :
totaldays += s :
firstday = totaldays % 7 :
col = 20 + flrslday ' 6 ;
clrscr( 1 ;
- --
- I, y ):
gotorc
...
KICIT I C I Lecture I7
...Calendar
,-
10
;lain(
26
20
- Mon
32
...
Iue Wed
Int days[ 1 = ( 31, 28, 31. 30,31. 30, 31, 31. 30,
..j
: I <=days[ m - 1 ] : I++ )
golorc ( row, c o l ) :
printf ( "Tbd". I )
col = col +- -6- :
11 ( col z 56 )
{
row++ ; cob = 20 :
1
1
c--.n u ) c-l'.tien':~!
(i&
~ ~ h y ~e y~
F
i11):
I
COY
...calendar]
)
;
d -
while ( 1 )
I
normaldays = ( y
-1
) ' 365L
Glendar
Next year
~ O I C
( 20,35
'
h
Kt%"C
.
1 % -
Prev. year
case 77 :
m++ ;
'\( -t+
...Calendar
)
I
intdays[]= {31,28,31,30,31,30,
31,31,30,31,30,31 ) :
scanf ( "%d%dn, &m, &y ) :
whlle ( 1 )
normaldays =
leapdays =
totaldays =
~ f ( y % 4 0 0 = = 0 ) l l ( y % l O O ! = O && y % d = = O ) )
days[ 1 ] = 29 :
else
days[ 1 ] = 28 ;
.. -
-I.-_
KICIT I C I Lecture 17
,,
n.&
Terminology
iame
1 1
e:2
Salary
4000.50
Handlrna
- Data
mein( )
I
char n [ ] = {'A', 'X', 'Y', 10') ;
int a[ ] = { 23, 27, 28 ) ;
float s[ 1 = { 4000.50, 5000.00, 6000.75 ) ;
int i ;
for( i = O ; i ~ = 2 ; I + + )
printf ( "Xc%d %f", n[ i 1, a[ i 1, s[ I ] ) ;
1;
float s ;
w \ ~he
n h v d d~timkp
KlClT / C I Lecture 18
~+bt;l
I jot&
& \co*e&
I+'*
Arrav of Structures
struct employee
I
1;
char n ;
int a ;
float s ;
struct employee e[ 1s {
rf
( 'A', 23,4000.50
{ 'X', 27, 5000.00 ) ,
{ 'Y', 28,6000.75 )
int i ;
I }
Terminology
Structure
namettag
char n ;
int a ;
A structure is
a collection of
dissimilar elements.
ructure elements are always stored In
memory locations.
1 1
struct employee e[ 3 j = {
1 1 1
.. }
KlClT I C I Lecture 18
Arrav of Structures
-struct employee e l I = ( ... 1 ;
char 'p ;
struct employee 'q :
struct employee ('r 113)
c . - to~
?~
array of structures
Ptr.
'
-7 I
3~
+b c k
struct employes @[ 3 I = I
.. ) :
-1
P'.
tr. to structure
* A&S ; ~
408
415
p=e ; q = e : r=e ;
p++ : q t t : I++:
printf ( "YOU",
printl ( "Xu",
p);
q);
wz
float s ;
s;
) e = { "Rahul", 25,4000.50
I
I
1, x, y
'
Copying 1
main( )
I
struct ernp
1
char n[20];
int a :
float s ;
e2.s = el .s ;
G i v i n g at
e3 = el ;-I-*
printl ( "%s %d %1", e3.11, e3.a, e3.s ) ;
KlClT / C / Lecture 18
'd
p b
(93 3 Jh.Jffu@.
QOoJ3
Copyinq Arravs
struct z
{
1;
int srr(lo] ;
truct address
; int
age ;
}d
{
1:
'
1
printf
KICIT I C I Lecture 18
ho\d
P a s s ~ n gStructures
struct book
char n[20] ; Int nop ; tloat p r :
siruet book b = ( "Baalc", 425, 135.00 ) ;
display1 ( struct b 6 k bb ).
/rr to
[
I
OA
~hf--hm+
9
struct corn c ;
struct corn add (struct corn, struct corn) ;
: 7-7
struct corn z ;
y:r ;
*xf y.1 ;
return z ;
2 r
(mainO
a en\Lng
Complex Nos.
float a [ ] = ( 2 . 5 , 1.3);
float b [ ] = ( 1 . 2 , 1 . 7 ) ;
float *c ,
float ' add ( float *, float * ) ;
c=add(a, b ) ;
printf ( "%f %f", ' c , ' ( c + 1 ) ) ;
b=&.
'
ons of Structures
+ Pooltlonlng Cursors
+ Recelulng aecll and scan codas
+ Dlsplaying characters
Prlntlng on prlnter
4
Mause Programmlng
Graphlcs Programmlng
All Disk Operstlons
etc.
Endlsk
II * Faster
KICIT I C I Lecture 18
Iplatter ( aluminium ) )
Parts of platter
of same capacity
KlCIT / C / Lecture 18
07)hi-
%a~
9 air q i a a\(
i+.
Fain{
char arr [ 512 ] ; lnt I ;
KlClT / C 1 Lecture 19
& B I J ~&\eb
J
~ & s P . F,hcr P c ~ i ar n f L i s ~ , b&
5
%S
i+
w
t
o Oie*
&fi0di*
,
~b~wad=
d & k. p I k ~ & ~
a]89.
GVctb
Boot Parameters
No-
Kb
1.2
Ub
1.44
Mb
512
512
512
720
2400 2880
9
2
0
35
2
0
18
2
0
360
,,gs
Descrlptlon
Jump lnstructlon
System 10
NO. of byteslsector
No, of seetorslcluster
No. of sectors In reserved area
No, of copies of FAT
. Max. no. of root dlr. entries
Total no. o l sectors
Medla descriptor
No. of sectorslFAT
No. of sectorsltrack
No, of sides
No. of hidden sectors
2
2
&ch
h
b cm h a
(3+ChA0,mam
w a 3 d ~ w h pwacedy
n
we- w ~ W n qdCf mtaei, ih B
struct boot
..
I i
-Im a r lump1
. .*.1 ;
I Descrlptlon
IT3
Jump lnstructlon
...~ o n( t
include "dos.h"
yain( )
struct boot
char jump[ 3 ] ;
char sysld[ 8 ] ;
int bps ;
char spc :
..
',
b'
ol8 did it
dl&&
8)-&4 f
+ Ach
&@no.
H ~ o cw.
In General
absread ( drive no., no. of sectors to read,
sector from where reading should beg~n,butrer ) ;
absread ( 0,1,0, &b ) ;
LW-
& no.
Typlcal Obtains
Values Values
t02
83190
lBM3.3
Bytedsector
08s Secforslclusler
Sectors In reserved area
512
1
....
20480
0
I
-24
2
224
1 '": 1 1
0
SO
Sides
-9
nldaen *actors
i~L
J C A
so b.~&
cl~wmw b@w2a--
Anti-Viral
# Include "dos.h"
main( )
l
printf ("insert infected disk, Press any key" )
getchl)
--
fl
l o +"d
M-Cm4*
eo08*&kab.
20 880
KIClT 1 C 1 Lecture 19
#include "d0s.h"
Better...
... 1 ;
r\
abswrlte ( 0,1, 0, a ) ;
prlntf ( "Another dlsk yln" ) ;
ch = getche( ) :
&
disp\% =kG_~l\l~
ih hamQ
4 bytes
Il Include "do8.h"
yaln(
rruct
1:
Printincl Directory
entry
char n[ 8 ] ; char ext[
char unused[ 17 ] ;
long Int size ;
: getch( ) ;
tor(j=O;j<=7;j++)
prlntf ( "%c", e[ 1 ].n[ j 1 ) ;
lor(j=O;j<=2;j++)
printt ( "%c0, e[ I ].ext[ j ] ) :
printf ( "Xld", e[ i ].size ) ;
y'iq %C
CIT C / Lecture 19
natdfr &WQ
&
OP