Você está na página 1de 227

Why C?

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

Words, Numbers Constants. Variables,

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

1 2. May be +vs or -ve. Default: +vs

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

+ An entity whose value can 1


change

.) A name given to a location in


I
memory

KlClT I C I Lecture 1
3.0
--
- Integer Constant
Real Constant
$3' ---t Character Constant

a -7
b - ?
c + ?

C's Wav of ldentifyinq


Variables
int a float b char c
a=3 b = 3.0 c = '3'

$hpt.3 bin$h t
@*td-

Rules for Buildinq


Variable Names
First character must be an alphabet, rest
@a,
can be . @ h a t s , or underscores.

Ex, pop98 sl-int si-int


Usl-~nt
r
a m
Length <= 8 (Us ally)

No commas or spaces

Variable names are I-p


Ex. abc ABC Abc aBc AbC

KICIT I C I Lecture 1
C Keywords
.) How many?
What are Keywords?
I) What are Reserved words?

Would This Work?


integer a El ink '

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?

P' Alphabets, Digits, Sp. Symbols

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

Tip: All variables must be declared.

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

Any number of comments anywhere


I' formula ' 1 si = p • ......*I
n * r 1100 ; I*
Multiline comments -
r .............................
...............................
'I
Nested comments -
r ..............I* .......' I ........' I

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

r* Editor and Com~iler


Edltor - Helps In typing I edltlng of a
program

* Compiler - Converts C language


program to machine
language program

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

' Do* Shell


AlttF5
.-]
CtrltFg Compile
Output-

AIi +F3 CIosm


wlndow

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 ~

The Whole Picture


main( ) n

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

Alphabets, Digits, Sp.Symbols

1Constants, Variables, Keyword& I J

<FStatements or Indructlons

C Statements I Instructiong
* Type Declaratlon lnstructlon
Ex. Int i, 1, k :
float a, b, c ;
char ch ;

Tvae Declaration, A few


Subtleties
Int a ;

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( )

Arithmetic Instructions. A Few


Small Issues

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;

Leaal Arithmetic Operations

1 Onerandl I Omrand2 1 Result I

float float float

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

General Form of prinffLS I


prlntf ( "lormat string", list of rarlabler ) ;
1
prlntf I "Enter values of c and d" ) ;
List d varlab4es is optional.

printf ( "%I %i %i",a, 3 9 2 + 8 % 3 ) ; -/


4 ~l~tcancontalnvarlables,cons~nts
or expressions.

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

int .....; float ...... ;


prlrltf ( ......);
scanf ( ......);
a = .... ;
b = ....;
prlntf (: .......);
1
I 1 ~ i pSequence
: CI Is the default CI I

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 ) ;

I' Calculatlon of total expenses ' I


main( )
{
lnt qty ; float price ; Int dis ; float iotexp ;

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 :

Tip: Condition -True - Replaced by 1


- -
Condition False Replaced by 0

Int a ;
printf ( "%d", a )

Tip: Unless specifleally initialised a


variable contains abarbage value.

r Calculation of Total Expenses *I


main( )
{
Int qty ; float price ; int dls = 0 ; float totexp ;

If qty >= 1000)


dis= 1 0 ;
-
totexp = qty " price qty price * dls 1 100 ;
prlntf ( "Total expenses = Rs %f",totexp ) ;
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; If block
else

-
hn-br'lSI100;
ea 200 ;
ga bs + hta da +
printf ( "Grolr salary
balongs to

= Ra. sf',~r ) :

k t bm. hta, ca, da, gs ;


pdntli "Entar bmlc salary" ) :
mcanf (*%r',&bs ) :

- dam ...;bra= ...; c a m . ..;


6 )
else
a
Y )
' dam ... ;bra- ... ; c # - , , , ;
gs=bs+d*+hra+ca;
prlntl( "Grorr firlary Rs Xf", gs ) :
1

One More Form


