You are on page 1of 46

C Language

The c Programming language was introduced by Dennis Ritchie in At and T Bell laboratories in early 19 !s"

#$#%&P'()R* $
A c +rogram has been con,erted in to the &achine language be-ore e.ecution" A c #om+iler translates a c +rogram in to a &achine language"

PR)$PR%#)//%R* $
Be-ore com+ailing a #$+rogram a +rocess called Pre$Processor is done on the source board by the +rogram called Pre$Processor" 0nderstanding this +rocess will hel+ us 1 include 2stdio"h3" This line is a +re$+rocessor Directory"The +re$+ocessor +rocess the # +rogram be-ore the com+ailer "All (ines in the +rogram beginning with the hash 415 sign are +rocessed by the Pre$+rocessor"The 1 include directi,e causes the Pre$+rocessor to e--ecti,ely insert the stdio"6 by into the #$Program" 7hen the #om+ailer com+iles the +rogram8 it seems the conte.ts o- the stdio"h -ile inserted into the Pre$+rocessor ,alue -ollowed by the rest o- the +rogram" 9ollowing are the +re +rocessor directi,es* 1de-ine 1include 1i1else 1unde1+rogma 1 de-ine*$ 1de-ine is used to ma6e macro de-inition in a c +rogram" ).* 1 de-ine &A: 1!! ;ere we de-ine a constant ma. with its ,alue 1!!" 7hene,er a +rogram encounted &A: it is re+laced to 1!!" 1include*$

-1 -

C Language
This is used to include the header -iles containing the -unction to be used in the -unction" 1include2stdio"h3 This will include the stdio"h -ile -rom the include directory" ).am+le +rogram 1include2stdio"h3 1include2conio"h3 main45 < clrscr45= +rint-4>7elcome To #>5= getch45= ?

