Escolar Documentos
Profissional Documentos
Cultura Documentos
.) C is simple
+ C is small
.) C is fast
+ C offers better interaction wlth
hardware
Historical Development of C
1960 International Committee
Mach~neOriented
, Ex. Assembly, Ex. Basic, Fortran,
Pascal,Cobot
KlClT I C I Lecture 1
I
In The Beqinnina ...
Alphabets, Dlglts Symbols Dlgltr, SP.
Alphabets,
Alphabets, Diaits,
-
Constants and Variables
KlClT I C i Lecture I
Character1 1 String i
Structure I
1I Union
Enurn
etc. ]
lnteqer Constants I
Rules: . /-
1. No decimal point. a ic
3. No comma or
spaces
4 * 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
V m
+ HOW many types? @&=J
As many as the types of constants.
Is it necessary to identify types?
7
Memory
y=4
z=x+y
print z
KlClT I C I Lecture 1
3.0
--
- Integer Constant
Real Constant
$3' ---t Character Constant
a -7
b - ?
c + ?
$hpt.3 bin$h t
@*td-
No commas or spaces
KICIT I C I Lecture 1
C Keywords
.) How many?
What are Keywords?
I) What are Reserved words?
real b 11db
character c ~ h &c
And How About This...
1 int f l o a e
float = 3 float = 3.1
char
Where Uo We Stand?
91 ~ o n s t m t sVariables.
, Keywords 1
51 Statements or Instructions
KlClT I C I Lecture I
The First C Proclram
float p, r, si
int n
p = 4000.50
n=3
r=15.5
sl=p*n*rll00
1 float p, r, sI
int n
*fim
p = 1000.50
n=3
r=15.5
sl=p'n+r1100
printf ( "%fl, si )
KlClT I C I Lecture I
Terminoloay Matters
() Parentheses
{ ) Braces
[ 1 Brackets
General Form of ~ r i n t f (
printf ( "format string", list of variables )
'I
b
. contain
can
, <+
,
-
-
%i integer
5 4,L i.?!
-
%c character
-
%f float
Ex, prlntf ( "%f %I %f %fl,p, n, r, si )
) awd ulric,bb in
tlw mdQ/r d dC Q I & & ~ -
I I amStatement Terminators
a boy p, noat r, si
-
I go to school int n
float p, r, si ;
int n ;
ctaty~t
erminato)
: colon ; semicolon
KlCIT I C I Lecture 1
Free borm Lanauaae
float p, r, si ;
int n ;
is same as
9
Q
floatp,r,si; intn;
What I o Execute
,-main(
n=3:
r=15.5;
si=p+n'rIlOO:
si ) ;
printf "%fl,
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
I' Calculation of slrnpltr Interest ' I
& &'
main( )
{
float p, r, si ;
Int n ;
p=1000.50:n=3;r=15.5;
si=p'n'rllOO;
printf ( ""/of', si ) ;
1
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 ) ;
1
KlClT I C I Lecture I
1 Forms of Real Constants 1
-0.00254 -
427.62 ----+ 4.2762E2
+24.295 -4 2.4295~1
Fractional Form
-2.54e.3
Exponential Form
==a
-3
-., CI
->
4.2762:
-
E&T
Mantissa Exponent
Character Constants
'm' '3'
Rule : A single character enclosed
within a pair of ' '.
Are Thev OK?
'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
1
* tattlce C
J
.rt Watcom C
3r GreenLeaf C
* Vitamin C
l ntesrated Develooment
Environment --cde~k?m
n
4p
KlClT I C I Lecture 2
Editin ommands
Cursor Movement Daletlon
+ Up Arrow Backspace
4 Down Arrow Ctrl+T
+ Rlght Arrow CtrltY
+Len Arrow
Home End Computers are fun
PgUp PgDn
~ h e + h f m d+&SW h ~P,D/
b o b (9j ~wmh ,
*+ ir\.ln\ Some
~ hrub.k
More Commands
-6' ~lsinhciid f i b o b +Fvr~v
q 1i ~ h t
;?$ Perm
Tempora
* '
Save As
Exit
RLt t X e~i*
Tlp: Use rneanlngful filenames. Ex. CHtPR1.C
1 lnterchanclina Contents of
Two ~ariab1.s
maln( )
. .
Int c.d:
I prtnti ( "Enter values of c and d" ) ; d I
KICIT I C I Lecture 2
lnterchanaina Contents of
Two Variables e d t
mar( )
I
BBLl
c d t
Inlc,d,t;
printf ( "Enter values of c and d" 1 ;
rcsntl "XI%I",LC,&d ) ; C d t
t-c;
c-d; ~1~~
dwt: c d t
1
prlntf ( "XI Xi", c. d ) ;
mmm
.-
One More Way
c=c+d;
d=c-d;
c=c.d;
prlnU( "XI XI". c, d ) :
Sum of Diaits
main( )
Int n ;
printf ( "Enter a flve dlnlt no." ) ;
.....-
0.
KIClT I C I Lecture 2
Calculations
26913 1 10 : 2691 - Division I
2691 3 % 10 1
7 3 - Modular division
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 1
* Leap year or not
* Odd 1 Even
Prime or not -s diw&bk by
Ior ib$.
KiClT I C I Lecture 2
Where Are We ....
<FStatements or Indructlons
C Statements I Instructiong
* Type Declaratlon lnstructlon
Ex. Int i, 1, k :
float a, b, c ;
char ch ;
2
a-5;
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
T V Dof~ Arithmetic lnstruction~
+ Integer mode Al
* RmtmoQo Al
+ Mixad modo AJ
Ex. i n t m = 3 , b * 4 , c ;
c-agb+5%8-b+l4;
Tty This
Int a ;
a=512;
ao5.012; 4 Q
a=512.0; - 2
a = 5.0 12.0 ; + 2
ar215; -0
a82.015; --*D
a 2 1 5.0 ; ---+ @
a = 2.0 I 5.0 ; L 0
KlClT I C I Lecture 2
Try This
float a ;
r" G % P - @ b ~ - ihn t vanicJda
float c, f = 21 2.0 ;
KlClT I C I Lecture 2
C Instructions
* Type Declaration Instructions
* Arithmetic lnstrudlonr
* Input I Output Instructions
printf() - Output
scanf( ) - Input
printi( )
prlntf ("format atrlr~g",llrt of varlmbl~s) ;
a
KlClT I C I Lecture 2
Numberina Svstems
Declrnal Octal Hsxadsclmal Binary
(0.S) (0.7) (0.W.A-F) (0.1)
0 0 0 0
t I I 1
10
1W IW 1Wo
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 . =3
\L* \*a' + \ A % ' q
-.-
More Conversions
It=~ a i n d P / v .
KlClT I C I Lecture 2
q0d = &JpJmd P ~ ~ v ~ v R L P ~
% O =& & q w l i v M
Xr= ~ ~ ~ ~ ~ e d t u r u
gB y - hkaq.bhd
p r ~ n t f l Makes
) It Handy
$r -
\n Newllne
Irr \t -Tab
$r prlntf ( '%d\n%d\t%dt', 10,20,30 ) :
v Other Characters
-3 -
* prlntf ( ''Enter valuer of c and d" ) ;
\
It prlntf ( "Hello" ) ;
g p r l n " ( "\nSlmple Interest
rn n
- Rs. %f',
$1) ;
KlClT I C I Lecture 2
I .*scanf ( "format string", list of variables j ;
* scanf ( "%d %f %c", &l,&a, bch ) ;
C Instructions
KlClT I C I Lecture 2
+ Type Declaratlon lnstructlons
+ Arithmetic lnstructlons
+ Input 1 Output Instructions
* Control Instructions
Control Instructions
+ What are they?
-Control the sequence of execution
of lnstructtons
+ What different types?
-
Sequence
-
Decision
-
~e~etition(hp>
-
Case
KlClT I C I Lecture 3
I*Calculatlon of total expenses *I
main( )
{
lnt qty ;
float prlce ;
printf ( "Enter quantity and prfce" ) ;
scanf ( "%d%f", &qty, &price ) ;
dis = 0 ;
-
totexp = qty ' price qty * price ' dis I100 ;
printf ( "Total expenses = Rs %f", totexp 1 ;
1
Tips.
+ if, else Keywords
+ General form:
if ( condltlon )
statement1 ;
else
statement2 ;
+ Cond. is usuallv built uslng <, >, c=, >=, = =, I=
a==-
2
KlClT I C I Lecture 3
(@
Would This Work?
J Int a = 5, b = 4, c, d ;
Jprlntf ( "Xd", a + b ) ; /
J prlntf ( "%d", a r = b ) :&
/~=a'b;
-
-
&. .)
.----
j ' p r l n t f ( "Y,d", c j :
Int a ;
printf ( "%d", a )
KICIT 1 C I Lecture 3
( - -'
float bs, hra, ca, da, 0s ;
prlntl( "Entrr bask walary" ;
*cant ( "W',&bs ) ;
ca=bs*IZ1100; If block
else
-
hn-br'lSI100;
ea 200 ;
ga bs + hta da +
printf ( "Grolr salary
balongs to
= Ra. sf',~r ) :
KlClT I C I Lecture 3
w Leap Year or Nof
Int y :
prlntf ( "Enter year" ) ;
scanf ( "%d", By J ;
Laap Leap
olio -
prlnff ( "Not Leap* ) ;
1
1
KlClT I C I Lecture 3
11111 dMonday
8t $11 <' Monday
151111 C~Manday
221 11 1 Monday
281 11 1 Mandry
...
...
... 1111 1 to3111Zl 1887
i ~ i t i v ~C@
e
If ( firstday 0)
prlntf ( "Monday" ) ;
main( )
{
tnt y, firstday, leapdays ;
Long Int normaldays, totaldays ;
,, ,,
-
normaldays = ( y I) '365 ;
,, ,,
1
-32768 to +32767
long int -2147483648 to +2147483647
KlClT I C I Lecture 3
What's Wrona Here'r I
normaldays = ( y I) ' 365 ; -
;float a ; c? float a ;
Soln: normaldays = ( y -
Soln: normaldays = ( y 11 ) 365 ; 4 -
-Soln: long Int y :
Keyword
, 3 %
-3 u
4
-
4ea
int a = 5 , b = S , c = S ;
i((a==t]==c)
prlntf ( -Hello':) ;
KlClT I C I Lecture 3
; e h cOLei~ be. .
I What Will Be The O u t ~ u t
-F\O rn 'U Joe
WI sdo4em.d.
-ad ? v r n rkm'b Lo& Ak "d
b=30;
printf ( "%dm,b ) ;
& !- p h d d J
I nh.
6
9 if(a=5)+
printf ( "Hi" ) ;
if(=)+ if(5)
main()
Int m l , m2,m3, m4,rn5 ; lnt per ;
prlntf ( "Enter marks In five subjects" ) ;
:-- scanf ( "\n%dY.d%d%d%dn, &ml, &m2, &m3, &m4, &mS )
per=(ml +m2tm3+m4+m5)15;
lf(per>=60)
prlntf ( "Flrst divislon" ) ;
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
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" ) ;
prlntf ( "Fail" ) :
c;>
(
Int m i , m2,rn3, m4, m5,per ;
printf ( "Enter marks in five cuh~ods"
);
runt ( "Xd%d%d%d%d",&mi,&m2,am3, &ml,LmS ) ;
per=(ml+mZ +m3+rn4+m5)/5;
if ( per >= 60F i )r s t d~vlslon"1 ;
prhtf
Is This
if ( per >= 50 a& per c 60 )
printf ( "Second division" ) ;
I f ( per - 4 0 L& per c 5 0 )
prlntf "Thlrd dlvlslon" ) :
If ( per r 40 )
printf ( "Fall" ) ;
1
KlClT I C I Lecture 4
Loqical Operators
Useful in
;h
' checking?
j Ranges / I Problem
Int age ;
chars, ms ;
prlntf ( "Enter age, sex, marital status"
scanf ( "%d%c%c", &a e, as, &ins )
)XI
if(Ims9.'m'lll(ms
11 [me= = 'u' &&
B
s = 'f 88 ape > 25 )) mh o m
QL
prlntf ( "Insured") ; Q
else
prlntf ( " ~ o itn s u r e d ) ; ecltEhtw ~t b ~ b i &
11534 +3bt+k b b b e b .
KlClT I C I Lecture 4
Workinq of && And U
Arlthmellc
+- Arithmsllc
c T <= >= Relational
=. [=
Exchancinq Blocks
rnaln( ) main( )
I
inta=3, b = 4 ; inta=3,b=4;
if ( a < = b ) if(arb)
printf ( "A" ) ; printf ( "B" ) ;
else else
printf ( "0" j ; printf ( "A" ) ;
1 1
Output: A Output: A
KlClT I C I Lecture 4
main( ) main( )
ff(a<=b) i f ( l a > b)
printf ( "A" ) ; printf ( "A" ) :
else else
pr~ntf( "B" ) ;
:. net&= p
printf ( "B" ) ;
I 1
output: A output:
:.ButptCt 0 .
anta=3,b=4;
lf(lIa>b))
printf ( " A ) ;
else
prinif ( "B" ) ;
1
output: A
KlClT / C I Lecture 4
What Would be The O u t ~ u t
main( )
I
Int a
cwla;
- 3, b = - 4, c ,d ;
dmlb;
printf ( "%d %d %d %d", c, d , a, b ) ;
main( )
Int a = 3, b = 4, c :
I 1
printf ( "%d %d", b, 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 ) ;
1
KlClT I C I Lecture 4
inta=3,b=4;
Yet Another Way I
if(a<=b)
prlntf ( "A" ) ;
else Output: A
printf ( " B ) ;
1
..
..
a *r b 7 prlnff ( "A" ) : pflntf ( "6" 1 ; -
If ( a < = b )
prlntf ( "A" ) ; I +I
a c= b 7 printf ( " A ) ;
{
inta=3, b = 4 ;
a <= b 7 printf ( "A" ) :
1
KlClT I C I Lecture 4
a <= b 7 printf ( " A " ) : '.
1
I Terminator]
3 S0ln: a c= b 3 printf,( "A" ) : printf ( " " 1 ;
-
< - b ? p r i n l l [ l a ~ ) (: . = a ) ;
aabtgnmd
bnO fl
main( )
Int a = 3, b ;
~1<~5?(b=IO):(b=20);
prlntf ( "%dl1,b ) ;
1
KlClT I C I Lecture 4
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 ) ;
f %f %f ",a, b, c, d 1 ;
printf ( " " / ~%d CIS^ 7 8 1 h D , Q O O / -i.g2?e ~ 5
Re~resentation
int 1 = 65 ; float a = 3.14 char ch = 'A'
7 ii
I
KlClT I C I Lecture 5
ASCII Codes
I .-
inary Is Difficult
I Character Decimal
I
'
A
B
Of 0000D1
01000010
C 01000011
: D 01000100 68
i E I 01000~01 ' 6i
I
I
I " I "" I ..
pr~ntf( "%cT7.i ) ; 4 14
printf ( " % d U , ch ) ; + 65
KlClT I C I Lecture 5
I Characters to be Kenreranted I
+ 26
+2
+fO
Capital letters ( A- Z )
Srn.lh.*. 1.11.~ (
Digits ( 0 - 9 )
1).- cb
+ 32 Spwlai Symbol8 ( + -
1 . : ; etc. ]
+ 34 -
Non-prhkblr cham control chars
+ 128 Graphlc charactem
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
Int I = 35 ;
prinlf ( "Wf". I ) ;
i
0sandI1 I ~ s n n d t s l I
KlClT I C I Lecture 5
Sur~rised?
'
rain' float a = 0.7 ;
if ( a c 0.7 )
printf ( "A" ) ;
else
printf { "B" ) ;
1
= 04d=A .
Binary of A Float
KlClT I C I Lecture 5
Local vls S l U vls 1SU Calls
i
printf ( "in I am In main" 1 ;
Passinq Values
main( )
I
1
calsum ( int x, Int y, int z )
(
int s, Arguments
I I
prlntf ( YO^", s ) ;
KlClT I C I Lecture 8
d
ao w t z & h &ky)
=-
Returninq Values
r = calsum ( a, b, c ) ;
prinv ( "%dm,r ) 9
calsum ( lnt x, Int y, Int z )
t:
Int ss ;
ss=x+y*x;
return ( ss ) ; return ( 80 ) ;
---=
3 '
Are These Calls OK?
calsum ( a, 25, d ) ;. j 5 ( = . ~ Ye25 ~ = d ,
calsum ( ID + 2 , 2 5 x 3, d 1 ; =\me y=n5%? r = d ,
calsum ( a, calsum ( 25, 10,4 ), d ) ; . 8
> ) = vahQ 2-r
calsum ( int x, int y. int z )
1 9c&~su~~'[~s,~~,Q)
I
int ss :
u=x+y+z;
return ( 8s 1 ;
1
Nested calls are legal.
Call wlthln 417 expression Is legal.
KlClT I C I Lecture 8
main( ) One More Try
{
Int a - -
10, b = 20. c 30 :
-
int s. p :
I sumprod ( a, b, c ) ;
p sumprod ( a, b, c ) ;
prlnn ( "%d~/.d",5, p ) ;
1
rumptod ( lnt x, Int y, lnt z )
{
int ss,pp ;
)
::':':':.-
return ( ss 1 ;
turn ( PP ;
,. 4 hrr,
(~1;i); $g durn0-b
c!mBPh
s sumprod ( a, b. c, I ) :
printf { "%d%dn, s, p ) ; n
1
sumprod ( Int w, Int y, Int z. Int code )
{
4 -J?4
roturn ( IS ) ;
else time
,I
return ( pp ) ;
,= % i d \ q no.
&-'d m hw
~r)be &t&.
1
II A Better Wav
;urnprod ( lnt a, int y, Int z, Int code) -
I
int IS,pp ;
pp-x'y'r;
return ( pp ;
KICIT I C I Lecture 8
NSI VIS K & R N ~ H O V O JS k m d ~ d
Alvl~-tm
I 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" ) :
1
KlClT I C I Lecture 9
...
y = romanlse ( y, 1000, 'm' ) ; ,-2
y = romanlse ( y, 500. 'dl ) ; _3
,
y = romanlse ( y, 1W. 'c' ) ,;
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
+ Call by value I Call by reference
+ Recursion
C 2.5 \ - 5 + a * D&\aa
C-
main( ) 2.0
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 : (bmd b d*OF r.34 2
9
s
oat square ( float x )
flaat y :
I yax-x; bcl Jalnll~P- n 9,mn \ ~ ! w ~ n ~ f i r
float y ;
Y RSqUaro [ a ) ;
prlntf ( "%1". y ) ;
Int r 10 ;
printl ( k l \ ~ a = % d o . , a J'O
);
A \\3
f0;
prlntf ("\n am Y.d", a ) :
KlClT I C I Lecture 9
olution
main( )
I
Int a = l O ;
prlntf ( 'Ina = X d " ,a ) ;
f( 1 ;
prlntf ( " h a = % d " , a ) ;
KICIT I C I Lecture 9
Would lhls Work ...
Int I = 10 :
j=&i;. /--
j= a m : ----
jEg(i434); wad LO&
prlntf ( "%do, 'j ) ;
variable, constant
"
I prinni l * i n v a ~ u c o f i =YO^
Jmal;-tadddish&hj.
7n * ( a t ) ;V
)m$
~ H n f f ( " h a d d r e a r o f I = X d " . 8 1 : .,AAL -r
,
' I
-9 6 ~ 0 5119,
, BY).hrnJ6o1o.
"j vnlue &s ,
variable *&variable
-.
J - "
1
*(s+g E *(s+/) 32 ; -
1
1
1
KlClT I C I Lecture 9
I-
-
char tar 'r ;Int r ;
char far Y ; char sh ;
110x68000000 ;
Int c ;
-
for ( c = 0 ;e <= 79 ; c++ ) ur
(
ch = * ( 8 +c.2);
kr(r=l;ra24;r++)
I
1
1
KICIT I C I Lecture 10
- /
?in (1
/
Bells And Whistles
' ~raaooooooI I Screen I
char far 'v ;Lnt r, c ;
s = OrB800MHIO : v l I I J
+a 1
c = random ( 79 ) ;
mund
I-
, ..
Are Thev Same I
char h r s
' ;
char far % ;
char k r 'a :
for ( I = 1 ; I C* 3999 ; i += 2
*(s+l~-color;
KlClT l C I Lecture 10 2
pr-;-
m e Caps Lock Un
,
main( ) Kmys
~oggle
v& h et
'~u
on't Do Delete
I rthdav Josh1 -
7 6 5 4 3 2 1 0
['\a+ vim rtD &ah&.
I Our Program L
<-- Del-Ctrl Alt + Del
D& h C M *+QJ
KlClT I C I Lecture 10 3
what would be the o u t ~ u t
mln( 1
I 1000 2090 9000
lnti=10;flo8ta=3.~4:charch='z'; j b dh
Lnt 7, "k ; float 'b, "c ; char 'dh, "ah;-I
j=&i: b-&;dh=Lch; 4000 5000 M O O
k-&J;c=&b;eh-ldh; k c oh
pdnH ( " % d # d % d M j , b , d h l :
, c c z ~ rcc m'. m
W[ 0
01
18
00
P]
MO 1500 2500
prlntl
1-c. Lcm .Ad %d'Ad", 'k ,'c , 'ah ) :
prlnbl("%d'AdW',sltmof(I),sluof(a),rl~f(ch 1);
2 1-1 I
[~ontlnumd ...
]
400
01w1
KQ-2-
prlnH('9hd %d%d",slxooi(J), slzoof( b ) , rlzoof(dh) ) ;
pr/ntf ( "%d w w",sizeof ( k ), mhd ( c ), rlzed ( ah ) ) ;
c
main( )
I
int i * 25 ; int *j ;
float a = 3.34 ;float *b ;
char far "kb ;
kboOx417;
j-&i;
KlClT I C I Lecture 10
@w
+bock fi)' he btomvdios I
M
' E!& C B n w i -~- O r R % c ~ @ o o .
Pointer Sizes
main( )
{
Int far *m ;
m = 0x413 ;
KlClT I C I Lecture t 0
C~~XINJ&Eh a UMUS
.--
?in( 1
Int a n l o , b = 2 0 ; ,o,~,o
pdntf ( "KdYod", a, b ) ;
/L C I
rwapv(a, b 1 ; 10
1
J
prinn ( "%d%dn. a, b ) ;
&& &
x Y t
swapv ( Int x, lnt y )
I 110]150]110]
int t ; \O x Y t
t * x ; x = y ; y - t ; TO pi-'pJB
prlnti ( "%d%dW,x, y ) ;
X Y t
1
pqwm
Q=\O k.s=W
-
int a = I O , b = 2 0 ;
prlntf ( "~/ld%d",a, b ) ; 17
' 0
swapr ( &a, &b 1 ;
. n heLSQ be u pk. : .
1
swapr (lnt 'x, lnt 'y ) -
I
Int t ; X Y t
'v = t ;
1
I
KlClT I C I Lecture 10
I Selection I
char 'n[ ] = {
"Sanjay", "Amol",
"Slvaramkrishnan", ..
1:
int I, ] ; char 't ;
for(I=0;1<=3;l++)
I
1)
\
~O~(I=O;IC=~;I++)
printf ( "fds", n[ i ] ) ;
yarn( 1
Calendar 1/8/1~9-?
1HI1 Mon
I
Int m, y ; lnt leapdays ;
long In1 normatdays :
long int totaldays ;
prlntt ( "Enter month and year" ) ;
scant ( "%d%d", Lrn, & y )-;
-normaldays = ( y - 1 ) * 365 L ;
1--leapdays=(y-1)/4-(y-1)1100
+ (y-1)1400:
totaldays = normatdays + leapdays ;
...Calendar
Int
int 1, a ; inl firstday ;
..
totaldays = normaldays t
leapdays ;
- 120;
for(i=O; 1<rm:2 ;i++)
s = s +days[ i 1 ; i to 311711999
i / i ~999
totaldays += s :
flrstdey = totaldays X 7 :
...Lalendar
inain( ) 0 'I 1- +, c\ jYL .+- 1 rr
i n t d a y s [ ] - { 3 1 . 2 8 . ~ 1 , ~ 0 ~ ~ 1 , 3 0 ~ 3 ~ , 3 0 , 3 1 ,13; 0 , 3 1
.. ..
tolaldaya = normaldays + leapdays ;
i f ( ( y % d D O = = o ) 11 ( y % i a o i = O & b y % 4 = = 0 ) 1 3
days[l1=29: ---7
s=o: .-
for(i=O: l < = m - 2 :I++)
s = s + days[ I ] ;
totaldays += s ;
tirstday = tolaldavs 'b 7 :
..
1
C 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
J
....Calendar
I 20
I
26
l
32
l
-
firstday = totaldays % 7 :
col = 20 + flrslday ' 6 ; - -- -
clrscr( 1 ;
KICIT I C I Lecture I7 4
...Calendar ,-
20
I
26
I
32
I
10 - Mon Iue Wed ...
;lain( )
Int days[ 1 = ( 31, 28, 31. 30,31. 30, 31, 31. 30, ..j ;
I' print month year ' I
I' prjnt days 'I
rwr-12:
for I = 1 : I <=days[ m - 1 ] : I++ )
golorc ( row, c o l ) :
printf ( "Tbd". I ) :
col = col +- -6- : -
11 ( col z 56 )
1
{
1 3
row++ ; cob = 20 :
F
i11-
- ): c--.n u ) c-l'.tien':~!
COY ~ ~ h y ~e y~
(i&
d -
I ) ...calendar]
I idanf "%d%dH,am, & y
while ( 1 )
I
normaldays = ( y -1
;
) ' 365L :
Glendar Next year '\( -t+
( 20,35
~ O I C : h
printi ( "Rt-Next mth ..." ) ;
ch = getkey() ; Kt%"C
. *
t
'
switch ( ch )
case 77 :
1 % -
Prev. year -
m++ ;
. -
. - K ,, n.& ,
1
1
)
...Calendar
I
intdays[]= {31,28,31,30,31,30,
31,31,30,31,30,31 ) :
scanf ( "%d%dn, &m, &y ) :
whlle ( 1 )
i
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
Terminology
iame 1 1
e:2 Salary
4000.50
1
- Data
Handlrna
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 ;
1;
struct employee e l = { 'A', 23,4000.50 } ; w \ ~he & \co*e& I+'*
struct employee e2 = { 'X', 27, 5000.00 ) ;
struct empioyee e3 = { 'Y', 28,6000.75 ) ;
prlnti ( "%c %d %f", el .n, e l a, e l s ) ; -
printl ( "%c %d %fW, e2.n, e2.a, e2.s ) ;
printf ( "%c %d %f", e3.n, e3.a, e3.s ) ;
}
n h v d d~timkp I jot&
~+bt;l ,
KlClT / C I Lecture 18
Arrav of Structures
struct employee
I
char n ;
int a ;
float s ;
1;
struct employee e[ 1s {
( 'A', 23,4000.50 rf
{ 'X', 27, 5000.00 ) ,
{ 'Y', 28,6000.75 )
int i ;
for( i = O ; i < = 2 ; i++)
printf ( "%c %d %f", e[ i ].n,e[ i ].a, e[ i J.s)
I }
Terminology
Structure
namettag
char n ;
int a ;
I A structure is
dissimilar elements.
a collection of
KlClT I C I Lecture 18
Arrav of Structures
--
struct employee e l I = ( ... 1 ;
char 'p ; * A&S ; ~ tr. to structure P'.
struct employee 'q : c . - to~ ?~
array of structures
Ptr.
I
struct employee ('r 113)
(93 3 Jh.Jffu@.
'
-7 I-- -
3~
struct employes @[ 3 I = I+b c k.. ) : p b b QOoJ3
1
!A 123 400.50 X 27 500.00
--- 1 1 -1
401 408 415
p=e ; q = e : r=e ;
p++ : q t t : I++:
printf ( "YOU", p); wz
printl ( "Xu", q);
float s ;
I iiruct employee e = { "Rahul", 25,4000.50 1 ; I
I main( )
I
'
Copying 1
I struct ernp
1
char n[20];
I
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 ) ;
u
'd
KlClT / C / Lecture 18
Copyinq Arravs
int a1101 = { 3, 6, 5,... 1 ;
int bIl 0] ;
f o r ( i = O ; i < = 9 ; l++)
b[i]=a[i];
struct z
{ int srr(lo] ;
1;
struct z a = { 3, 6,5, .., ) ;
struct z b ;
b=a;
truct address
; int age ;
KICIT I C I Lecture 18
P a s s ~ n gStructures
struct book
char n[20] ; Int nop ; tloat p r :
siruet book b = ( "Baalc", 425, 135.00 ) ;
displayl ( b ) : showl ( &b>~-----------)
1 /--
display1 ( struct b 6 k bb ). -
printf ( "%s %d %I3',
bb.n, bb.nop, bD.pr ) ;
I
showl ( struct book 'bb)
/rr to b=&.
I
printf ( "'As %d %f", (' bb ).n, (' bb ).nap,(' bb ).pr )
printf ( "%s %d %f", bb -> n, bb -> nop, bb -r pr )
+
I
rstruct corn
Complex Nos. --U
9OA ~hf--hm+
[ : 7-7
struct corn z ;
2 r
+
+y:r ;
*xf y.1 ;
return z ;
a en\Lng ' -
I
I (mainO Complex Nos.
float a [ ] = ( 2 . 5 , 1.3);
I
float b [ ] = ( 1 . 2 , 1 . 7 ) ;
float *c ,
float ' add ( float *, float * ) ;
c=add(a, b ) ;
printf ( "%f %f", ' c , ' ( c + 1 ) ) ;
ons of Structures
+ Pooltlonlng Cursors
+ Recelulng aecll and scan codas
+ Dlsplaying characters
Prlntlng on prlnter
4 Mause Programmlng
Graphlcs Programmlng
4 All Disk Operstlons
4 etc.
II * Faster
KICIT I C I Lecture 18
Parts of platter
of same capacity
KlCIT / C / Lecture 18
07)hi-
9 air q i a a\(
#~nclude"dos.h"
Fain{
1
getch( ;
%a~
i+.
Readina Boot Sector
ebsread ( O , l , 0.arr ) ,
for( 1 = 0 ; 1 < 5 1 2 ; I++)
printi ( "%cV, err 1 I 1 1 ;
& B I J ~&\eb
%S i+
J
w o Oie*
t
-
~ & s P . F,hcr P c ~ i ar n f L i s ~ , b&
~b~wad=
&fi0di*
,
~
5
d & k. p I k ~ & ~
a]89. GVctb
KlClT / C 1 Lecture 19
Boot Parameters @ &ch ol8 did it
No- 360 1.2 1.44
Descrlptlon
,,gs Kb Ub Mb
Jump lnstructlon
System 10
NO. of byteslsector 2 512 512 512
No, of seetorslcluster
No. of sectors In reserved area 2 1 1 1
No, of copies of FAT
. Max. no. of root dlr. entries
Total no. o l sectors 720 2400 2880
Medla descriptor
No. of sectorslFAT
No. of sectorsltrack 2
2
9
7
35
9
18
h b cm h a
(3+ChA0,mam
8)-&4 f m d
No, of sides 2 2 2 2
No. of hidden sectors 2 0 0 0
w a 3 d ~ w h pwacedy
n
we- w ~ W n qdCf mtaei, ih B dl&&
struct boot
..
I i -Im a r lump1
. .*.1 ; 3
I Descrlptlon IT3
Jump lnstructlon
w
...~ o n( t include "dos.h"
yain( )
I struct boot
char jump[ 3 ] ;
I
char sysld[ 8 ] ;
int bps ;
char spc :
..
',
:
struct boot b ; lnt I ;
absread ( 0, 1,0, &b ) ;
lor(l=0;Is=2:1++)
prlnti ( " % X , b,jurnp[ I ] )
for(1=D;I<r7:1++)
prlnti ( "%cV, b.aysld[ I ] ) ;
printf ( "Bytadsector = %dl*,b.bps ) ;
-
I , . g r l n t l ( "Sectordcluster = %dmb.spc )
b'
:
p &@no. + Ach 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 ) ;
Typlcal Obtains
Values Values
€83190 t02
lBM3.3 ....
Bytedsector 512 20480
08s Secforslclusler 1 0
Sectors In reserved area I I
2 -24
1 '": 1 1
Maw. root dlr. entries 224 0
Total no. of sactars
Madta descrlator O 4 SO dl- boot & WuNb
Sides
nldaen *actors -9 w i~L J C A
so b.~&
cl~wmw b@w2a--
Anti-Viral
# Include "dos.h"
main( )
l
1
getchl) :
--
fl
4 bytes
Il Include "do8.h"
yaln( 1 Printincl Directory
rruct entry
char n[ 8 ] ; char ext[
char unused[ 17 ] ;
long Int size ;
1:
struct entry e [ 16 ] ; Int I, :
prlnll ( "Insert dlsk, Press any key" ) : getch( ) ;
absread ( 0 , I , 19, e ) ;
for(i=O:i<=l5;l++)
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 &