If ( condllbn )
I

\ ITlp: - Dafault scopa of If and o I m Ia'only


the next strtmrnent after tham.
-To ovorrlde *I#dotaust scope use { )

KlClT I C I Lecture 3
w Leap Year or Nof

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
prlntt ( "Not l e a p "

olio -
prlnff ( "Not Leap* ) ;
1
1

69 First Day of Anv Year


main( 1
(
Int y ;
prlntf ( "Enter year" ) ;
scanf ( "%d", &y ) ;ax>
.. .

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

scanf ( "Xd". &y ) :


normaldays = ( y 1 ) 365 ; 30
leapd~ys-(y-1)14-(y-1)/100+(y-1)/400;
totaldays = normaldays + leapdayr
4 %-OAoo+'& ~00iod h.
--
t a
firstday * toUldays % 7 : a

If ( firstday 0)
prlntf ( "Monday" ) ;

7 $7 't-C%-\))~oo 3 2000, cy-Q&


c w d i n q T-otd no. qj d&,

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 :

The sizeoft ) O~erator


-l
4
int i ;
six
u
O-A
(J -L\w WU~U bib rwm9
long int j ; w&&J
prlntf ( "%d % d l sizeof ( I ), sizeof ( j ) ) ;

Keyword

Are You Sure?


4 % 3 - r n ip: Sign of remainder Is
3 % 4 L 2 me as slgn of numerator
-3 % 4 4 3

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

What Will Be The O u t ~ u t


main( ) Tip : Truth In C is nonzero,
I whereas,falslty Is zero.
int a = 10 ;
if ( a = 5 )

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" ) ;

prlnti ( "Thlrd dlvlslon" 1 :

prlntf ( "Fail" ) :

I---- Inlrnl. m2, rn3, m4, m5, per ;


prlnti ("Enter mark* In five subjects'' ) :
scanf ( "*hdXd%d%dY.d, L m l , Bm2, Lm3. &m4. am5 ) ;
prr=(ml tml * m 3 + m 4 * m 5 ) / 5 ;
If (per >= 60 )
prlnll( "Flrwt dlvlrlon" ) ;
*=
l l ( per 50
printt ( "Second dlvlaion" ) ;
If ( pat ?= 40 && per < 50 )
prlntf 1 "Thlrd dlvlrion" ) ;
111m
prlntf ( "Fall" ) i
1

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

prlnn ( "Enter age. sex. marital status" ) ;


scanf ( "%d%t%c", Bags, as,
f ( ms = = ' m ' )
prlntt ( "Insured ) ;
'iP
C' L gl...,m~, else
printf ( "Not In
if ( age r 30 1
-pint! ( "lnsured" ) ;
else
..

1 1 printf ( "Not I n s u r e d ) ;Ir I


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

False False False False


, True False , False True
i False I True False True
6 hula3 OR C-1
0 0 0 0
0 1 0 \
1 0 0 I
I I I

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

Yet Another Way


main( ) main( )
I
Inta=3, b = 4 ; inta=3,bE4;
it(!(ac=b))
if ( a > b )
printf ( "0") ;
printt ("B");
else
else
printf ( "A" 1 ;
printf ( "A" 1;
1 1
Output: fl Output: 4

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

HOW would YOU Convert I

If ( a < = b )
prlntf ( "A" ) ; I +I
a c= b 7 printf ( " A ) ;

Remove The Error

{
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

+ Use one, use another


4 Dummy statement

main( )

Int a = 3, b ;
~1<~5?(b=IO):(b=20);
prlntf ( "%dl1,b ) ;
1

r prlntf ( "%do',a <= 5 7 10 : 20 ) ;


0

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

Conversions Continued ...


main( )
{
int i = 65, j = 90 ;
char ch = 'A', dh = 'Z';
printf ( "%d %d %c %c", i, j, ch, dh ) ;
printf ( "%c %c", i, j ) ;
printf ( "%d %d", ch, dh ) ;
1

Re~resentation
int 1 = 65 ; float a = 3.14 char ch = 'A'

7 ii
I

B ~ n a r yEqv. Binary Eqv. ASCII code

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

Methods Are Different


= 65 ;
int I char ch = 'A' ;

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

What About More Than 255 1

Whv Unreliable Conversions


float I = 3.14 ;
printf ( "%em,i ) ;
prlntf ( "Kd", i ) ;
i
/ ~ ~ a n d l s ~ ~ s m
0 nd
mdl 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 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 .

I Aanearances Are Misleadinq I


tloat a = 0.7 ;
printf ( " X d K d , slzeof ( a ), stteof ( 0,7 ) ) ;
1

Binary of A Float

Truncated after 64-blt

KlClT I C I Lecture 5
Local vls S l U vls 1SU Calls

i
printf ( "in I am In main" 1 ;

-+K q c n u I ) CdlD c & )


---3 mdm ~ l s c&)
a
(n-oem <
Communication
main( )
(
Int a = 10, b = 20, c = 30 ; Int s ;
calsum ( ) ;
* ,b& L I W ~ ~
printf ( "%dU, s ) :
>calsurn(
I )
iM
&w&@I
lnt a, b, c, r ; v
s=a+b+c:
prlntf ( "Kd", s ) ;

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.

Returninq More Than 1 Value


main( )
Ivalue at a tlme
I
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 ) ; sqaw&
1
sumprod ( Int
I
*, int y, Int x ) /&clmati~r
Int IS,pp ;
ss=x+y+z;
pp'x'y'z:
return I ss,PP 1 ;x

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

The Only Wav Out


maln( )

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

1; calsum ( int x, int y, int r )


,. - q l
I calsurn ( x, y, z )
Kornighan B.
I Int x, y, z ;

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

A More General Gall


maln( )
I
Int y ;
prlntf ( "Enter year" ) ;
scanf ( "%d", By ) ;
y = romanlse ( y, 1000, 'rn' ) ;
1
romanire ( lnt yy, lnt j , char ch)
(
int n, i ;
n=yylI;
lor(l=l ;Ic=n;I++)
prlnlf ( "%c", ch ) :
return(yy%]);
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 ) ;

float square ( float x )

What Would Be The Output


mrln( )
(
Int a = 10 ;
prinn ( *ma = %don .
" Ia ) ;
a=fO;
-
1
f( J
prlntl ( "\n a = % d , I1 ;
9 d y
{
printf ( "Hello I" ) ;
1

Int r 10 ;
printl ( k l \ ~ a = % d o . , a J'O
);

A \\3
f0;
prlntf ("\n am Y.d", a ) :

Returned value is Ignored.


prlntf ( "Hello I" ) ;

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 ) ;

Advanced Features of functions


+ Returning a non-integer v a l u e d
+ Call by Value I Call by Reference -x
+ Recursion

prlntf ( " \n address of 1 a %d" , &I ) ;


printf ("ktvalueof I = ' A d " , ' ( & I ) ) ;

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

"

lnt I 10 ; Int 'j ; Int '*k ; 4 B BOtO 9 1 2


prlntf ( " \ n v 3 l u a o f I = % d " . 1 1 : v n o~ ~
p 1..
. . .. . -.
1 1 1

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

- . -- , , -- , --. , - r--- "-'x.Lq a

-9 6 ~ 0 5119,
, BY).hrnJ6o1o.
"j vnlue &s ,

variable *&variable

-.
J - "

KlClT I C I Lecture 9 id *k; c*k=v& of 4bolo


b &clk iht L C L ~ ~;SC ~
urn9 -
J& C*k j * k ~ v ~ d h @A~ am @
'lea.
0xB8000000
whlle ( 1 )
I s = Ox88000000 ;
for ( I = O : ler1999; I + + )
{
( .(s+I) Z= 'A' a& *(s+I) u 'zl )
*(s+L) a *(s+i) + 32 ;
else
(
if *(s+l) =
. 'a' && *(*+I) c- '2' )

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 Gr 'a, "v ; E] char far *s,far % ;


7 -

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

{ char far *kb ; Capr lock.


Num lock
kbrOx417 ; Scroll lock
Inwrt

How would you put off

v& h et
'~u

on't Do Delete

chat far 'kb :


kbaOx417;
prtntl { "PI. press delete key");
*kb = l 2 ; -

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

dky+Sinm dteIadn)l&o &

]
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 ) ) ;

prlnff ( '%d %f %en ,"k, -c, "oh } ; \1292,

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( )
{

float 8 3.14 ; noat *b ;


char h r *kb ;
kb = Ox417 ;
J = &I;
b = 8.;

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

;urnprod ( Int x, Int y , Int z,int 'as, Int 'pp )


{
..rrx+v+z:l a b P

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

I r-rharf = 'm~nfhsi / "January'. "February,, , , 1 :


I
totaldays += s :

-
firstday = totaldays % 7 :
col = 20 + flrslday ' 6 ; - -- -
clrscr( 1 ;

rintt ( "96s %d", months[ m 1 - I, y ):


gotorc I D , 20 ) ; ...

printl ( "Mon Tue Wed Thu Fri Sal Sun" ) ;

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

ructure elements are always stored In


memory locations.
struct employee e[ 3 j = { .. } ;
1 1 1 1 1 1
A 23 400.50 X 27 500.00 Y ] 28 ] 600.75 1
401 403 qo? 408 41 5

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);

Declarations & Definitions


gtruct e r n p l o y e m

float s ;
I iiruct employee e = { "Rahul", 25,4000.50 1 ; I

char n[20] ; in! a ; float s;


) e = { "Rahul", 25,4000.50 1, x, y ;

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 ;

I i i u c t emp e=( "Rahul", 23, "Ngp", 44010,4000.50 }d

Passina Structure Elements


main( )
{ struct book
{
char n[20] ; int nop ; float pr ;
1:
ktruct book b = { "Basic", 425, 135.00 ) ;
dlspla ( b.n, b.nop, b.pr ) ;
show rb.n, &b.nop, bb.pr ) ;
1
display (chbr *n, int pg, float p )
ho\d
'
1
printf ( "%s %d %I", n, pg, p ) ;
;how ( char *n, in1 *pg, float 'p )
I
printf "%s %d %f", n, 'pg, 'p ) ;
1

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+

struct corn a = ( 2.5, i.3 } ;


I struct corn b = I1.2, 1.7 ) ;
struct corn c ;
I
struct corn add (struct corn, struct corn) ;

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

Endlsk Iplatter ( aluminium ) )

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

char arr [ 512 ] ; lnt I ;


printf ( "Enter floppy in drive A
press any key ..." ) ;

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 ) ;

sector relorred uuinn


LW- & no.

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

printf ("insert infected disk, Press any key" ) -

1
getchl) :
--

fl

l o +"d Q . M-Cm4* eo08*&kab.


20 880
KIClT 1 C 1 Lecture 19 3
Better...
#include "d0s.h"

char a[ 512 ] : char ch = 'y' ;


char 'names[ ] P ("0Jerulalwrn'+, "Yankee Doodle",
"Robinson Cruso", "Eddle Murphy",
prlntl( "lnaert unlniectmd dlak, Pteas any key" ] ;
... 1 ; r\
getch( ;
absread( 0 , 1 , 0, a ) ;
whlle ( ch = 'y' )
prlntf ( "Insert Infected dlsk, Press any key" ) :
abswrlte ( 0,1, 0, a ) ;
prlntf ( "Another dlsk yln" ) ;
ch = getche( ) :

& disp\% =kG_~l\l~


ih hamQ OP

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 &

Você também pode gostar