@AR'AB()/ AAD ):PR)//'%A/* $


#;ARA#T)R /)T* $ The # character set consists o- u++er and lower case Al+habets8 Digits8 /+ecial #haracters and 7hite /+aces" The Al+habets and Digits together called as al+hanumeric characters" Al+habets * ABBB"C aBBB""D Digits * !BB"9 /+ecial characters * comma8 +eriod8 ash" 7hite s+ace characters * blan6 s+ace8 Aew line"

'D)AT'9')R/*$
'denti-iers as the name suggests are used to identi-y or name ,ariables8 /ymbolical constants8 -unctionsBetc" There are some rules regarding identi-iers names as -ollows*$ 415 'denti-ier name must be a seEuence o- digits or letters and must begins with a letter" 4F5 The underscore 4 G 5 symbol is considered as a letter" 4H5 Aames shouldnt be the same as 6ey words"

-2 -

C Language

I)J 7%RD/*$
Iey words ha,e +re$de-ined meanings and cannot be changed by the user" Iey words are +re$de-ined by the language and cannot be used by the user in any way" The -ollowing 6ey words are +re$de-ined by the language" Auto int -or e.term Brea6 long int while switch #ase long goto else #har i-loat do BBBB etc are some o- the +re$de-ined 6ey words in c"

@AR'AB()/*$
A ,ariable is an antity that has the ,alue and is 6nown to the +rogram by a name" A ,ariable decision associates a memory location with the ,ariable name" A ,ariable can ha,e only one ,alue assigned it at any gi,en time during the e.ecution o- the +rogram" @ariable names are identi-iers used to the name ,ariable" A ,ariable name consists oseEuence o- letters and digits" The -irst character must be a letter"

BA/'#$ DATA TJP)/*$


The c language su++orts the -ollowing Data ty+es" #har * A single byte that holds a character" 'nt * An 'nteger" 9loat * ;olds a -loating +oint number" Double * A double +recession -loating +oint number" RAAK) %9 DATA TJP)/*$ 'nteger 0n$signed int (ong int #haracter 0n$signed char 'nteger 9loat 9loat range #har Double

*$ *$ *$ *$ *$ *$ *$ *$ *$ *$

HF LM$HF L " !$LNNHN" F1O MHLO $F1O OMHLO " 1FM$1FM" !$FNN" F Bytes" O Bytes" $H"Oe $HM$H"OePHM 1 Byte" M Bytes"

-3 -

C Language
Double range :1.7e-308-1.7e+308.

A(K%R'T;&*$
An Algorithm is a -inite set o- roots -or sol,ing a s+eci-ic ty+e o+roblems" 4or5 Algorithm is nothing but the ste+ by ste+ +rocess o- a +rogram" /TR0#T0R) %9 A(K%R'T;&*$ Algorithm consists o- -ollowing ste+s* /te+ 1 *$ 'n$+ut" /te+ F *$ Assignment ste+" /te+ H *$ Decission ste+" /te+ O *$ Re+itati,e ste+" /te+ N *$ out$+ut"

9(%7 #;ART*$
A -low chart is a +ictorial diagram to sol,e a +articular +rogram" 't is a diagrammatic re+resentation that illustrates the seEuence oo+erations to be +er-ormed to arri,e the solutions" The o+erating instructions are +laced in bo.es8 which are connected by arrows" /J&B%(/*$The symbols are*$ %,al Rectangle Parallelogram Rhombus /tartQ/to+" Processing Bo." 'n+ut Q %ut +ut" Decisions bo."

#ircle

#onnector"

%P)RAT%R/ AAD ):PR)//'%A/$


There are /e,en ty+es o- %+erators *$ i" Arthematic %+erator"

-4 -

C Language
ii" Relational %+erator" iii" (ogical %+erator" i," Assignment %+erator" ," #onditional %+erator" ,i" Bit$7ise %+erator" ,ii" #omma %+erator" ART;)&AT'# %P)RAT%R*$ The Arthematic o+erator +er-orms arthematic o+erations and can be #lassi-ied in to unary and Binary Arthematic %+erations" %P)RAT%R/*$ P Addition o- unary P" $ /ubstraction o- unary $ " &ulti+lication" Q Di,ision" S &odulo Di,ision" ).am+le -or Arthematic o+erator 4addition5*$ 1include2stdio"h3 main45 < int a8b8cT!= +rint-4>)nter Two Aumbers aUb*>5= scan-4>SdSd>8Ua8Ub5= cTaPb= +rint-4>aPb 's Sd>8c5= getch45= ? /imilarly by changing the symbols o- the arthematic o+eratirs we can see the other o+erator +rograms4$8R8Q8S5 R)(AT'%AA( %P)RAT%R*$ These are used to com+are arthematic8 logical and character e.+ressions" %P)RAT%R/*$ 2 (ess than" 3 Kreater than" 2T (ess than or )Eual to" 3T Kreater than or )Eual to"

-5 -

C Language
TT )Eual to" VT Aot eEual to" (%K'#A( %P)RAT%R*$ 't is used to com+are or eEualate logical and relational e.+ressions" There are three logical %+erators in c" i" (ogical %R" ii" (ogical AAD" iii" (ogical A%T" (%K'#A( AAD*$ #onsider the -ollowing distribution" a3b UU .TT! The e.+ression on the le-t is a is greater than b" The e.+ression on the right .TT!"The whole e.+ression e,aluates to true only i- both e.+ressions are true" (%K'#A( %R*$ #onsider the -ollowing distribution" a3b WW .TT! 7hole e.+ression is one o- them is true8 or i- both o- them are true8 i"e" i- the ,alue o- a is less than o- m or n" (%K'#A( A%T*$ The not o+erator ta6es single e.+ression and e,aluates to true ithe e.+ression is -alse8 e,aluates the -alse i- the e.+ression is true" B'T$7'/) %P)RAT%R*$ 't o+erates each +rint o- Data" These o+erators are used -or testing8 #om+lementing or shi-ting bits to the right or le-t" UU Bit wise AAD" WW Bit wise %R"

X
Y 22 33

Bit wise :%R" Bit wise #om+lement" /hi-t (e-t" /hi-t Right"

):A&P()*$ AT1H8 BT " Binary re+resentation o- a is !!!! !!!! 11!1 Binary re+resentation o- a is !!!! !!!! !111 B'T$7'/) AAD*$ Bit wise AAD %+erator aUUb"

-6 -

C Language
a T !!!! !!!! 11!1 b T !!!! !!!! !111 cTaUUb T !!!! !!!! !1!1 B'T$7'/) %R*$ The statement aWWb" a T !!!! !!!! 11!1 b T !!!! !!!! !111 cTaWWb T !!!! !!!! 1111 B'T$7'/) :%R*$ The statement aXb a-ter this statement is e.ected8 a bit in c will be when e,er the corres+onding bits in aXb di--er" a T !!!! !!!! 11!1 b T !!!! !!!! !111 cTaXb T !!!! !!!! 1!1! /;'9T ()9T %P)RAT%R*$ The le-t shi-t o+erator is a binary o+erator" 9or e.am+le consider the statement cTa22H" The ,alue in integer a is shi-ted to le-t by H bit +osition" The result is assigned to integer c"The ,alue o- a is !!!! !!!! 11!1" The ,alue o- c a-ter the e.ecution oabo,e statement is !!!! !11! 1!!! " /;'9T R'K;T %P)RAT%R*$ The Right shi-t o+erator is also a binary o+erator" 9or e.am+le consider the statement cTa33F" The ,alue in integer a is shi-ted to right by F bit +osition" The result is assigned to integer c" The ,alue o- a is !!!! !!!! 11!1" The ,alue o- c a-ter the e.ecution oabo,e statement is !!!! !!!! !!11" B'T$7'/) #%&P()&)AT *$ 't is a unary o+erator" 't gi,es the ,alue got by com+lementing each bit o- o+erator" ART;)&AT'# A//'KA&)AT %P)RAT%R*$ This %+erator e,aluates the e.+ression on right8 and assigns the ,alue to ,ariable on the le-t8 the assignment o+erators are* T8 $T8 PT8 QT8 RT " 'A#R)&)AT $ D)#R)&)AT %P)RAT%R*$ These are e.tensi,ely used in -or U while loo+s" The synta. o- %+erators is gi,ae below"

-7 -

C Language
PP4@ariable name5 'A#R)&)AT $$4@ariable name5 4@ariable name5PP D)#R)&)AT 4@ariable name5 The o+erator PP ads %ne and $$ /ubstracts %ne" #%AD'T'%AA( %P)RAT%R*$ The conditional o+erator consists o- two symbols 4Z And *5 [uestion and coloumn" /JATA:*$ Te.t ).+ression" ).+ression F largeT i 3\ Z i*\ ).+ression 1 #onditional %+erator" /TR0#T0R)*$
is text exp

T Conditional exp takes value exp 1

F conditional exp takes exp 2

+rogram*$ 1include2stdio"h3 main4 5 < int i8\8large= +rint-4enter ,alue -or i8\5 = scan-4SdSd8Ui8U\5 = largeTi3\Zi*\ = +rint-4large ,alue is Sd8large5 = ? #%&&A %P)RAT%R*$ A set o- e.+ressions se+arated by commas is ,alid construct in the c$language" ).*$ i8\ are declared by the statement" int i8\= +rogram*$ Area o- a Triangle" 1include2stdio"h3

-8 -

C Language
main4 5 < -loat b8h8area= +rint-4)nter ,alue -or b8h5= scan-4SdSd8Ub8Uh5= areaT4bRh5QF= +rint-4Area o- the triangle is Sd8area5= ?

'AP0T /TAT)&)AT/*$
'n+ut statements are used -or reading integer8 character8 string and mi.ed ty+e data" 9or this +ur+ose some standard in+ut -unctions are used" They are i" get char4 5= ii" scan-4 5= iii" gets4 5= i," getche4 5= 4i5" K)T #;AR4 5= *$ This -unction is used -or reading a single character -rom the 6eyboard" The ,ariable in which you want to store a single character should be o- character ty+e " The synta. is char ,= ,T get char4 5= 4ii5" /can-4 5= *$ The scan- in+ut statement is used -or reading mi.ed data ty+es" 7e can read int8 -loat8 char8 e.ha8 decimal8 octaBBetc" by using it" 'ts control codes are -ormat codes" synta. * /can-4control string 58 U,18 U,F8B""5= 4iii5" Kets4 5= *$ The +ur+ose o- the gets statement is to read a string" 't will read a string until you +ress enter 6ey -rom the 6ey board" 't will mar6 null

-9 -

C Language
character in the memory at the end o- the string when you +ress the enter 6ey" synta. * ,T gets4 5= here , is the string ,ariable" i"e" character ,ariable" 4i,5" get che4 5= *$ 't will store any ,alue -rom the 6ey board and also dis+lays it on the screen" synta. * getche4 5=

%0T P0T /TAT)&)AT/*$


%ut +ut statements are used -or writing8 dis+laying8 +rinting8 character8 string8 mi.ed ty+e data" These are * i" +ut char 4 5= ii" +uts 4 5= iii" +rint- 4 5= +ut char 4 5= *$ +ut char is an out +ut statement" The +ur+ose o- +ut char statement is to dis+lay a single character on the screen" synta. * Put char 4,5= 7here , is the ,ariable o- character ty+e in which a single character data is stored" Puts4 5= *$ The +ur+ose o- the +uts statement is the +rint or dis+laying a string in+uted by the gets statement" synta. * Puts 4,5= ;ere , is the string ,ariable" Print- 4 5= *$ The +rint- statement is used to dis+lay a te.t message or a ,alue stored in the ,ariable" synta. *

-10 -

C Language
Print-4control string8 ,18 ,F8BB5= ;ere ,18 ,F8B are ,ariables"

#%ATR%( /TR0#T0R)/ *$
#ontrol /tructures are o- two ty+es "They are*$ i" Decission ma6ing control structures" i" iii" i-$else iii" switch ii" (oo+ #onstracts" i" -or loo+" ii" 7hile loo+" iii" Do$while loo+" D)#'//'%A &AI'AK #%ATR%( /TR0#T0R)/ *$ '9 /TAT)&)AT*$ /JATA:*$ i-4te.t e.+5 /tatement 1= /ingle /tatement i-4te.t e.+5 < /tatement 1= #om+ound /tatement /tatement F= ? 9(%7 #;ART*$
text exp

is

body o- the loo+

+rogram*$ (argest o- three numbers 1 include2stdio"h3 main4 5

-11 -

C Language
int a8b8c8big= +rint-4)nter any three integer ,alues 5= scan-4SdSdSd8Ua8Ub8Uc5= i-4bigT!5 +rint-4a is big5= i-4bTbig5 +rint-4B is big5= i-4cTbig5 +rint-4c is big5= +rint-4The largest ,alue is Sd8 big5= <

? '9$)(/) /TAT)&)AT *$ /JATA:*$ /'AK() /TAT)&)AT *$ i-4te.t e.+ression5 statement 1= else statement F= #%&P%0AD /TAT)&)AT/*$ i-4te.t e.+ression5 < statement 1= statement F= BBBBBB" ? else < statement 1= statement F= BBBBBBB" ? 9(%7 #;ART *$

Text exp

is

-12 -

C Language
Body o- istatement body o- i-$else statement

).it Program*$ 1include2stdio"h3 main4 5= int '= +rint-4)nter ,alue -or '5= scan-4Sd8Ui5 = i-4'SHTT!5 +rint-4Sd is di,isible by H8 '5 = else +rint-4Sd is not di,isible by H 8 '5 = ? /7'T#; /TAT)&)AT*$ A switch statement allows the user to choose a statement or grou+ o- statements among se,eral alternati,es" The switch statement is use-ul when a ,ariable is to be com+ared with di--erent constants and in case it is eEual to a constant a set o- statements are to be e.ecuted" Also the constants in the case statement can be o- char8 int data ty+es only " /JATA: *$ /witch4n5 < case 1* /tatement 1= /tatement F= Brea6= #ase F* /tatement 1= /tatement F= Brea6= BBB"= de-ault= statement 1= ?

-13 -

C Language

9(%7 #;ART*$

Switch variable 1st Case

body of 1st loop

/witch ,ariableFnd case

body o- Fnd loo+

body o- de-ault (oo+ ).am+le*$ Roots o- [uadratic ).+ressions* 1include2stdio"h3 1include2math"h3 main45 < -loat a8b8c8d8real8img8 r18rF= int 6= +rint-4)nter the three ,alues -or a8b8c5= scan-4S-S-S-8Ua8Ub8Uc5= dTbRb$4ORaRc5= i-4aVT!5 < i-4d2 !5 6T1= else i-4dTT!5 6TF= e.it

-14 -

C Language
i-4d3!5 6TH= switch465 < case 1* +rint-4The roots are imaginary5= dT$d= realT$bQ4FRa5= imgTsErt4dQ4FRa55= +rint-4The roots are5= +rint-4r1TS-PS-8real8img5= +rint-4rFTS-$S-8real8img5= brea6= case F* +rint-4The roots are real and eEual5= r1T$bQ4FRa5= +rint-4r1TrFTS-8r15= brea6= case H* +rint-4The roots are real U un eEual5= realT$bQ4FRa5= imgTsErt4dQ4FRa55= +rint-4The roots are5= +rint-4r1TS-PS-8real8img5= +rint-4rFTS-$S-8real8img5= brea6= ?

? else ?

+rint-4The eEuation is linear5=

(%%P #%A/TR0#T/ *$ (oo+s in c called as a section o- a +rogram to be e.ecuted re+eatedly while an e.+ression is true " 7hen the e.+ression becomes -alse8 the loo+ terminates U the control +asses on to the statement -ollowing the loo+" A loo+ consists o- two statements" i" #ontrol /tatement"

-15 -

C Language
ii" Body o- the loo+" 9%R (%%P*$ 't is used while e.ecuting the loo+ a number o- times" /JATA:*$ 9or4initialiDation= te.t e.+= u+date e.+5 /tatement Qcom+ound statement= ):A&P() *$ The +rogram that dis+lays the -irst 1! multi+les o- N on a single line is gi,en below" 1include2stdio"h3 main4 5 < int i= -or4iT!=i2PN=iPP5 +rint-4Sd8i5= ? The -or 6ey word is -ollowed by three com+onents enclosed within round braces4 5"These three com+onents are se+arated by semicoloun4=5" 'n the abo,e e.am+le the three com+onents are iT18i2TN8iPP The -irst com+onent iT1 is e.ecuted only once +rior to the statements with in the -or loo+" This is called initialiDation e.+ression" The second com+onent '2PN is e,aluated once be-ore e,ery e.ecution o- the statement with in the loo+" This is called the te.t e.+ression o- the loo+" '- this e.+ression is true the statement with in the loo+ e.ecutes " '- it is -alse8 the loo+ terminates U the control o- the e.ecution is trans-ered to the statement -ollowing the -or loo+" The third com+onent iPP is e.ecuted once e,ery e.ecution othe statement with in the loo+" 'n this case this increment the ,alue o- i by 1" 't is called the u+date e.+ression" 'A'T'A('CAT'%A ):PR)//'%A*$ The control ,ariable is initialiDed" This +art is e.ecuted once be-ore e.ecuting the body o- the loo+" T)/T ):PR)//'%A*$

-16 -

C Language
The ,alue o- the control ,ariable is tested and i- -ound true 8the body o- the loo+ is e.ecuted other wise the loo+ is terminated" This e.+ression is e,aluated be-ore e,ery e.ecution o- the loo+ body" 0P DAT) ):PR)//'%A*$ A-ter e.ecuted the last e.+ in the -or loo+ the control is trans-erred bac6 to the -or loo+ statement" The control ,ariable is u+dated using an e.+ such as 'PP and the te.t e.+ is e,aluated to see iit is true be-ore e.ecuting the body o- the loo+" 9or multi+le initialiDation and u+ date e.+ and u+ date loo+ @ariables we use the comma o+erator" ):A&P()*$ 1include2stdio"h3 main4 5 < int i86= +rint-4't is Nth table 5= -or4iT!=i2TF!=iPP5 < 6TiRN= +rint-4NRSdTSd]n8i865= ? ? 9(%7 #;ART*$ initialiDation

T Body o- loo+ 0+date e.+

is te.t e.+

9 e.it

7;'() (%%P*$

-17 -

C Language
The while loo+ is used when the no o- times the loo+ is to be e.ecuted is not 6nown in ad,ance" 7hile loo+ is to+ tested loo+" /JATA:*$ 7hile4te.t e.+5 < statement 1= statement F= BBBB"= ? 9(%7 #;ART*$

T Body o- loo+

while
exp

e.it

):A&P() *$ QR A,erage o- n numbers RQ 1include 2stdio"h3 main4 5 < int i8nT!= -loat sumT!8a,g= +rint-4)nter ,alues ^1 to terminate 5= scan-4Sd8Ui5= while 4iVT$15 < sumTsumPi= nTnPP= scan-4Sd8Ui5= ? a,gTsumQF= +rint-4A,erage o- S- is 8a,g5= ? The -irst scan- statement \ust be-ore the while loo+ in+uts the -irst mar6s and stores it in i" The scan- statement inside the loo+ reads

-18 -

C Language
the rest o- the numbers one by one when ^1 is read8 the condition in the while statement" iVT$1 e,aluates to -alse" /o8 the while loo+ terminates and the +rogram e.ecution +roceedes with the statements immediately a-ter the while loo+" 7hich in the abo,e +rogram" D%$7;'() (%%P*$ The Do$while loo+ e,aluates the condition a-ter the e.ecution ostatement in its construct" The statement with in the Do$ loo+ are e.ecuted at least o!nce" /o8 the Do$while loo+ is called a bottom tested loo+" '- a single statement has to be e.ecuted re+eatedly8 then the synta. is /'AK() ):PR)//'%A* do statement 1= 7hole 4te.t e.+5= &0(T' /TAT)&)AT * do < statement 1= stement F= ? while 4te.t e.+5= 9(%7 #;ART*$

Body o- loo+

te.t e.+

is

e.it

e.am+le +rogram*$ QR Aumber Palindrome RQ 1include2stdio"h3

-19 -

C Language
main4 5 < int num8digit8re,8n= +rint- 4)nter ,alue -or n5= scan- 4Sd8Un5= numTn= do < digitTnQ1!= re,Tre,R1!Rdigit= nTnS1!= ? while nVT!5= i-4numTTre,5 +rint-4Sd is Palindrome8 num5= else +rint-4Sd is not +alindrome 8num5= ? BR)AI*$ A brea6 statement terminates the loo+ U control is trans-erred to the statement immediately -ollowing the loo+" 9(%7 #;ART*$

is T
Te.t e.+

brea6

)nd o- loo+ ).am+le -or brea6 is similar to the switch which are used combindly in many o- our reEuirements" #%AT'A0) *$ 't is used to by +ass the remainder o- the current +ass through a loo+" The loo+ doesnt terminate when a continue statement is

-20 -

C Language
encounted" The remaining loo+ statements are s6i++ed and com+utation +roceeds directly to the ne.t +ass through the loo+" This statement is gi,en as * 9(%7 #;ART *$

#ontinue T

condition 'n loo+

remaining +art o- loo+

).am+le*$ /um o- two +ositi,e integers" 1include2stdio"h3 main45 < int iT!8num8sumT!= -or4iT!= i2N= iPP5 < +rint-4)nter any ,alues5= scan-4Sd8Unum5= i-4num2!5 < +rint-4't is negati,e number5= continue= ? sumPTnum= ? +rint-4/um o- P,e number is Sd8sum5= ?

#A/T'AK A @A(0) *$
The +rocess o- con,erting one data ty+e to another data ty+e is called casting a ,alue" To cast a ,alue we ha,e to -ollow the below synta.* _,arT`data ty+e 4e.+5= .T-loat4 5=

-21 -

C Language
e.* .T1!QH ,alue in . is H"!!! . T-loat41!5QH .TH"HHHH

%P)RAT%R PR)#'D)A#)*$
The +recidenci o- o+erators will always be considered by the com+iler when e,aluating an e.+ression" The +recidenci o- o+erators is as shown below" 45 " R8Q P8$8S 28 38 3T8 2T8 TT8 VT UU WW V

90A#T'%A/*$
A number o- -unctions loo+ed in to a single logical unit is re-erred to as a -unction" /TR0#T0R) %9 A 90A#T'%A*$ Return ty+e -unction name4asrgs list5 < body o- the -unction ? ).am+le*$ 1include2stdio"h3 int cube 4int i5 9unction declaration" Parameter ty+e" < Parameter" int ret ,al= Return ty+e" (ocal ,ariable" ret @alTiRiRi= return ret ,al= ? main4 5

-22 -

C Language
< int a= (ocal ,ariable" +rint-4)nter ,alue 5= scan-4Sd8Ua5= +rint-4The cube o- ,alue o- Sd is Sd8"a8 cube4int a55= 9unction call"

PART/ %9 A 90A#T'%A*$ i" 9unction declaration" ii" 9unction de-inition" iii" Return /tatement" i," 9unction #all" 90A#T'%A D)#(ARAT'%A*$ A -unction declaration +ro,ides the -ollowing in-ormation to the com+ailer" i" The name o- the -unction" ii" The ty+e o- the ,alue returned" iii" The number and the ty+e o- arguments that must be su++lied in a call to the -unction" 7hen a -unction call is encounted the com+ailer chec6s the -unction call with its declaration" A -unction declaration is has the -ollowing synta." /JATA:* Return ty+e -unction name 4ty+e 5= Return ty+e s+eci-ies the data ty+e o- the ,alue in the return statement" The -unction can written in any data ty+e" '- there is no return ,alue the 6ey ,oid is +laced be-ore the -unction name" 9unction declarations are also called +rototy+es" 90A#T'%A D)9'AAT'%A*$ 't is similar to the -unction declaration but doesnt ha,e the semicoloun the -irst line in the -unction de- is called -unction declaration" This is -ollowed by the -unction body" 't is com+ared o- the statement that ma6es u+ the -unction8 delimited by braces" The declaration U declaration must use the same -unction name8 no o- arguments8 argument ty+e U the return ty+e" Ao -unction de-inition is allowed with in a -unction de-inition"

-23 -

C Language
/ynta.*$ Return ty+e 9unction name 4arguments5= 90A#T'%A R)T0RA @A(0)* $ 9unctions in c may or may not ha,e return ,alues" '- a -unction doesnt return a ,alue8 the return ty+e in -unction de-inition and declaration is s+eci-ied as ,alid" 90A#T'%A #A((* $ A -unction call is s+eci-ied by the -unction name -ollowed by the ,alue o- the +arameter enclosed with in +arenthesis8 terminated by the semicoloun 4=5" ).am+le*$ &a.imum o- three numbers 1include2stdio"h3 int ma. -unc4int '8int \8 int 65 < int ma.= i-4i3T\ UU i3T65 ma.T'= else i-4\3T65 ma.T\= else ma.T6= return ma.= ? main45 < int a8b8c8m= +rint-4)nter the ,alues -or a8b8c5= scan-4SdSdSd8Ua8Ub8Uc5= mTma.-unc4a8b8c5= +rint-4&a.imum o- SdSdSd is Sd8a8b8c8m5= ?

-24 -

C Language
90A#T'%A PARA&)T)R* $ These are the means o- communication between the calling U called -unctions" They can be classi-ied in to -ormer +arameters U actual +arameters" The -ormer +arameters are the +arameters gi,en in the -unction declaration U -unction de-inition" The actual +arameters8 o-ten 6nown as arguments are s+eci-ied in the -unction call" #AT)K%RJ %9 90A#T'%A/*$ The -unction categories are o- three ty+es* '" 9unction with no arguments and no return ,alues" ''" 9unction with arguments and no return ,alues" '''" 9unction with arguments and with return statements"

'"90A#T'%A 7'T; A% ARK0&)AT/ AAD A% R)T0RA @A(0)/* $ 7hen a -unction has no arguments it does not recei,e data -rom the calling -unction" /imilarly when it doesnt return a ,alue the calling -unction does not recei,e any ,alue" 't e--ect there is no trans-er odata between calling and called -unctions" ''"90A#T'%A 7'T; ARK0&)AT/ AAD A% R)T0RA @A(0)/* 7hen a -unction has arguments it must recei,e data -rom the calling -unction" 't this category data will be trans-er in a one$way direction" 9rom calling -unction to called -unction" '''"90A#T'%A 7'T; ARK0&)AT/ AAD 7'T; R)T0RA /TAT)&)AT/* 't this category data will be trans-er both the ways8 -rom calling to called -unction and called to calling -unction" AD@AA#)D 9)AT0R)/ %9 90A#T'%A/*$ '" 9unction declaration and +roto ty+e" ''" #alling -unction by ,alue or by re-erence" '''" Recursi,e -unctions" '" 90A#T'%A D)#(ARAT'%A AAD PR%T% TJP)"

-25 -

C Language
Any # -unction by de-ault returns an integer ,alue" To return a ,alue other than an integer be ha,e to s+eci-y the +roto ty+e declaration" /ynta.*$ Return ty+e -unction ty+e 4argument5= 'n the abo,e synta. return ty+e s+eci-y the ty+e o- ,alue return by the -unction" 9unction name is the name o- the -unction and arguments are the data ty+es o- the ,alues to be +assed to the -unction" The +rototy+e declaration must be s+eci-ied the declaration +art o- the calling -unction" This declaration s+eci-ies the -ollowing" Aame o- the -unction" Return ty+e o- the -unction" The number and data ty+e o- the arguments"

).*

1include2stdio"h3 main4 5 < int a8b8c= int +ower 4int8 int 5= cT+ower4a8b5= ? +ower int .8 int y5 < -loat abc 4int8 int5= abc 4.8y5= ? -loat abc4int 68 int l5 < ?

''" #A(('AK 90A#T'%A/ BJ @A(0) %R R)99)R)A#)*$ Passing the ,alues o- ,ariables -rom one -unction to another -unction is called call by ,alue" #A(( BJ R)9)R)A#)*$

-26 -

C Language
'n a -unction call statement i- we +ass the address o- a ,ariable then such a -unction call is called as call by re-erence" '''"R)#0R/'%A &)T;%D*$ ).+ressing any intity interms o- itsel- is called R)#0R/'%A" 'n c a -unction can call any -unction that has been de-ined including itsel-"

Recursion*$
).+ressing an intity in terms o- itsel- is called R)#0R/'%A" 'n c a -unction can call any -unction that has been de-ined including itsel-" ).am+le*$ 9actorial o- a Aumberusing Recursion" 1include2stdio"h3 main45 < int n= long int .= +rint-4)nter the ,alue -or n5= scan-4Sd8 Un5= nT-act4n5= +rint-49actorial o- Sd is Sd8 n8.5= ? long int -act4int num5 < i- 4numTT!5 return 1= else return numR-act4num$15= ?

/T%RAK) #(A0/)/*$
The storage clause o- a ,ariable indicates the allocation o- storage +lace to the ,ariable" /torage clauses are o- -our ty+es* i" Auto" ii" Register" iii" /tatic"

-27 -

C Language
i," ).term" A0T% @ariable*$ All ,ariables declared with in a -unction are A0T% by de-ault" @ariables declared A0T% can only be accessed only with in the -unction or the nested bloc6 with in which they are declared" They are created in to and destroyed when it is e.ited" Aote*$ All the +rograms we are written are ta6en or treated as A0T% ,ariables " R)K'/T)R @AR'AB()* $ There are stored in the register o- the micro+rocessors" The number o- ,ariables which can be declared register are limited" '- more ,ariables are declared they are treated as A0T% ,ariables" A +rogram that uses R)K'/T)R ,ariables e.ecutes -aster as com+ared to a similar +rogram without register ,ariables"

/TAT'# @AR'AB()/* $ /tatic ,ariables are o- two ty+es* i" /tatic ,ariables that are declared with in a -unction" ii" 9ile static @ariable" /TAT'# @AR'AB()/ T;AT AR) D)#(AR)D 7'T; 'A A 90A#T'%A*$ These ,ariables retain their ,alues -rom the +re,ious call" i"e" the ,alues which they had be-ore returning -rom the -unction" 9'() /TAT'# @AR'AB() *$ These are declared outside any -unction using the 6eyword /TAT'#" 9ile static ,ariables are accessiable only in the -ile in which they are declared" ).am+le*$ 1include2stdio"h3 +rint count45 < static int countT1= +rint-4countTSd8 count5= countTcountP1= ?

-28 -

C Language
main45 < +rint count45= +rint count45= +rint count45= ? ):T)R& *$ 7hen a +rogram scans across di--erent -iles we want to ha,e a global ,ariable accessible to all -unctions in these -iles8 the 6ey word ):T)R& should be used be-ore the data ty+e name in the declarations in all the -iles where it is accessed e.ce+t one" The lin6er reEuires that only one o- these -iles ha,e the de-inition o- the identi-iers" Klobal ,ariables de-initions can occur in one -ile only "

ARRAJ/*$

An array is a seEuence o- data in memory where in all data are o- the same ty+e8 and are +laced in +hysically ad\acent location" A string can be considered as seEuence o- characters" Arrays are classi-ied as -ollows %ne$dimensional array" Two$ dimensional array" &ulti dimensional array" %A)$D'&)A/'%AA( ARRAJ" This is an array which as only a single row" To declare one$dimensional array we ha,e to use the -ollowing synta." Data ty+e argument _siDe`= /ynta.* return ty+e ,ariable name_siDe o- the array`= ).am+le*$ A,erage o- A Aumbers" 1include2stdio"h3 main45 <

-29 -

C Language
int a_1N`8'8n= -loat sumT!8a,g= +rint-4)nter the range o- the array5= scan-4Sd8Un5= +rint-4)nter the ,alues5= -or4iT!=i2n=iPP5 < scan-4Sd= Ua_i`5= sumTsumPa_i`= ? -or4iT!=i2n=iPP5 +rint-4SOd8a_i`5= a,gTsumQn= +rint-4a,gTS-8a,g5 ?

T7%$ D'&)A/'%AA( ARRAJ" An array is used to store data in more than one row and one coloumn is called two$dimensional arrays" /ynta.* Data ty+e array name _rows`_columns`= ).am+le*$ matri. addition using arrays 1include2stdio"h3 main45 < int i8 a_1!`_1!`8 b_1!`_1!`8 c_1!`_1!`8 \8m8n8+8E= +rint-4)nter the siDe o- the array A5= scan-4SdSd8Um8Un5= +rint-4> )nter the ,alues o- the matrices A>5= -or4iT!= i2 m= iPP5 -or4\T!= \2 n= \PP5 < scan-4>Sd>8Ua_i`_\`5= ?

-30 -

C Language
+rint-4)nter the siDe o- the array B5= scan-4SdSd8U+8UE5= +rint-4> )nter the ,alues o- the matrices B>5= -or4iT!=i2 +=iPP5 -or4\T!=\2 E =\PP5 < scan-4>Sd>8Ub_i`_\`5= ? -or4iT!=i2 m=iPP5 -or4\T!=\2 E=\PP5 < c_i`_\`Ta_i`_\`Pb_i`_\`= ? +rint-4The &atri. sum is 5= -or4iT!=i2 m=iPP5 < -or4\T!=\2 n=\PP5 < +rint-4>Sd]t>8c_i`_\`5= ? +rint-4>]n>5= ? ? &ulti ^ Dimensional Array*$ # allows us o- three or more dimensions" The e.act limit is determined by the com+ailer" The general -orm o- this is as -ollows /ynta.*$ Ty+e name array name _s1`_sF`_sH`BB_si`= 7here si is the siDe o- the ith dimension" ).am+le*$ 'nt _H`_O`_ `=

/TR'AK/ *$

/trings are arrays o- characters i"e" they are characters arranged one a-ter another in memory" To ma6e the end o- the string8 c uses the null character" /trings inc are enclosed with in double Euotes"

-31 -

C Language
/TR'AK 90A#T'%A/*$ There are -our ty+es o- string -unctions *$ ' /tr cat" '' /tr cm+" ''' /tr c+y" '@ /tr len" To use these -unctions the header -ile string"h must be include in the +rogram with the statement" /TR #AT*$ 4string concatenation5 This -unction concotinates two strings i"e" in a++ends one string and that o- another" The -unction acce+ts two strings as +arameters and stores the contents o- the second string at the end o- the -irst" ).am+le*$ 1include2stdio"h3 1include2string"h3 main45 < char str1_F!`T+hanindra= char strF_F!`Tchowdary= +rint-4/tring1TSs8str15= +rint-4/tringFTSs8strF5= strcat4str18strF5= +rint-4A-ter concatenating the string is Ss 8 str1 5= ? /TR #&P* $ 4string com+arision5 The -unction ctr cm+ com+ares two strings" This -unctionc is use-ul while writing +rograms -or constructing and searching string as arranged in a directory" The -unction acce+ts two strings as +arameters and returns an integer whose ,alue is less than Dero i- the -irst string is less than the second" T! '- both are identical" 3! '- the -irst string is greater than second" The -unction str cm+ com+ares the two strings8 character by character8 to decide the greater one" 7hen e,er the two characters in the string

-32 -

C Language
di--er 8 the string which has the character8 with a higher A/#'' ,alue is greater" ).am+le*$ 1include2stdio"h3 1include2string"h3 main45 < char str1_F!`T+hanindra= char strF_F!`T+hanindra chowdary= int result= resultTstrcm+4str18strF5= i-4result3!5 +rint-4/tring1 is greater than stringF TSs8Ss8str18strF5= else i-4resultTT!5 +rint-4/tring1is eEuals to stringFTSs8Ss8str18strF5= else +rint-4/tring1 is leess than stringF TSs8Ss8str18strF5= ?

/TR #PJ*$ 4string co+y5 The str c+y -unction co+ies one string to another" The -unction acce+ts two strings as +arameters and co+ies the second string character by character in to the -irst one8 u+ to and including the null character o- the second string" ).am+le*$ 1include2stdio"h3 1include2string"h3 main45 < char str1_F!`T+hanindra= char strF_F!`Tchowdary &aganti= +rint-4Be-ore co+ying/tring18stringFTSs8Ss8str18strF5= strc+y4str18strF5= +rint-4A-ter co+ying the string is Ss 8 str1 5=

-33 -

C Language
? /TR ()A *$ 4/tring length5 This -unction returns the integer which denotes the length othe string +assed" The length o- the string is the number o- characters +ercent in it" ).cluding the terminating the null character" 1include2stdio"h3 1include2string"h3 main45 < char str1_F!`T+hanindra= int len= lenTstr len4str15= +rint-4The length o- the /tring1TSs8len5= ?

P%'AT)R/*$
&emory is organiDed as a seEuence o- byte$siDed notation" These bytes are numbered beginning with a Dero" The number associated with a byte is 6nown as its address or memory location" A +ointer is an intity which contains a memory address" A +ointer is a number8 which s+eci-ies a location in memory" )ach +oint in memory is associated with a uniEue address" An address is an integer ha,ing -i.ed number o- bits8 labeling a byte in the memory" Addresses a-ter +ositi,e integer ,alues that range -rom Dero to some +ositi,e integers constant corres+onding to the last location in the memory" ),ery ob\ect i"e" loaded in memory is associated with a ,alid range o- address"

-34 -

C Language
The siDe o- the data ty+e is re-erenced by the +ointer is the no o- bytes that may be accessed directly by using that address" 0/)/ %9 P%'AT)R/*$ i" Accessing Array elements" ii" Passing arguments through -unctions" iii" Passing arrays and strings through -unctions" i," #reating data structures such as lin6ed lists8 trees etc" ," %btaining memory -rom the system dynamic memory"

).am+le*$
7or6ing o- a +ointer using addtresses" 1include2stdio"h3 main45 < int aTN8bT1!8cTF!= +rint-4Address o- Sd is Sd8a8Ua5= +rint-4Address o- Sd is Sd8b8Ub5= +rint-4Address o- Sd is Sd8c8Uc5= ?

).am+le*$

7or6ing an ,ariable using +ointer" 1include2stdio"h3 main45 < int R+tr= int ,ar18,arF= ,arT1!!= ,arTF!!= +trTU,ar1= +rint-4Address and contents o- ,ar1 is Sd8+tr8,ar15= +trTU,arF= +rint-4Address and contents o- ,arF is Sd8+tr8,arF5= +trT1FN= +rint-4Address and contents o- ,arF is Sd8+tr8,arF5= ,ar1TR+trP1= +rint-4Address and contents o- ,ar1 is Sd8+tr8,ar15= ?

-35 -

C Language Address o+erator*$


All the ,ariables de-ined in a +rogram resisted s+eci-ic address" These +ossible to obtain the address o- a +rogram ,ariable by using the address o+erator 4U5 am+ersand" D)$R)9)R)A#'AK P%'AT)R*$ 't is an o+erator +er-orms to access and mani+ulate data contained in memory location +ointed to by a +ointer" The o+erator 4R5 is used to de$re-erencing +ointers" A +ointer ,ariable is de$re-erenced when the unary o+erator 4R5 in this case as a +re-i. to the +ointer ,ariable or +ointer e.+ression" ).am+le*$ 1include2stdio"h3 main45 < int R+tr8,ar18,arF= R+trTFN= R+trPT1!= +rint-4,ariable ,ar1 contains Sd8R+tr5= ,aeFTR+tr= +trTU,arF= Ri+trPTF!= +rint-4,ariable ,arF contains Sd8R+tr5= ? ).am+le*$ 1include2stdio"h3 main45 < -loat data18dataF= -loat swa+4-loat8-loat5= +rint-4)nter any two -loating ty+e ,alues5= scan-4S-S-8Udata18dataF5= +rint-4Be-ore swa+ing the numbers are S-8S-8 data18dataF5= swa+4Udata18 UdataF5= +rint-4A-ter swa+ing the numbers are5= +rint-4data1TS-8dataFPS-8data18dataF5= ?

-36 -

C Language
-loat swa+4-loat Rd18 -loat RdF5= < -loat tem+= tem+TRd1= Rd1TRdF= RdFTtem+= ? P%'AT)R/ 7'T; ARRAJ*$ The elements o- an array can be e--iciently accessed by using +ointer" 415" Array elements are always stored in continuos memory location irres+ecti,e o- the siDe o- Array" 4F5" The siDe o- the data ty+e which the +ointer ,ariable +re-ers to8 is de+endent on the data ty+e +ointed to by +ointer" 9or e.am+le i- the +ointer ,ariable +oints to an integer data ty+e8 then it access two bytes in memory" 4H5" A +ointer when incremented8 always +oints to a location a-ter s6i++ing the no o- bytes reEuired -rom the data ty+e +ointed to by it" 9or e.am+le a +ointer ,ariable +oints to an integer ty+e at a certain address 1!!!8then on incrementing the +ointer ,ariable i- +oints to the location 1!!F"

)am+le*$

/earching the smallest element o- an ARRAJ using P%'AT)R 1include2stdio"h3 main45 < int R+tr8 a _F!`8i8n8 small= +rint-4)nter the siDe o- the array5= scan-4Sd8Un5= +rint-4)nter the )lements o- the array5= -or4'T!='2 n='PP5 scan-4Sd8Ua_'`5= +trTa= smallTR+tr=

-37 -

C Language
+trPP= -or4'T1='2 n='PP5 < i-4small3R+tr5 smallTR+tr= +trPP= ? +rint-4/mallest element is Sd8 small5= ? P%'AT)R T% P%'AT)R*$ ),ery +ointer created in the memory has an address" This address can be stored in another +ointer ,ariable " the +ointer which stores the address o- another +ointer is called +ointer to +ointer" To declare a +ointer to +ointer ,ariable we ha,e to use the -ollowing synta." Data ty+e RR +ointer = ).* int RR6=

/TR0#T0R)/ *$
A structure in c is a hetrogenous user de-ined data" A structure may contain di--erent data ty+es" 't grou+s ,ariables in to a single antity"

/truct student < int rno= int sub= -loat mar6s=

/tructure name Iey word"

/tructure member Declaration"

?=

R0()/ 9%R D)#(ARAT'%A %9 'AD'@'D0A( &0&B)R/*$ The indi,idual members o- a structure may be any o- common data ty+es such as int8 -loat8B"" )tc8 +ointers8 arrays or e,en other

-38 -

C Language
structures" All member names with in a +articular structures must be di--erent" 'ndi,idual members cannot be initialiDed inside the structure declaration" D)#(AR'AK /TR0#T0R) @AR'AB()*$ 415" 'A T;) /TR0#T0R) D)#(ARAT'%A*$ The structure ,ariable can be s+eci-ied a-ter the closing braces" struct student < int rno= int sub= -loat mar6s= ? student 18 student F= /tudent is the structure tag" /tructure 1 and structure F are ,ariables o- ty+e student" '- other ,ariables o- the structure are not reEuired the tag name student can be omitted" 4F5" 0/'AK T;) /TR0#T0R) TAK*$ The ,ariables o- the structure may also be declared se+arately by using the structure tag" /truct student student 18 student F= student 1 and student F are structure ,ariables o- the ty+e indicated by the structure tag"

/TR0#T0R) 'A'T'A('/AT'%A*$ /truct student < char name4FN5= int rno= int sub= -loat mar6s= ?= A ,ariable o- this structure can initialiDed during its declaration as shown below" /truct student student 1T<AAA=HF!N8F89!"N?=

-39 -

C Language
/TR0#T0R) 7'T; 'A /TR0#T0R)*$ struct date < int day= int month= int year= ?= struct +erson < char name_FN`= struct date birthday= -loat salary= ?= The embaded structure ty+e 4date5 must be declared be-ore its use with in the containing structure" This is because8 only legal data ty+es are allowed in the structure declaration" %P)RAT'%A/ %A /TR0#T0R)*$ # +ro,ides the +eriod or dot4"5 o+erator to access the members o- a structure inde+endently" The dot o+erator connects the member with the structure ,ariable" This can be re+resented as struct ,ar" member ,ar= ;er struct ,ar is a structure ,ariable and member ,ar is on is one oits member" The dot o+erator must ha,e a structure ,ariable on its le-t and a legal member name on its right" /truct +erson < char name _FN`= int age= -loat salary= ? em+rec= em+rec is a structure ,ariable o- ty+e +erson and name8 age8 salary are the members o- the structure" )m+rec" Aame will access em+recs name" )m+rec" Age will access em+recs age" ARRAJ %9 /TR0#T0R)*$

-40 -

C Language
The array will ha,e indi,idual structures as its elements" /imilar to declaring structure ,ariable" /truct +erson < char name_FN`= struct date= < int day8 month8 year= ? birthday = -loat salary = ? em+rec_1!`= em+rec is an array o- 1! +erson structures" )ach element o- the array em+rec will contain the structure o- ty+e +erson" The +erson structure consists o- three indi,idual members" An array name8 salary and another structure data" em+rec_O`"name access the name o- Nth structure" TJP) D)9 /TAT)&)AT*$ Ty+e de- statement is used -or de-ining new data ty+es in,ol,ing structures" A new data ty+e re+resenting the structure is declared using the ty+e de- 6ey word" Ty+e de- struct student struct < int roll no = int sub\ect = int mar6s = ? student = ;ere student struct is the tag" /tudent is the name o- the new ty+e" /tudent student 18 student F= /tudent 18 student F are structure ,ariables" /TR0#T0R) %9 90A#T'%A/ *$ A structure ,ariable can be +assed to a -unction in the same manner as that o- an ordinary ,ariable" in the -unction call statement we s+eci-y the structure ,ariable as an argument" 7hen we +ass a structure ,ariable to a -unction the entire contents o- the structure ,ariable will be +assed to the -unction" The structure ,ariable declare in the -ormal

-41 -

C Language
+arameter must belong to the same structure as that o- the actual ,ariable" 7hen e,er we +ass ,ariable to a -unction the structure must be declared global8 this is because the structure de-inition must be a,ailable to all the -unctions in the +rogram"

0A'%A/*$
A union is a ty+e in c which allows the o,er lay o- more than one ,ariable in the same memory area" 0nion is similar to a structure" The only change in the declaration is the substitution o- the 6ey word 0A'%A -or the 6ey word /TR0#T" 0nion _2union ty+e name3` < 2 ty+e 3 2,ariable name3= BBB"" BBB"" ?_2 union ,ariable3`= All ,ariables inside a union share storage s+ace" The com+ailer will allocate su--icient storage -or the union ,ariable to accomidate the largest element in the union" %ther elements o- the union use the same s+ace indi,idual ,ariables in a union occu+y the same location in the memory" Thus writing into one will o,erwrite the other" )lements o- a union or accessed in the same manner as the elements o- a structure" D'99)R)A#) B)T7))A /TR0#T0R) AAD 0A'%A*$ The amount o- memory reEuired to store a structure ,ariable is the sum o- siDes o- all the members in addition to the +adding bytes that may be +ro,ided by the com+ailer" 'n case o- a union8 the amount o- memory reEuiring is the same as that reEuired by its largest member" /'C) %9 4 5 %P)RAT%R*$ 't is also called com+ailer o+erator or com+ile ty+e o+erator" 't dis+lays no o- bytes co,ered by a ,ariable or an e.+ression" The synta. o- siDe o- o+erator is nTsiDe o- 4,5= ;ere n should be o- integer ty+e"

-42 -

C Language
;ere , is ,ariable" TJP) %P)RAT%R*$ Ty+e o+erator is used -or con,ersion +ur+ose" /o it is called as con,ert o+erator" This o+erator con,erts -loat ty+e data in integer -rom con,erts -loat ty+e data in integer -rom and ,ice ,ersa" /o it is also used -or casting a ,alue and +rocess to con,ert one -rom to another is called casting" she synta. o- ty+e o+erator is 4ty+e 5, or e 7here , is ,ariable" e is an e.+ression"

9'()/*$
A -ile is a grou+ o- data items or in-ormation stored some way on the secondary storage de,ice" To store the data gi,en to a +articular +rogram on a secondary storage de,ice we ha,e to use the conce+t o- 9'()/" %nce we store the data +ermanently that data can be used -rom the -iles when e,er necessary" To do -ile o+erations we must declare a +ointer o- ty+e 9'()" This is because a -ile can be accessed only through its +ointer" /ynta.* 9ile R +ointer= ).* 9ile R -+= The o+erations that can be +er-orm on a -ile or reading and writing" To +er-orm any o+erations on a -ile we ha,e to -ollow the -ollowing ste+s" i" select a sutiable data structure -or the -ile " ii" select a sutiable -or the -ile and o+en it" iii" Per-orm the o+eration on the -ile" i," #lose the -ile" %P)A'AK A 9'() *$ To o+en a -ile we ha,e the -unction -o+en4 5= whose synta. is as shown below" This -unction o+ens the s+eci-ied -ile and returns a +ointer i- the -ile has been success-ully o+ened" %therwise it returns a null ,alue" /ynta.* 9ile +ointerT -o+en 4-ile name8mode5=

-43 -

C Language
'n the abo,e synta. -ile name is the name o- the -ile which is to be o+ened" &ode is the +ur+ose -or which we want to o+en the -ile" The di--erent -ile o+ening modes are 78 r8 a8 wP8 rP8 aP" 7 *$ %+ens a -ile -or writing only" '- the -ile e.ists the contents in the 9'() will be deleted and the new contents will be return to the 9'()" '- the 9'() doesnt e.ist a new 9'() will be created" R *$ %+ens a -ile only -or reading +ur+ose" A *$ %+ens a -ile in a++end mode" This means that we can add contents to a -ile -rom the end o- that -ile" 7hen a -ile is o+ened in this mode the e.isting data will remain the same and the new data will be added -rom the end" '- the 9'() doesnt e.ists a new -ile will be created" 7P*$ 'dentical to w" but allows both reading and writing o+erUations" RP *$ %+ens a -ile -or reading and writing" AP *$ %+ens a -ile in a++end mode and allows reading also" R)AD'AK 9R%& A 9'() *$ To read ,alues -rom a -ile" 7e ha,e the -unction - scan-4 5=" /ynta.* -scan-4-ile +ointer8control string8U ,ariable list5= in the abo,e synta. -ile +ointer s+eci-ies the 9'() -rom which data is to be read" #ontrol string and ,ariable list are same as in scan-4 5= e.* -scan-4-+8 SdSsSd8Uem+"id8em+"name8Uem+"sal5= 7R'T'AK A 9'()* $ To write a -ile 8we ha,e the -unction -+rint- 4 5= /JATA: *$ 9+rint- 4-ile +ointer8control string8U ,ariable list5= 'n the abo,e synta. -ile +ointer s+eci-ies the -ile where the contents or to be written" #ontrol string and ,ariable list are same as that in +rint-4 5= ).* +rint-4-+8nSd]nSd]nSs8 em+"id8em+"sal8em+"name5=

-44 -

C Language
#(%/'AK A 9'()*$ To close a -ile we ha,e a -unction called -close4 5= this -unction ta6es one argument which is a -ile +ointer" This -unction close the -ile which is associated with the s+eci-ied -ile +ointer" /ynta.* 9close4-ile +ointer5= ).* 9close 4-+5= D)T)#T'AK )AD %9 9'()*$ To detect the end o- the -ile we ha,e the -eo- 4 5" This -unction returns n true ,alues i- it has reached the end o- the -ile otherwise it will return a -alse ,alue" /ynta.* 9eo- 4-ile +ointer5= 9ile +ointer s+eci-ies the -ile -or which we want to detect" RAAD%& A##)// %9 9'()*$ This means locating the -ile +ointer at a desired +osition in the -ile" To achie,e this tas6 we ha,e the -see64 5= /JATA:* 9see6 4-+8 no8 o-by8 +osition5= 9'() P%'AT)R*$ This +arameter s+eci-ies the -ile +ointer which we want to mo,e" This +arameter s+eci-ies the no o- bytes by which we want to mo,e the -ile +ointer" '- we s+eci-y a +ositi,e ,alue -ile +ointer will be mo,e in the -orward direction" '- we s+eci-y the negati,e ,alue it will mo,e in the bac6 ward direction" 9tell4 5= *$ This -unction returns the current +osition o- the 9'() +ointer in the 9'()8 i"e" no o- bytes it is away -rom the starting +oint o- the 9'()" /ynta.*$ @arT-tell 4-ile +ointer5=

-45 -

C Language
'n the abo,e synta. 9'() +ointer is the +ointer whose current +osition is to be 6nown" /ince this -unction returns a long int ty+e o- ,alue the ,ariable should belong to long int data ty+e" ).*$ :T-tell 4-+5=

)A0&)RAT)D DATA TJP)*$


)numerated data ty+e is the data ty+e de-ined by the user it allows the user to de-ine this own data ty+e" To declare the enumerated the enumerated data ty+e we use the 6ey word enum" /ynta.*$ )num name <constant?= ).*$ )num colour <red8 green8 blue?

-46 -

You might also like