C Handbook
C Handbook
C Handbook
Chapter 1
Introduction to C Programming
Topics Covered
Objectives
Why use C
Uses of C
A Brief History of C
C for Persona Computers
The !dit"Compie"#in$"!%ecute Process
!diting
Compiing
#in$ing
!%ecutabe &ies
Using 'icrosoft C
!dit stage
Compie and in$
!%ecute
!rrors
LCC - Quality Computer Education (Since 1998) 1
C Programming Handbook
Introduction to C Programming
Objectives
!i" "ection i" de"igned to gi#e you a general o#er#ie$ o% t!e C programming language&
'lt!oug! muc! o% t!i" "ection $ill be e(panded in later "ection" it gi#e" you a ta"te o%
$!at i" to come&
Why use C(
C !a" been u"ed "ucce""%ully %or e#ery type o% programming problem imaginable %rom
operating "y"tem" to "pread"!eet" to e(pert "y"tem" - and e%%icient compiler" are a#ailable
%or mac!ine" ranging in po$er %rom t!e 'pple )acinto"! to t!e Cray "upercomputer"&
!e large"t mea"ure o% C*" "ucce"" "eem" to be ba"ed on purely practical con"ideration"+
!e portability o% t!e compiler,
!e "tandard library concept,
' po$er%ul and #aried repertoire o% operator",
'n elegant "ynta(,
-eady acce"" to t!e !ard$are $!en needed,
!e ea"e $it! $!ic! application" can be optimi.ed by !and-coding i"olated
procedure"
C i" o%ten called a /)iddle Le#el/ programming language& !i" i" not a re%lection on it"
lack o% programming po$er but more a re%lection on it" capability to acce"" t!e "y"tem*"
lo$ le#el %unction"& )o"t !ig!-le#el language" (e&g& 01--'2) pro#ide e#eryt!ing t!e
programmer mig!t $ant to do already built into t!e language& ' lo$ le#el language (e&g&
a""embler) pro#ide" not!ing ot!er t!an acce"" to t!e mac!ine" ba"ic in"truction "et& '
middle le#el language3 "uc! a" C3 probably doe"n*t "upply all t!e con"truct" %ound in
!ig!-language" - but it pro#ide" you $it! all t!e building block" t!at you $ill need to
produce t!e re"ult" you $ant4
Uses of C
LCC - Quality Computer Education (Since 1998) 5
C Programming Handbook
C $a" initially u"ed %or "y"tem de#elopment $ork3 in particular t!e program" t!at make-
up t!e operating "y"tem& 6!y u"e C7 )ainly becau"e it produce" code t!at run" nearly a"
%a"t a" code $ritten in a""embly language& Some e(ample" o% t!e u"e o% C mig!t be+
1perating Sy"tem"
Language Compiler"
'""embler"
e(t Editor"
Print Spooler"
2et$ork 8ri#er"
)odern Program"
8ata 9a"e"
Language :nterpreter"
;tilitie"
:n recent year" C !a" been u"ed a" a general-purpo"e language becau"e o% it" popularity
$it! programmer"& :t i" not t!e $orld*" ea"ie"t language to learn and you $ill certainly
bene%it i% you are not learning C a" your %ir"t programming language4 C i" trendy (: nearly
"aid "e(y) - many $ell e"tabli"!ed programmer" are "$itc!ing to C %or all "ort" o%
rea"on"3 but mainly becau"e o% t!e portability t!at $riting "tandard C program" can o%%er&
A Brief History of C
C i" a general-purpo"e language $!ic! !a" been clo"ely a""ociated $it! t!e ;2:<
operating "y"tem %or $!ic! it $a" de#eloped - "ince t!e "y"tem and mo"t o% t!e program"
t!at run it are $ritten in C&
)any o% t!e important idea" o% C "tem %rom t!e language 9CPL3 de#eloped by )artin
-ic!ard"& !e in%luence o% 9CPL on C proceeded indirectly t!roug! t!e language 93
$!ic! $a" $ritten by =en !omp"on in 19>? at 9ell Lab"3 %or t!e %ir"t ;2:< "y"tem on a
8EC P8P->& 9CPL and 9 are /type le""/ language" $!erea" C pro#ide" a #ariety o% data
type"&
:n 19>5 8enni" -itc!ie at 9ell Lab" $rite" C and in 19>8 t!e publication o% !e C
Programming Language by =ernig!an @ -itc!ie cau"ed a re#olution in t!e computing
$orld&
LCC - Quality Computer Education (Since 1998) A
C Programming Handbook
:n 198A3 t!e 'merican 2ational Standard" :n"titute ('2S:) e"tabli"!ed a committee to
pro#ide a modern3 compre!en"i#e de%inition o% C& !e re"ulting de%inition3 t!e '2S:
"tandard3 or /'2S: C/3 $a" completed late 1988&
C for Persona Computers
6it! regard" to per"onal computer" )icro"o%t C %or :9) (or clone") PC*"& and 9orlandB"
C are "een to be t!e t$o mo"t commonly u"ed "y"tem"& Ho$e#er3 t!e late"t #er"ion o%
)icro"o%t C i" no$ con"idered to be t!e mo"t po$er%ul and e%%icient C compiler %or
per"onal computer"&
The !dit"Compie"#in$"!%ecute Process
8e#eloping a program in a compiled language "uc! a" C reCuire" at lea"t %our "tep"+
1& Editing (or $riting) t!e program
5& Compiling it
A& Linking it
D& E(ecuting it
!diting
Eou $rite a computer program $it! $ord" and "ymbol" t!at are under"tandable to !uman
being"& !i" i" t!e editing part o% t!e de#elopment cycle& Eou type t!e program directly
into a $indo$ on t!e "creen and "a#e t!e re"ulting te(t a" a "eparate %ile& !i" i" o%ten
re%erred to a" t!e "ource %ile (you can read it $it! t!e EPE command in 81S or t!e cat
command in ;2:<)& !e cu"tom i" t!at t!e te(t o% a C program i" "tored in a %ile $it! t!e
e(ten"ion &c %or C programming language
Compiing
Eou cannot directly e(ecute t!e "ource %ile& o run on any computer "y"tem3 t!e "ource
%ile mu"t be tran"lated into binary number" under"tandable to t!e computer*" Central
Proce""ing ;nit (%or e(ample3 t!e 8?F8> microproce""or)& !i" proce"" produce" an
intermediate obGect %ile - $it! t!e e(ten"ion &obG3 t!e &obG "tand" %or 1bGect&
#in$ing
LCC - Quality Computer Education (Since 1998) D
C Programming Handbook
!e %ir"t Cue"tion t!at come" to mo"t people" mind" i" $!y i" linking nece""ary7 !e
main rea"on i" t!at many compiled language" come $it! library routine" $!ic! can be
added to your program& !e"e" routine" are $ritten by t!e manu%acturer o% t!e compiler to
per%orm a #ariety o% ta"k"3 %rom inputHoutput to complicated mat!ematical %unction"& :n
t!e ca"e o% C t!e "tandard input and output %unction" are contained in a library ("tdio&!) "o
e#en t!e mo"t ba"ic program $ill reCuire a library %unction& '%ter linking t!e %ile
e(ten"ion i" &e(e $!ic! are e(ecutable %ile"&
!%ecutabe fies
!u" t!e te(t editor produce" &c "ource %ile"3 $!ic! go to t!e compiler3 $!ic! produce"
&obG obGect %ile"3 $!ic! go to t!e linker3 $!ic! produce" &e(e e(ecutable %ile& Eou can t!en
run &e(e %ile" a" you can ot!er application"3 "imply by typing t!eir name" at t!e 81S
prompt or run u"ing $indo$" menu&
Using 'icrosoft C
!dit stage)
ype program in u"ing one o% t!e )icro"o%t 6indo$" editing package"&
Compie and in$)
Select 9uilding %rom )ake menu& 9uilding option allo$" you to bot! compile and link in
t!e "ame option&
!%ecute)
;"e t!e -un menu and "elect Io option&
!rrors)
0ir"t error !ig!lig!ted& ;"e 2e(t Error %rom Searc! menu %or %urt!er error" i% applicable&
:% you get an error me""age3 or you %ind t!at t!e program doe"n*t $ork $!en you %inally
run it (at lea"t not in t!e $ay you anticipated) you $ill !a#e to go back to t!e "ource %ile -
t!e &c %ile - to make c!ange" and go t!roug! t!e $!ole de#elopment proce"" again4
LCC - Quality Computer Education (Since 1998) J
C Programming Handbook
Chapter *
+etting ,tarted -ith C
Topics Covered
Characteristics of C
Size of C Language
Structure of a C Program
Variable Declarations
Executable Statements
C Character Set
Identifiers & e!"ords
Data #!pes
Escape Se$uences
Data Input % &utput and C &perators
LCC - Quality Computer Education (Since 1998) K
C Programming Handbook
+etting ,tarted -ith C
Characteristics of C
'll language" !a#e a c!aracteri"tic look& C !a" a #ariety o% language c!aracteri"tic" t!at
are largely a con"eCuence o% t!e original con"tricted de#elopment en#ironment and it"
original application" implementation o% an operating "y"tem&
Some o% t!e mo"t "igni%icant c!aracteri"tic" o% t!e language" are li"ted belo$+
,i.e of C #anguage
C i" an e(ternally "mall language& C doe" not !a#e any built-in inputHoutput capabilitie",
it contain" no "tring !andling %unction" and arit!metic operation" beyond t!o"e "uc! a"
ba"ic addition or "ubtraction& C pro#ide" t!i" %unctionality t!roug! a ric! "et o% %unction
librarie"& )o"t C implementation" include "tandard librarie" %or inputHoutput3 "tring
manipulation" and arit!metic operation"& C i" c!aracteri.ed by t!e ability to $rite conci"e
"ource program"& C program" u"e and e(ten"i#e u"age o% %unction call"& C program" are
!ig!ly portable&
,tructure of a C Program
' C program mu"t !a#e a %e$ ba"ic component"
The main / 0 &unction
!i" i" t!e %unction $it! $!ic! t!e operating "y"tem $ill "tart e(ecution& Eac! and e#ery
C program mu"t !a#e one and only main ( ) %unction de%ined a" %ollo$"&
main ( )
L
Mariable declaration,
Program "tatement",
N
1ariabe 2ecarations
'll #ariable" u"ed in a program mu"t be declared& C #ariable declaration" include t!e
name o% t!e #ariable and it" data type&
LCC - Quality Computer Education (Since 1998) >
C Programming Handbook
!%ecutabe ,tatements
E(ecutable "tatement" mu"t !a#e #ariable declaration", 'n e(ecutable "tatement i" an
e(pre""ion %ollo$ed by a "emicolon or a control con"truct "uc! a" an :% or a 6!ile
"tatement&
)o"t program" and "upporting %unction" contain program comment" $!o"e purpo"e i" to
male t!e program more under"tandable& Program comment" may contain any me""age
"tarting $it! t!e c!aracter "eCuence HF and ending $it! a "eCuence FH&
!%ampe of a simpe C Program
OincludeP"tdio&!Q
main( )
L
print%(R6elcome to t!e $ord o% C programmingSnT),
N
C Character ,et
C u"e" t!e %ollo$ing a" building block" to %orm ba"ic program element" a" con"tant"3
#ariable"3 operator"3 e(pre""ion" etc&
Identifiers 3 4ey-ords
:denti%ier" and name" gi#en #ariou" program element"3 "uc! a" #ariable3 %unction" and
array identi%ier" con"i"t o% letter" and digit"3 in any order3 e(pect t!at t!e %ir"t c!aracter
mu"t be a letter& !e under"core U can be al"o included& 'n identi%ier may al"o begin $it!
an under"core&
!e %ollo$ing name" are e(ample" o% #alid identi%ier"
< E1 SumU1 Utemp
2ame area -ollUno 'ME-'IE
-e"er#ed $ord" called a" key$ord"3 t!at !a#e a "tandard prede%ined meaning in C cannot
be u"ed a" u"er de%ined key$ord" and are u"ed only %or intended purpo"e&
Standard =ey$ord" are gi#en belo$&
Auto if doube struct do ong
signed e%tern typedef return char register
short si.eof brea$ continue voatie goto
static -hie foat union ese s-itch
case enum const void for unsigned
defaut int
LCC - Quality Computer Education (Since 1998) 8
C Programming Handbook
2ata Types
!e data type "upported in a language dictate" t!e type o% #alue"3 $!ic! can be po""e""ed
by t!e language& C "upport" "e#eral di%%erent type" o% data3 eac! o% $!ic! may be
repre"ented di%%erently $it!in t!e computer" memory&
!e data type" "upported by C may be cla""i%ied into
Simple3 Primiti#e or atomic data&
Compound3 Structured or deri#ed data
'n atomic datum i" a %undamental unit o% in%ormation3 $!ic! cannot be broken do$n to
con"tituent part"& 6!erea" a deri#ed data item i" made up o% one or more "imple data
item"& !e li"t gi#en belo$ cla""i%ie" t!e item" a" "imple compound&
S:)PLE :nteger" C!aracter" 0loat" 8ouble"
C1)P1;28 'rray" Structure" ;nion" 9it%ield"
!e ba"ic data type"
2ata type 2escription 'emory 5e6uired
int :nteger Quantity 5 byte"
c!ar Single C!aracter 1 byte
%loat 0loating point number (decimal noB") D byte"
double 8ouble preci"ion %loating point no 8 byte"
more "igni%icant %igure and an e(ponent
$!ic! may be larger in magnitude
9a"ic data type can be augmented by u"ing t!e data type Cuali%ier "!ort3 long3 "igned and
un"igned& !e memory reCuirement" %or eac! data type may #ary %rom one C compiler to
anot!er&
Integers
!e range o% #alue" t!at can be "tored in #ariou" %orm" o% integer" i" al"o di%%erent a"
"ummari.ed belo$
Type Bytes 5e6uired 5ange
"!ort int 5 - A53>K8 to V A53>K>
int 5 - A53>K8 to V A53>K>
LCC - Quality Computer Education (Since 1998) 9
C Programming Handbook
long int D -531D>3D8A 3KD8 to V 531D>3D8A3 KD>
un"igned "!ort int 5 ? to V KJ3JAJ
un"igned int 5 ? to V KJ3JAJ
un"igned long int D ? to VD3593D93K>359J
Characters
C!ar i" a data type3 $!ic! i" u"ed to repre"ent indi#idual c!aracter"& !e c!ar type $ill
generally reCuire one byte o% memory& ' c!ar data type may !a#e identi%ier" "igned and
un"igned&
Type Bytes 5e6uired 5ange
Signed C!ar 1 -158 to V15>
;n"igned C!ar 1 ? to 5JJ
&oats
0loating point data type "tored numerical #alue" $it! a %ractional portion& !ere are t$o
type" o% %loating data named %loat and double& !e"e may al"o !a#e t!e Cuali%ier long&
Long %loat may be eCui#alent to double and long double may be eCui#alent to double3 or
it may re%er to a "eparate3 Re(tra largeT double preci"ion data type reCuiring more t!an 8
byte" o% memory& 0loating-point number" are e""entially "igned&
Type 2escription ,i.e 5ange
0loat Single Preci"ion D A&DE W A8 to A&DE V A8
8ouble 8ouble Preci"ion 8 1&>E W A?8 to 1&>EA?8
Long double E(tended Preci"ion ? 1&>E W A?8 to 1&>EA?8
Constants
C !a" %our ba"ic type o% con"tant"
:nteger con"tant"
0loating-point con"tant"
C!aracter con"tant"
String con"tant"
LCC - Quality Computer Education (Since 1998) 1?
C Programming Handbook
Integer Constants
'n integer con"tant i" an integer-#alued number& !u"3 it con"i"t" o% a "eCuence o%
number"& :nteger con"tant" can be $ritten in t!ree di%%erent number "y"tem+ decimal (ba"e
1?)3 octal (ba"e 8) and !e(adecimal (ba"e 1K)3 binary (ba"e 5)&
' decimal integer con"tant can con"i"t o% any combination o% digit" taken %rom t!e "et ?
t!roug! 9& E(ample" o% #alid decimal integer con"tant" are+ 7 8 9:; ;*9<9
1ctal integer" con"tant can digit" taken %rom t!e "et ? t!roug! >& !e %ir"t digit mu"t be ?
in order to identi%y t!e con"tant a" an octal number& E(ample o% #alid octal integer
con"tant" are+ 78 79:; 799999
' !e(adecimal integer con"tant mu"t begin $it! eit!er &?( or ?(& :t can t!en be %ollo$ed
by any combination o% digit" taken %rom t!e "et" ? t!roug! 9 and ' t!roug! 0& E(ample"
o% #alid !e(adecimal integer con"tant" are + 7% 7%8 7%9&&& 7%abcd=
!e magnitude o% an integer con"tant can range %rom .ero to "ome ma(imum #alue& '
typical ma(imum #alue %or mo"t per"onal computer" and may microcomputer" i" A5>K>
in decimal notation t!i" i" eCui#alent to >>>>> octal or >%%% !e(adecimal&
'n un"igned integer con"tant can be identi%ied by appending t!e letter ; or u to t!e end
o% t!e con"tant& E(ample" are+ J?????; (decimal un"igned)3 ?>>>>>>; (octal
un"igned)3 ?(J????; (!e(adecimal un"igned)&
Long integer con"tant" can be identi%ied by t!e letter L or l at t!e end o% t!e con"tant& 0or
e%ampe 8*;:><9?@# /decima ong0A 78*;:><# /Octa ong0=
'n un"igned long integer may be "peci%ied by appending t!e letter" ;L to t!e end o% t!e
con"tant"& E(ample" are3 15ADJK>8;L (decimal un"igned long)3 1<00000;L
(!e(adecimal un"igned long)&
&oating B point constants
' %loating-point con"tant" i" decimal number t!at repre"ent" a "igned real number& !e
repre"entation o% a %loating-point con"tant an integer portion and a decimal point or an
e(ponent or bot!)& E(ample o% #alid %loating W point con"tant" are3
LCC - Quality Computer Education (Since 1998) 11
C Programming Handbook
1& ?&5 85>&K?5
J???? ?&???DA> 15&A
5E8 ?&??Ke-A 1&KKK>E V8&1515151e15
0loating- point con"tant" !a#e a muc! greater range t!an integer con"tant"& ypically3 t!e
magnitude o% a %loating-point con"tant mig!t range %rom a minimum #alue" A&DE-A8 to a
ma(imum o% A&DEVA8&
0loating W point con"tant" are o% t!e type %loat3 double are long& ' %loating-point con"tant
$it!out an %3 03 l3 or L "u%%i( i" o% type o% double& :% t!e letter % or 0 i" t!e "u%%i(3 t!e
con"tant !a" type %loat& :% t!e letter l or L i" t!e "u%%i(3 it i" o% t!e type long double& 0or
e(ample+
1??L HF Ha" type long double FH
1?? 0 HF Ha" type %loat FH
1?? 8 HF Ha" type double FH
!e preci"ion o% %loating W point con"tant" (i&e& t!e number o% "igni%icant %igure") $ill
#ary %rom one #er"ion o% C to anot!er&
Character Constants
' c!aracter con"tant i" a "ingle c!aracter3 enclo"ed in "ingle Cuotation mark"& E(ample"
o% c!aracter con"tant" are 3 X'B3 X(B 3 XAB3 XYB&
C!aracter con"tant" !a#e integer #alue" t!at are determined by t!e computerB" particular
c!aracter "et& )o"t computer" make u"e o% t!e 'SC:: c!aracter "et3 in $!ic! eac!
indi#idual c!aracter i" numerically encoded& 0or e(ample3
Constant 1aue
X'B KJ
X(B 15?
XAB J1
XYB AK
X B A5
LCC - Quality Computer Education (Since 1998) 15
C Programming Handbook
!scape ,e6uences
Certain 'SC:: c!aracter" are unprintable3 $!ic! mean" t!ey are not di"played on t!e
"creen or printer& !o"e c!aracter" per%orm ot!er %unction" a"ide %rom di"playing te(t&
E(ample" are back"pacing3 mo#ing to a ne$ line3 or ringing a bell& 6orking $it! C3 you
!a#e already $orked $it! Sn and St e"cape "eCuence"& E"cape "eCuence" u"ually con"i"t o%
a back"la"! and a letter or a combination o% digit"& o repre"ent t!e creation o%
nonprinting c!aracter "uc! a" a ne$ line c!aracter3 "ingle Cuotation mark3 or certain ot!er
c!aracter "uc! a" R3 X 3 7 and S e"cape "eCuence can be u"ed& '" e"cape "eCuence i"
regarded a" a "ingle c!aracter and i" t!ere%ore #alid a" a c!aracter con"tant& E"cape
"eCuence are typically u"ed to "peci%y action" "uc! a" carriage return and tab mo#ement"
on terminal" and printer"& !e commonly u"ed e"cape "eCuence" are li"ted belo$&
!scape ,e6uence 5epresents Ascii 1aue
Sa 9ell (alert) ??>
Sb 9ack"pace ??8
S% 0orm 0eed ?15
Sn 2e$ Line ?1?
Sr Carriage -eturn ?1A
St Hori.ontal tab ??9
S# Mertical ab ?11
SS 9ack"la"! ?95
S? 2ull ???
:% a back"la"! precede" a c!aracter t!at doe" not apper in t!e abo#e table3 t!e compiler
!andle" t!e unde%ined c!aracter" a" t!e c!aracter it"el% and t!u" t!e may be unpredictable&
S - : 2 I S?
C!aracter "tring" ' c!aracter "tring con"tant con"i"t o% any number o% con"ecuti#e
c!aracter" (including none) enclo"ed in double Cuotation mark"& E(ample o% "tring
con"tant" are RgreenT3 R6a!"!ington38&C&5???JT3TY19&9JT
Since a "tring can be o% any lengt! t!e end o% t!e "tring i" marked $it! t!e "ingle
c!aracter RS?T t!e null c!aracter3 !a#ing an 'SC:: #alue o% ?& 2ote t!at a c!aracter
con"tant X'B and t!e corre"ponding "ingle c!aracter "tring con"tantB"T are not eCui#alent&
LCC - Quality Computer Education (Since 1998) 1A
C Programming Handbook
'l"o a c!aracter con"tant !a" an eCui#alent integer #alue& ' "ingle c!aracter "tring
con"tant con"i"t" o% t!e t$o c!aracter", t!e "peci%ied c!aracter %ollo$ed by t!e null
c!aracter(S?)& 0or e(ample3 t!e "tring TS-:2IT $ould be internally "tored a"& Here eac!
bo( repre"ent" a memory location containing one c!aracter& !e "tring RS-:2IT $ould
be reCuire > element", "i( %or t!e "tring and one %or t!e null c!aracter&
1ariabes
' #ariable i" an identi%ier u"ed to repre"ent "ome "peci%ied type o% in%ormation& ' #ariable
repre"ent" a "ingle data item t!at i" a numerical Cuantity or a c!aracter con"tant etc& !e
data item can t!en be acce""ed later in t!e program "imple by re%erring to t!e #ariable
name& !e in%ormation repre"ented by t!e #ariable can c!ange during t!e e(ecution o% t!e
program by a""igning di%%erently data item" at #ariou" place" $it!in t!e program& 9ut t!e
data type a""ociated $it! t!e #ariable cannot c!ange&
1ariabe 2ecaration
9e%ore a #ariable i" u"ed in a C Program3 it mu"t be declared& !i" in#ol#e" noti%ying t!e
compiler o% t!e #ariableB" name and type a" %ollo$"+ type name, 6!ere can be i% t!e
#ariable i" going to !old an integer #alue3 c!ar i% t!i" to be a c!aracter #ariable3 or %loat i%
t!i" i" a %loating point #ariable& !e %ollo$ing i" an e(ample o% #ariable declaration+ int
#arUname, in t!e abo#e e(ample t!e name o% t!e #ariable i" #arUname and it i" o% type int&
Se#eral #ariable" o% t!e "ame type can be declared in one "tatement a" $ell& 0or e(ample +
int count3 %lag3 "tate, 2ote t!at eac! o% t!e"e #ariable" $ill be o% type int&
:nteger-type #ariable" can al"o be declared to be un"igned& 0or e(ample3 un"igned (3y on
t!e ot!er !and 3 an un"igned long integer can be declared a" %ollo$"3 un"igned long p3C,
Ii#en belo$ are e(ample" i% declaring #ariable" data type"&
%loat root13root5,
c!ar %lag3 te(t Z8?[,
double %actor,
:n t!e abo#e e(ample& Xe(tB i" c!aracter type array& :t can "tore a c!aracter "tring o%
ma(imum lengt! >9 c!aracter"& !e 8?
t!
c!aracter i" %or t!e delimiter3 XS?B& :nitial #alue"
LCC - Quality Computer Education (Since 1998) 1D
C Programming Handbook
can be a""igned to #ariable" $it!in a type declaration& o do "o3 t!e declaration mu"t
con"i"t o% a data type3 %ollo$ed by a #ariable name3 an eCual "ign (\) and a con"tant o% t!e
appropriate type& 0or e(ample+
int c \ 15,
c!ar "tart \ XYB
%loat "um \ ?&?
on t!e ot!er !and 3 a c!aracter type array can be initiali.ed $it!in declaration a" %ollo$"3
c!ar te(tZ11[ \ Xabcde%g!iGkT
!%pressions
'n e(pre""ion repre"ent" a "ingle data item3 "uc! a" a number or a c!aracter& !e
e(pre""ion may con"i"t o% a "ingle entity3 "uc! a" a con"tant3 a #ariable or a re%erence too
a %unction& :t may al"o con"i"t o% "ome combination o% t!e abo#e entitie" interconnected
by one or more operator" are+
>
15A&DJ
XaB
#ar
#ar \ #ar V 1K
print%(R!elloSnT)
,tatements
' "tatement cau"e" t!e computer to carry out "ome action& !ere are t$o type" o%
"tatement" in t!e C language3 "imple and compound&
Simple "tatement" (e(pre""ion "tatement") Con"i"t" o% an e(pre""ion %ollo$ed by a
"emicolon& 0or e(ample
main ( )
L
int (3y,
(\yVA,
N
LCC - Quality Computer Education (Since 1998) 1J
C Programming Handbook
!e %ir"t line $it!in t!e body o% t!i" program declare" t$o integer #ariable" ( and y& !e
e(ecutable portion o% t!e e(ample i" made up o% "e#eral e(pre""ion" are t!e #alue" A3 y 3
t!e e(pre""ion yVA and t!e a""ignment ( \ yVA&
' compound "tatement con"i"t o% "e#eral indi#idual "tatement" enclo"ed $it!in a pair
brace" L and N& !e indi#idual "tatement" may t!em"el#e" be e(pre""ion "tatement or
compound "tatement"& !i" collection o% "tatement" i" treated a" a "ingle "tatement by C
compiler& 'n e(ample o% compound "tatement i" "!o$n belo$+
L
pi \ A&1D,
circum%erence \ 5 F pi F radiu",
area \ pi F radiu" F radiu",
N
!e compound "tatement pro#ide" capability %or embedding "tatement" $it!in ot!er
"tatement"&
2ata Input C Output and C Operators
:nput H 1utput %unction" are u"ed to accept #alue" into #ariable" and printing t!em a%ter
t!e proce""ing i" o#er& !e input and out o% data can be done t!roug! t!e "tandard
inputHoutput media or t!roug! %ile"& !e "tandard input medium i" t!e keyboard3 $!erea"
t!e "tandard output medium i" t!e con"ole&
!e C language i" accompanied by a collection o% !eader %ile" t!at pro#ide nece""ary
in%ormation& Eac! !eader %ile contain" in%ormation in "upport o% a group o% related library
%unction"& !e"e %ile" are included in t!e program by gi#ing t!e Oinclude "tatement at t!e
beginning o% t!e program& !e !eader %ile reCuired by t!e "tandard inputH output library
%unction" i" called "tdio&!&
'n input H output %unction can be acce""ed %rom any$!ere $it!in a program "imply by
$riting t!e %unction name3 %ollo$ed by a li"t o% argument" enclo"ed in parent!e"e"& !e
argument" repre"ent data item" t!at are "ent to t!e %unction&
LCC - Quality Computer Education (Since 1998) 1K
C Programming Handbook
Some inputHoutput %unction" return data item $!erea" ot!er donBt& !e %unction" returning
data item" may appear $it!in e(pre""ion"3 a" t!oug! eac! %unction re%erence $ere an
ordinary #ariable %or e(ample c \ getc!ar ( ) & 1n t!e ot!er !and3 %unction" not returning
data item" may be re%erenced a" t!oug! t!ey $ere "eparate "tatement"3 %or e(ample
putc!ar(&&),
!e C library contain" t$o di"tinct "y"tem" o% routine" t!at !andle inputH output
operation"& !ey are
9u%%ered :H1&
;nbu%%ered :H1&
!e bu%%ered :H1 i" u"ed to read and $rite 'SC:: c!aracter" $!erea" t!e unbu%%ered :H1 i"
u"ed to read and $rite binary data&
' bu%%er i" a temporary a "torage area3 eit!er in t!e memory3 or on t!e controller card %or
t!e de#ice& :n bu%%ered :H13 c!aracter" typed at t!e keyboard are collected until t!e u"er
pre""e" t!e -E;-2 or t!e E2E- key& !e c!aracter" are made a#ailable to t!e
program a" a block&
9u%%ered :H1 can be %urt!er "ubdi#ided into con"ole :H1 and 9u%%ered 0ile :H1& Con"ole
:H1 re%er" to operation" t!at occur at t!e keyboard and t!e "creen o% your computer&
9u%%ered %ile :H1 re%er" to operation" t!at are per%ormed to read and $rite data onto a %ile&
:n unbu%%ered :H13 t!e c!aracter3 $!ic! t!e u"er enter" at t!e keyboard3 i" made a#ailable
to t!e program immediately $it!out being "tored temporarily in a bu%%er location& :t i" not
nece""ary to pre"" t!e E2E- key to regi"ter t!e c!aracter&
' bu%%ered "y"tem i" pre%erred o#er an unbu%%ered "y"tem mainly %or t!e %ollo$ing
rea"on"&
1& 'n :H1 operation on a bu%%ered "y"tem i" le"" time con"uming3 "ince a block o%
c!aracter" can be tran"mitted at one time&
5& :n t!e e#ent $!en mi"take" are made and t!e return key i" not pre""ed3 t!e
mi"take" can be corrected&
LCC - Quality Computer Education (Since 1998) 1>
C Programming Handbook
:n "ome ca"e" !o$e#er3 unbu%%ered :H1 i" de"ired in interacti#e program"& ' $ord
proce""ing i" an e(ample&
,inge Character Input C Output &unctions
!e IECH'- ( ) 0unction
;"ing t!e C library %unction W getc!ar( )3 one i" allo$ed to input a "ingle c!aracter %rom
t!e terminal& !i" %unction i" t!e "imple"t input mec!ani"m $!ic! read" c!aracter at a
time& :t return" a "ingle c!aracter t!at i" typed in %rom t!e keyboard& !e %unction doe"
not reCuire any argument t!roug! a pair o% empty parent!e"e" mu"t %ollo$ t!e $ord
getc!ar& ' re%erence to t!e getc!ar %unction i" $ritten a" c!aracter #ariable \ getc!ar ( ) ,
$!ere c!aracter #ariable re%er" to "ome pre#iou"ly declared c!aracter #ariable&
C!ar c, HH declare" a #ariable o% c!aracter type
C \ getc!ar (), HH Cau"e" a "ingle c!aracter to be entered u"ing t!e keyboard
The Putchar/ 0 &unction
!e C library %unction P;CH'- ()3 allo$" t!e u"er to di"play a "tring c!aracter by
c!aracter3 !e putc!ar () %unction i" complement o% t!e getc!ar () %unction& !e putc!ar ()
%unction "imply print" t!e 'SC:: #alue o% t!e e(pre""ion it recei#e" a" an argument"& :t
tran"mit" a "ingle c!aracter to a "tand read out put de#ice i&e& t!e monitor& '
re%erence to t!e putc!ar %unction i" $ritten a" putc!ar( c!aracter #ariable)3$!ere
c!aracter #ariable re%er" to "ome pre#iou"ly declared c!aracter #ariable&
'c program contain" t!e %ollo$ing "tatement"
C!ar c HF 8eclare" c a" a c!aracter- type #ariableFH
]&
]&
Putc!ar(),
HF Cau"e" t!e current #alue o% c to be tran"mitted to t!e "tandered out put
de#ice(con"ole)$!ere it $ill be di"playedFH
LCC - Quality Computer Education (Since 1998) 18
C Programming Handbook
Printf/0
!e P-:20 () %unction3 tran"late" internal #alue" a" c!aracter& !i" %unction con#ert"3
%ormat" and di"play" argument u"ing t!e "tandard output i&e t!e con"ole& !i" %unction can
be u"ed to out put any combination o% numerical #alue"3 c!aracter" and "ting"& !e print%
() %unction i" $ritten a" print%(control "tring3arg13arg53argA]]&3argn),
6!ere control "tring re%er" to a "tring t!at contain %ormatting in%ormation3 and arg13arg53
]3argn are argument" $!ic! repre"ent t!e indi#idual data item to be di"played&
!e control "tring con"i"t" o% t$o typeB" obGect" namely t!e ordinary c!aracter" and t!e
con#er"ion "peci%ication"& 1rdinary c!aracter" are not!ing but t!o"e c!aracter"3 $!ic!
$ill be di"played on t!e "tandard3 output i&e& t!e monitor& !e con#er"ion "peci%ication i"
pre%i(ed by t!e percentage "ign (^) %ollo$ed by a con#er"ion c!aracter& !ere mu"t be
e(actly t!e "ame number o% argument" a" t!ere are %ormat "peci%ier"& !e %ormat
"peci%ier" and t!e argument" are matc!ed in order %rom le%t to rig!t&
2ote+ 9et$een t!e ^ "ign t!e and t!e con#er"ion c!aracter3 t!e %ollo$ing may al"o be
gi#en3 !o$e#er in t!e order "peci%ied& !e li"t i" a" %ollo$"
' minu" "ign& !i" "peci%ie" t!at t!e con#erted argument be le%t Gu"ti%ied&
' number3 $!ic! "peci%ie" t!e minimum %ield $idt!& :% le""er i" input3 t!e #alue
$ill be padded to t!e le%t or rig!t "o a" to make up %or t!e minimum di"play $idt!&
' period ( &) & !i" "eparate" t!e %ield $idt! %rom t!e preci"ion&
' number3 $!ic! "peci%ie" t!e ma(imum di"play lengt! o% t!e "tring in ca"e o% a
c!aracter or number o% digit" a%ter a decimal point in ca"e o% a %loating-point
#alue&
' G i% t!e integer i" to be printed a" "!ort and an i i% t!e "peci%ied a" long&
!%ampe)
OincludeP "tdio&!Q
main( )
L
print%(R^&D%SnT315A&15ADJK>),
print%(R^A&8dSnT31???),
N
Output)
LCC - Quality Computer Education (Since 1998) 19
C Programming Handbook
15A&15AJ
????1???
Chapter '
Contro 3 #ooping ,tatements
Topics Covered
If ,tatement
&orms of If statement
,-itch Case
#oops
Whie #oop
2o Whie #oop
&or #oop
Brea$ ,tatement
Continue ,tatement
LCC - Quality Computer Education (Since 1998) 5?
C Programming Handbook
Contro 3 #ooping ,tatements
If ,tatement
!i" "tatement i" t!e "imple"t o% all deci"ion making "tatement" $!ic! i" implemented
u"ing t$o %orm"&
,ynta%)
&orm 8)
if(condition/expression)
{
Statement block;
}
The above form the if statement checks only for the true condition. f condition/expression
returns true then the statement block is executed.
!%ampe
HH Program to c!eck $!et!er t!e gi#en number i" po"iti#e&
OincludeP"tdio&!Q
#oid main()
L
int no,
clr"cr(),
print%(REnter any 2umberSnT),
"can%(R^dT3@no),
i%(noQ?)
L
print%(R!e gi#en number i" po"iti#eSnT),
N
getc!(),
N
LCC - Quality Computer Education (Since 1998) 51
C Programming Handbook
&orm *)
if(condition/expression)
{
Statement block!;
}
else
{
Statement block";
}
The above form the if statement checks only for both true and false condition. f
condition/expression returns true then the statement block! is executed. f the
condition/expression returns false then the statement block " is executed.
!%ampe
HH Program to c!eck $!et!er t!e gi#en number i" po"iti#e or negati#e
OincludeP"tdio&!Q
#oid main()
L
int no,
clr"cr(),
print%(REnter any 2umberSnT),
"can%(R^dT3@no),
i%(noQ?)
L
print%(R!e gi#en number i" po"iti#eSnT),
N
el"e
L
print%(R!e gi#en number i" negati#eSnT),
N
getc!(),
N
LCC - Quality Computer Education (Since 1998) 55
C Programming Handbook
,-itch ,tatement
!i" i" a multiple branc!ing "tatement $!ere3 ba"ed on a condition3 t!e control i"
tran"%erred to one o% t!e many po""ible point"&
,ynta%)
s#itch (expression)
{
case !$ Statement %lock !;
break;
case "$ Statement %lock ";
break;
case &$ Statement %lock &;
break;
default$ Statement %lock ;
}
!%ampe
HH 'rit!metic 1peration"
OincludeP"tdio&!Q
#oid main()
L
int a3b3c,
int c!oice,
clr"cr( ),
print%(REnter $o :nteger"SnT),
"can%(R^d ^dT3@a3@b),
print%(R1&'dditionSnT),
print%(R5&SubtractionSnT),
print%(RA&E(itSnT),
print%(REnter Eour C!oiceSnT),
"can%(R^dT3@c!oice),
"$itc!(c!oice)
L
ca"e 1+ c \ aVb,
LCC - Quality Computer Education (Since 1998) 5A
C Programming Handbook
print%(R'ddition i" \ ^dSnT3c),
break,
ca"e 5+ c \ a-b,
print%(RSubtraction i" \ ^dSnT3c),
break,
ca"e A+ print%(RPre"" 'ny key to e(itSnT),
break,
de%ault+ print%(R!e c!oice in in#alidT),
N
getc!( ),
N
Loops
Loop" e(ecute "et in"truction" repeatedly %or a certain number o% time"& !ere are t!ree
type" o% loop" in C language& !ey are t!e for loop3 -hie and do -hie loop&
Whie #oop)
#hile (condition)
{
Statement block;
}
'hile loop is called as an entry(check loop. The body of the #hile #ill be executed only if
the condition is true. The control exits the loop once the condition is evaluated to false.
!%ampe
HH Program to print number" %rom 1 to 1?
OincludeP"tdio&!Q
#oid main()
L
int i \ 1,
$!ile(iP\1?)
L
print%(R^dSnT3i),
iVV,
N
getc!(),
N
LCC - Quality Computer Education (Since 1998) 5D
C Programming Handbook
2o Whie #oop
do
{
Statement block;
}
#hile (condition);
)o #hile loop is called as exit(check loop. The statement block #ill *et executed once
and then checks for the condition. f the condition returns true then the do block is
executed once a*ain and so on.
!%ampe
HH Program to print number" %rom 1 to 1?
OincludeP"tdio&!Q
#oid main()
L
int i \ 1,
do
L
print%(R^dSnT3i),
iVV,
N
$!ile(iP\1?),
getc!(),
N
&or #oop
for(initialvalue; test(condition;increment)
{
Statement block;
}
The for loop is an entry controlled loop and us used #hen an action is to be repeated for
a predetermined number of times.
LCC - Quality Computer Education (Since 1998) 5J
C Programming Handbook
!%ampe
HH Program to print number" %rom 1 to 1?
OincludeP"tdio&!Q
#oid main()
L
int i,
%ori \ 1, iP\1?, iVV)
L
print%(R^dSnT3i),
N
getc!(),
N
Brea$ ,tatement
,ynta%)
break;
!e break "tatement and continue "tatement allo$" t!e programmer to e%%ecti#ely alter
t!e pat! t!roug! t!e looping con"truct& !e break "tatement i" u"ed to terminate lop" or
e(it %rom a "$itc! "tatement& :t can be u"ed $it!in a do-$!ile3 %or and "$itc! "tatement&
6!en u"ed $it!in a "$itc!-ca"e "tatement3 control i" tran"%erred to t!e end o% t!e
con"truct& 6!erea"3 in looping con"truct"3 control i" tran"%erred to t!e ne(t "tatement
t!ereby pro#iding an early e(it %rom t!e loop&
!%ampe
OincludeP"tdio&!Q
#oid main( )
L
int ( \ 1,
clr"cr( ),
$!ile (( P \ 1?)
L
print%(R < \ ^dSnT3(),
i%(( \\ J)
break,
(VV,
LCC - Quality Computer Education (Since 1998) 5K
C Programming Handbook
N
getc!( ),
N
Program Output)
< \ 1
< \ 5
< \ A
< \ D
Continue ,tatement
,ynta%)
continue;
!e continue "tatement i" u"ed to bypa"" t!e remainder o% t!e current pa"" t!roug! a loop&
!e loop doe" not terminate $!en a continue "tatement i" encountered& :n%act t!e
remaining "tatement" in t!e loop are "kipped and control i" tran"%erred to t!e ne(t pa""
t!roug! t!e loop.
!%ampe
HH Program to %ind t!e "um o% only po"iti#e noB"
OincludeP"tdio&!Q
#oid main( )
L
int n3i3 "um \ ?,
clr"cr( ),
print%(REnter J 2umber"SnT),
%or(i \ 1 , iP\J, iVV)
L
"can%(R^dT@n),
i%(nP?)
continue,
el"e
"um \ "um V n,
N
getc!( ),
N
LCC - Quality Computer Education (Since 1998) 5>
C Programming Handbook
Chapter :
&unctions
Topics Covered
Introduction to &unctions
&unction 2efinition
Caing &unction
&unction e%ampe -ith no arguments and no returns
&unction e%ampe -ith no arguments and no returns
&unction e%ampe -ith arguments and no returns
&unction e%ampe -ith arguments and -ith returns
LCC - Quality Computer Education (Since 1998) 58
C Programming Handbook
&unctions
Introduction to &unctions
' %unction i" a "el% contained program "egment t!at carrie" out "ome "peci%ic3
$ell-de%ined ta"k& 0unction" break large computing ta"k" into "maller one"& C !a"
been de"igned to make %unction e%%icient and ea"y to u"e& C program" generally
con"i"t o% many "mall %unction" rat!er t!an a %e$ big one"&
!e u"e o% %unction" a#oid" redundant programming o% t!e "ame in"truction" can
be placed $it!in a "ingle %unction3 $!ic! can t!en acce""e" $!ene#er it i" needed&
)oreo#er a di%%erent "et o% data can be tran"%erred to t!e %unction eac! time it i"
acce""e"& C "upport" t!e u"e o% library %unction"3 $!ic! are u"ed to carry out a
number o% commonly u"ed operation" or calculation"& Print%( ) and "can%( ) are
e(ample" o% library %unction"& C al"o allo$" programmer" to $rite t!eir o$n
%unction" %or carrying out #ariou" indi#idual ta"k"&
E#ery C program o% one or more %unction"& 1ne o% t!e"e %unction" mu"t be t!e
main& Program e(ecution $ill al$ay" begin by carrying out t!e in"truction" in
main& ' %unction $ill carry out it" intended action $!ene#er it i" RcalledT %rom
"ome ot!er portion o% t!e program& !e "ame %unction can be acce""e" %rom
di%%erent place" $it!in a program& 1nce t!e %unction !a" carried out" it" intended
action3 control $ill be returned to t!e point %rom $!ic! t!e %unction $a" acce""ed&
&unction 2efinition
return type functioname(list of ar*uments)
{
local variable declarations;
function executable statements;
return (return value);
}
!e return type i" t!e #alue returned by t!e %unction& !e return #alue can be c!ar3
int or %loat& :% t!e %unction doe" not return any #alue3 t!e "peci%ied return #alue
mu"t be #oid& !e %unction name and "et o% argument" (%ormal argument")
"eparated by comma" are enclo"ed in parent!e"e"& !e return type "peci%ication
can be omitted i% t!e %unction return" an integer #alue& !e argument" (%ormal
LCC - Quality Computer Education (Since 1998) 59
C Programming Handbook
argument") allo$ in%ormation to be tran"%erred %rom t!e calling portion o% t!e
program to t!e %unction& !e identi%ier" u"ed a" %ormal argument" are local i&e&
t!ey are not recogni.ed out"ide o% t!e %unction& 'll t!e %ormal argument" are
declared t!e %ollo$ing t!e %ir"t line& Eac! %ormal argument mu"t be o% t!e "ame
data type " t!e data type it recei#e" %rom t!e calling point o% t!e program& !e
body o% t!e %unction de%ine" t!e action to be taken by t!e %unction& !e return
"tatement i" t!e mec!ani"m u"ed %or returning a #alue %rom t!e called %unction to
it" caller& !e return "tatement al"o cau"e" t!e control to t!e returned to t!e point
%rom $!ic! t!e %unction $a" acce""ed& 'ny e(pre""ion can %ollo$ return i&e&
return e(pre""ion&
!e #alue o% e(pre""ion i" returned to t!e calling portion o% t!e program& !e
e(pre""ion $ill be con#erted to t!e return type o% t!e %unction i% nece""ary& '
%unction can return only one #alue to t!e calling portion o% t!e program #ia return&
!ere need be no e(pre""ion a%ter return, in t!at ca"e3 t!e return "tatement "imply
cau"e" control to re#ert back to t!e calling portion& 2o "eparate return "tatement i"
nece""ary to "end back t!e control& 6!en t!e clo"ing brace" L N o% t!e called
%unction i" encountered t!e control i" returned to t!e calling %unction&
Caing &unction
' %unction can be acce""ed (i&e& called) by "peci%ying it" name3 %ollo$ed by a li"t
o% argument" enclo"ed in parent!e"e" and "eparated by comma"& :% t!e %unction
doe"nBt reCuire argument"3 an empty pair o% parent!e"e" mu"t %ollo$ t!e
%unctionB" name&
!e argument" appearing in t!e %unction call can be re%erred to a" actual
argument"& !e actual argument" ay be e(pre""ed a" con"tant"3 "ingle #ariable"3 or
more comple( e(pre""ion"& !e number o% actual argument" mu"t be o% t!e "ame
data type a" it" corre"ponding %ormal argument&
,ynta%)
functionname (list of actual ar*uments);
LCC - Quality Computer Education (Since 1998) A?
C Programming Handbook
!%ampe8
HH 0unction program $it! no argument" and no return"
OincludeP"tdio&!Q
#oid main( )
L
clr"cr( ),
add( ), HH Calling %unction $it! no argument"
N
HH 0unction de%inition
#oid add( ) HH #oid becau"e %unction doe" not return any #alue to t!e calling %unction
L
int a3b3c,
print%(REnter $o :nteger"SnT),
"can%(R^d ^dT3 @a3 @b),
c \ aVb,
print%(R!e addition i" \ ^dSnT3c),
N
!%ampe *
HH 0unction program $it! argument" and no return"
OincludeP"tdio&!Q
#oid main( )
L
clr"cr( ),
int a3b,
print%(REnter $o :nteger"SnT),
"can%(R^d ^dT3 @a3 @b),
add( a3b), HH Calling %unction $it! argument"
N
HH 0unction de%inition
#oid add( (3y) HH #oid becau"e %unction doe" not return any #alue to t!e calling %unction
int (3y,
L
int .,
. \ (Vy,
LCC - Quality Computer Education (Since 1998) A1
C Programming Handbook
print%(R!e addition i" \ ^dSnT3.),
N
!%ampe ;
HH 0unction program $it! argument" and no return"
OincludeP"tdio&!Q
#oid main( )
L
clr"cr( ),
int a3b3c,
print%(REnter $o :nteger"SnT),
"can%(R^d ^dT3 @a3 @b),
c \ add( a3b), HH Calling %unction $it! argument"
print%(R!e addition i" \ ^dSnT3c),
N
HH 0unction de%inition
int add( int (3int y) HH int becau"e %unction doe" an integer #alue to t!e calling %unction
L
int .,
. \ (Vy,
return .,
N
LCC - Quality Computer Education (Since 1998) A5
C Programming Handbook
Chapter (
,torage Casses
Topics Covered
Introduction to ,torage Casses
Automatic 1ariabes
Program !%ampe of Automatic 1ariabes
!%terna 1ariabes C +oba 1ariabes
Program !%ampe of !%terna 1ariabes
,tatic 1ariabes
Program !%ampe of ,tatic 1ariabes
5egister 1ariabe
Program !%ampe of 5egister 1ariabes
LCC - Quality Computer Education (Since 1998) AA
C Programming Handbook
,torage Casses
Introduction to ,torage Casses
!ere are t$o di%%erent $ay" o% c!aracteri.ing #ariable"& 0ir"t t!roug! data type"
and "econdly t!roug! "torage cla""e"& 8ata type re%er" to t!e type o% in%ormation
repre"ented by a #ariable3 e&g& integer-%loating-point or c!aracter& 1n t!e ot!er
!and3 "torage cla"" re%er" to t!e permanence o% a #ariable and it" "cope $it!in t!e
program& !at i" "torage cla"" dictate" !o$3 $!en and $!ere "torage $ill be
allocated %or t!e #ariable& Scope "peci%ie" t!e region o% t!e "ource program $!ere
a #ariable i" kno$n and acce""ible& Clo"ely a""ociated $it! "cope i" t!e notation
o% t!e e(tent o% a #ariable& !e e(tent o% a #ariable i" t!e li%etime o% t!e #ariable&
!i" notion "peci%ie" $!en a #ariable come" into e(i"tence and $!en it e(pire"&
!e %our di%%erent "torage cla""e" are
:& 'utomatic
::& E(ternal H Ilobal Mariable"
:::& Static
:M& -egi"ter
I= Automatic 1ariabes
'utomatic #ariable" are declared $it!in a %unction and are local only to
t!e %unction $!ic! t!ey are con%ined&
'ny #ariable declared in"ide a %unction i" "aid to be automatic&
'utomatic #ariable" do not retain t!eir pre#iou" #alue" $!en acce""ed
again&
Malue" a""igned to automatic #ariable" are lo"t once t!ey are e(ited %rom
t!e %unction&
LCC - Quality Computer Education (Since 1998) AD
C Programming Handbook
'utomatic #ariable" !a#e unpredicted initial #alue" and "!ould al$ay" be
initiali.ed be%ore t!ey are u"ed&
!e key$ord auto i" optional in t!e #ariable declaration&
,ynta%)
auto data type variablename;
Program !%ampe of Automatic 1ariabes
HH Program to %ind t!e %actorial o% a number u"ing %unction"
OincludeP"tdio&!Q
#oid main( )
L
auto int n, HH automatic #ariable belong" to main %unction
clr"cr( ),
print%(REnter 'ny 2umberSnT),
"can%(R^dB3@n),
print%(%act (n)),
getc!( ),
N
int %act (int ()
L
int i3 prod \ 1, HH automatic #ariable"
%or(i \ 1 , iP\(,iVV)
L
prod \ prod F i,
N
return (prod),
N
LCC - Quality Computer Education (Since 1998) AJ
C Programming Handbook
II= !%terna or +oba 1ariabes
E(ternal #ariable" %unction a" global #ariable"& E(ternal #ariable" are not con%ined
to a "ingle %unction&
!eir "cope e(tend" %rom t!e point o% de%inition t!roug! t!e remainder o% t!e
program&
!ey u"ually "pan t$o or more %unction" and o%ten an entire program&
E(ternal #ariable" return t!eir a""igned #alue" till t!e end o% t!e program&
E(ternal #ariable" are normally declared out"ide %unction"&
,ynta%)
extern data type variablename;
Program !%ampe of !%terna 1ariabes
HH Program to %ind t!e %actorial o% a number u"ing %unction"
OincludeP"tdio&!Q
int (3y3., HH E(ternal Mariable 8eclaration
#oid main( )
L
clr"cr ( ),
print%(REnter $o 2umber"SnT),
"can%(R^d ^d R3@(3@y),
mul ((3y),
getc!( ),
N
#oid mul(int (3int y)
L
. \ (Vy,
print%(R!e )ultiplication i" \ ^dSnT3.),
N
Dote) :n t!e abo#e program #ariable ( and y are u"ed in bot! t!e main( ) and mul
%unction" re"pecti#ely& Hence e(ternal or global #ariable" are a#ailable to all %unction" in
a program&
LCC - Quality Computer Education (Since 1998) AK
C Programming Handbook
III= ,tatic 1ariabes
Static #ariable" are "imilar to automatic #ariable" e(cept t!at t!ey retain t!eir
pre#iou" #alue" e#en $!en e(ited %rom t!e %unction to $!ic! t!ey are declared&
!i" %eature allo$" %unction" to retain in%ormation permanently t!roug!out t!e
e(ecution o% t!e program&
' "tatic #ariable i" preceded by a key$ord static=
Static #ariable" are automatically initiali.ed to .ero&
,ynta%)
static data type variablename;
Program !%ampe of ,tatic 1ariabes
CC Program to keep count o% t!e number o% time" o% e(ecution o% a %unction&
OincludeP"tdio&!Q
#oid main( )
L
clr"cr( ),
count( ),
count( ),
count ( ),
getc!( ),
N
#oid count( )
L
"tatic int c ,
cVV,
print% (R0unction E(ecution 2o i" \ ^dSnT3 c),
N
LCC - Quality Computer Education (Since 1998) A>
C Programming Handbook
I1= 5egister 1ariabes
' "ub"et o% regi"ter" i" regi"tered %or regi"ter #ariable"&
' regi"ter declaration ad#i"e" t!e computer t!at t!e #ariable in Cue"tion $ill be
u"ed !ea#ily&
-egi"ter #ariable" are "tored in regi"ter"& Hence regi"ter #ariable" are more ea"ily
acce""ible $!en t!e program i" e(ecuting and t!ere%ore are %a"ter t!an automatic
#ariable"&
!u" t!e"e #ariable" re"ult in "maller and %a"ter program"&
,ynta%)
static data type variablename;
Program !%ampe of 5egister 1ariabes
HH Program to print number" bet$een 1 to n&
OincludeP"tdio&!Q
#oid main( )
L
regi"ter int i \ 1,
clr"cr( ),
%or(i \ 1, iP\n,iVV)
L
print%(R ^dSnT3i),
N
getc!( ),
N
LCC - Quality Computer Education (Since 1998) A8
C Programming Handbook
Chapter )
Arrays
Topics Covered
Introduction
Array 2ecaration
!%ampe of One 2imensiona Array
Initiai.ing Arrays
'utidimensiona Arrays
!%ampe of T-o 2imensiona Array
LCC - Quality Computer Education (Since 1998) A9
C Programming Handbook
Arrays
Introduction
'n array i" a group o% element" (data item") t!at !a#e common c!aracteri"tic" and
"!are a common name& !e element" o% an array are di%%erentiated %rom one an
anot!er by t!eir po"ition"&
Eac! array element i" re%erred to by "peci%ying t!e array name %ollo$ed by it"
"ub"cript enclo"ed in "Cuare bracket"& !e "ub"cript indicate" t!e po"ition o% t!e
particular element $it! re"pect to t!e re"t o% t!e element&
' "ub"cript mu"t be a non negati#e integer&
0or e(ample in t!e array ( o% "i.e n t!e element" are ( Z1[3 ( Z5[]]]]&( Zn-1[&
Single and multidimen"ional array are a#ailable in C language and t!e number o%
"ub"cript" determine t!e dimen"ion" o% t!e array&
Array 2ecaration
'n array i" declared in t!e "ame manner a" t!at o% an ordinary #ariable e(cept t!at
eac! array name mu"t be mentioned by a "i.e "peci%ication& !i" i" reCuired
becau"e t!e C compiler mu"t kno$ t!e number o% "pace" to re"er#e %or t!e array&
' "ingle dimen"ional array i" declared a" %ollo$"+ typeUarrayname Z"i.e[ $!ere
arrayname o% t!e array and "i.e i" t!e no o% element" o% t!e type "peci%ied& !e
"i.e o% t!e array mu"t be an integer con"tant&
!%ampes of array decarations
int mark"Z1??[,
c!ar te(tZ8?[,
%loat nZ1?[,
!e "ub"cript u"ed to declare an array i" "ometime" called a dimen"ion and t!e
declaration %or t!e array i" o%ten re%erred to a" dimen"ioning& !e dimen"ion i"
u"ed to declare an array mu"t al$ay" be a po"iti#e integer con"tant or an
e(pre""ion t!at can be e#aluated to a con"tant $!en t!e program i" compiled&
'n indi#idual element in an array can be re%erred to by mean" o% t!e "ub"cript, t!e
number i" bracket" %ollo$ing t!e array name& ' "ub"cript i" t!e number t!at
"peci%ie" t!e elementB" po"ition in an array&
LCC - Quality Computer Education (Since 1998) D?
C Programming Handbook
:n C language t!e "ub"cript begin" $it! .ero& !u" t!e #alid "ub"cript #alue can
be %rom ? to n -1 &
!%ampe of One 2imensiona Array
HH Program to accept a one dim array and print it&
OincludeP"tdio&!Q
#oid main( )
L
int aZ1??[,
clr"cr( ),
print%(REnter !e Si.e o% t!e array Z)a( W 1??[SnT),
"can%(R^dT3@n),
print%(REnter ^d :nteger"SnT3n),
%or(i\?,iPn,iVV)
L
print% (REnter :nteger ^d+-SnT3iV1),
"can% (R^dT3@aZi[),
N
print% (R'rray Element"SnT),
%or (i\?,iPn,iVV)
L
print%(R:nteger ^d i" \ ^d+-SnT3iV13aZi[),
N
getc! ( ),
N
Initiai.ing Arrays
'n array can be initiali.ed $!en declared by "peci%ying t!e #alue" o% "ome or all
o% it" element"& 'rray" can be initiali.ed at t!e time o% declaration $!en t!eir
#alue" are kno$n in ad#ance&
LCC - Quality Computer Education (Since 1998) D1
C Programming Handbook
!e #alue" to initiali.e an array mu"t be con"tant" ne#er #ariable" or %unction
call"& !e array can be initiali.ed a" %ollo$"
int aZJ[ \ L1?35?3A?3D?3J?N,
%loat mark"ZA[ \ LD?&J391&?385&9N,
'utidimensiona Arrays
C language pro#ide" %or array" o% arbitrary dimen"ion"& ' t$o dimen"ional
array o% "i.e m ro$" and n column" i" declared a" %ollo$" type arrayname Zm[
Zn[&
' t$o dimen"ional array mark" o% type int3 $it! A ro$" and D column" i"
declared a" %ollo$"+ int mark" ZA[ZD[,
'rray" can be "tored in t!e memory in t$o order"& -o$ maGor order and
column maGor order&
)ulti-dimen"ional array" are "tored in memory u"ing t!e ro$ maGor order&
1rgani.ation o% t!e array named mark" $it! A ro$" and D column" i" "!o$n
belo$&
Col 1 Col 5 Col A Col D
-o$ 1 Z?[Z?[ Z?[Z1[ Z?[Z5[ Z?[ZA[
-o$ 5 Z1[Z?[ Z1[Z1[ Z1[Z5[ Z1[ZA[
-o$ A Z5[Z?[ Z5[Z1[ Z5[Z5[ Z5[ZA[
Element" in a t$o dimen"ional array can be acce""ed by mean" o% a ro$ and a
column& !e ro$ and "ub"cript generally i" "peci%ied be%ore t!e column "ub"cript&
0or e(ample mark" Z1[ ZA[ , $ill acce"" t!e element in ro$ number 1 and in a
column number A o% t!e array&
:n array notation mark"Z1[ repre"ent" a ro$ and mark"Z1[ZA[ repre"ent" a column
$it!in t!at ro$&
:nitiali.ing a t$o dimen"ional array at t!e time o% array declaration3 t!e element"
$ill be a""igned by ro$"3 t!at i" t!e element" o% t!e %ir"t ro$ $ill be a""igned 3
t!en t!e element" o% t!e "econd ro$ and "o on&
Con"ider t!e %ollo$ing declaration + int -noZA[ZD[ \ L1353A3D3J3K3>383931?311315N,
!e re"ult o% t!i" initial a""ignment i" a" %ollo$"
-no Z?[Z?[ \ 1 -noZ?[Z1[ \ 5 -noZ?[Z5[ \ A -noZ?[ZA[ \ D
-noZ1[Z?[ \ J -noZ1[Z1[ \ K -noZ1[Z5[ \ > -noZ1[ZA[ \ 8
-noZ5[Z?[ \ 9 -noZ5[Z1[ \ 1? -noZ5[Z5[ \ 11 -noZ5[ZA[ \ 15
LCC - Quality Computer Education (Since 1998) D5
C Programming Handbook
!e #alue" can al"o be initiali.ed by %orming group" o% initial #alue" enclo"ed
$it!in brace"& !e #alue" $it!in an inner pair o% brace" $ill be a""igned to t!e
element" o% a ro$& 0or e(ample
int rno ZA[ ZD[ \ L
L13 53 A3 DN3
LJ3 K3 >3 8N3
L93 1?3 113 15N
N,
!%ampe of T-o 2imensiona Array
HH Program to accept a t$o dim array and print it&
OincludeP"tdio&!Q
#oid main( )
L
int aZ1??[Z1??,
clr"cr( ),
print%(REnter !e -o$ Si.e o% t!e array Z)a( W 1??[SnT),
"can%(R^dT3@n),
print%(REnter !e Column Si.e o% t!e array Z)a( W 1??[SnT),
"can%(R^dT3@m),
print%(REnter ^d :nteger"SnT3nFm),
%or(i\?,iPn,iVV)
L
%or(G\?,GPm,GVV)
L
"can% (R^dT3@aZi[)ZG[,
N
N
print% (R'rray Element"SnT),
%or (i\?,iPn,iVV)
L
N
getc! ( ),
N
LCC - Quality Computer Education (Since 1998) DA
C Programming Handbook
Chapter )
,trings
Topics Covered
Introduction
Program to demonstrate printing of a string
Program to demonstrate printing of a string -ith a EF7G=
Program to demonstrate printing of a string -ith a pointer=
Program to demonstrate accepting and printing strings using
gets/ 0 and puts / 0=
Common ,tandard #ibrary ,tring &unctions
#ibrary ,tring &unction !%ampes
T-o 2imensiona Array of Characters !%ampe
Array of Pointers to ,trings
To !%change names using pointers !%ampe
LCC - Quality Computer Education (Since 1998) DD
C Programming Handbook
,trings
Introduction
!e $ay a group o% integer" can be "tored in an integer array3 "imilarly a group o%
c!aracter" can be "tored in a c!aracter array& C!aracter array" are many a tome
al"o called "tring"& C!aracter array" or "tring" are u"ed by programming language"
to manipulate te(t "uc! a" $ord" and "entence"&
' "tring con"tant i" a one dimen"ional array o% c!aracter" terminated by a null
XS?B& 0or e(ample
c!ar name Z[ \L X2B3 X'B3 XMB3BEB3BEB3B2B3BS?BN,
Eac! c!aracter in t!e array occupie" one byte o% memory and t!e la
"t c!aracter" i" al$ay" XS?B& !i" c!aracter alt!oug! look" t$o but i" actually only
one c!aracter and i" called a" t!e null c!aracter&
!e terminating null c!aracter XS?B i" important becau"e it i" t!e only $ay t!e
%unction" t!at $ork $it! a "tring can kno$ $!ere t!e "tring end"& !i" c!aracter i"
automatically in"erted at t!e end o% a "tring&
Program to demonstrate printing of a string
OincludeP"tdio&!Q
#oid main( )
L
c!ar nameZ [ \ R2a#eenT,
int i \ ?,
$!ile(iP\>)
L
print% (R^cT3nameZi[),
iVV,
N
getc!( ),
N
Program Output
2a#een
Note : This program relies on the string length 7.
LCC - Quality Computer Education (Since 1998) DJ
C Programming Handbook
Program to demonstrate printing of a string -ith a EF7G=
OincludeP"tdio&!Q
#oid main( )
L
c!ar nameZ [ \ R2a#eenT,
int i \ ?,
$!ile(nameZi[4\BS?B)
L
print% (R^cT3nameZi[),
iVV,
N
getc!( ),
N
Program Output
2a#een
Note : This program relies on the null character.
Program to demonstrate printing of a string -ith a pointer=
OincludeP"tdio&!Q
#oid main( )
L
c!ar nameZ [ \ R2a#eenT,
c!ar Fptr,
ptr \ name , HH Store" ba"e addre"" o% "tring
$!ile(Fptr4\BS?B)
L
print% (R^cT3Fptr)
ptrVV,
N
getc!( ),
N
Program Output
2a#een
LCC - Quality Computer Education (Since 1998) DK
C Programming Handbook
Note: As with integer array , by mentioning the name of the array we get the base
address (address of zeroth element of the array. This base address is stored in the
!ariable ptr using ptr " name. #nce the base address is obtained in ptr, $ptr would
yield the !alue at this address, which gets printed through (printf(%&c',$ptr.
Then ptr is incremented to point the ne(t character in the string. This deri!es from two
facts; array elements stored in contiguous memory locations and on incrementing a
pointer it points to the immediately ne(t location of its type. This process is carried out
till ptr doesn)t point to the last character in the string, that is *+,).
E#en t!oug! t!ere are "o many $ay" to re%er t!e element" o% a c!aracter array3 rarely i"
any one o% t!em u"ed& !i" i" becau"e print%( ) %unction !a" got a "$eet and "imple $ay o%
doing it a" "!o$n belo$&
main ( )
L
c!ar name Z [ \ R2a#eenT,
print%(X^"T3name),
N
!i" ^" u"ed in print% i" a %ormat "peci%ication %or printing out a "tring& !e "ame
"peci%ication i" u"ed to recei#e a "tring %rom t!e keyboard&
main ( )
L
c!ar name Z5J [,
print%( REnter Eour 2ameT),
"can%(R^"T3name),
print%(Hello X^"T3name),
N
2ote t!at t!e declaration c!ar name Z5J[ "et" a"ide 5J byte" under t!e array name Z [3
$!erea" t!e "can% %unction %ill" in t!e c!aracter" typed at keyboard into t!i" array until t!e
enter key i" !it& 1nce enter i" !it3 "can% place" a XS?B in t!e array& 2aturally $e "!ould pa""
t!e ba"e addre"" o% t!e array to t!e "can% ( ) %unction&
6!ile entering "can% ( ) $e mu"t be cautiou" about t$o t!ing"&
LCC - Quality Computer Education (Since 1998) D>
C Programming Handbook
(a)& !e lengt! o% t!e "tring "!ould not e(ceed t!e dimen"ion o% t!e c!aracter array&
!i" i" becau"e t!e C compiler doe"nBt per%orm bound" c!ecking on c!aracter
array"&
(b)& "can% ( ) i" not capable o% recei#ing multi $ord "tring"& !ere%ore name" "uc! a"
Lo!it -aga#endra $ould not be acceptable& !e $ay to get around $it! t!i"
limitation i" by u"ing t!e %unction get" ( ) and put"&
Program to demonstrate accepting and printing strings using gets/ 0 and puts / 0=
OincludeP"tdio&!Q
#oid main( )
L
c!ar nameZ5J [,
print%(REnter Eour 0ull 2ameSnT),
get"(name),
put" (RHello 4T),
put"(name),
getc!( ),
N
Program Output
Enter Eour 0ull 2ame
2a#een
Hello4
2a#een&
Note:
puts ( can display only one string at a time. puts ( places the cursor on the
nest line.
gets( is capable of getting only one string at a time, but can recei!e multi word
strings.
LCC - Quality Computer Education (Since 1998) D8
C Programming Handbook
Common ,tandard #ibrary ,tring &unctions
&unction Use
"trlen 0ind" t!e lengt! o% a "tring
"trl$r Con#ert" a "tring to lo$erca"e
"trupr Con#ert" a "tring to upperca"e
"trcpy Copie" a "tring into anot!er
"trcmp Compare" t$o "tring"
"trre# -e#er"e" a "tring
"trcat 'ppend" one "tring at t!e end o% anot!er
stren/ 0 e%ampe
main ( )
L
c!ar name Z[ \ R2a#eenT,
int len1,
len1 \ "trlen(arr),
print%(RString \ ^" lengt! \ ^dSnT3arr3len),
N
Output
,tring H Daveen ength H <
strcpy/ 0 e%ampe
main ( )
L
c!ar name1 Z[ \ R2a#eenT,
c!ar name5Z1?[,
"trcpy(name53name1),
print%(R2ame1 \ ^"SnT3name1),
print%(R2ame5 \ ^"SnT3name5),
N
Output
Dame8 H Daveen
Dame* H Daveen
strcat/ 0 e%ampe
main ( )
LCC - Quality Computer Education (Since 1998) D9
C Programming Handbook
L
c!ar name1 Z[ \ R2a#eenT,
c!ar name5Z[ \ RLccT,
"trcat(name53name1),
print%(R2ame1 \ ^"SnT3name1),
print%(R2ame5 \ ^"SnT3name5),
N
Output
Dame8 H Daveen
Dame* H #ccDaveen
strcmp/ 0 e%ampe
main ( )
L
c!ar name1 Z[ \ R_erryT,
c!ar name5Z[ \ R0erryT,
int i ,
i \ "trcmp(name13T_erryT),
G \ "trcmp(name13name5),
k \ "trcmp("tring13T_erry boyT),
print%(R^d ^d ^dSnT3i3G3k),
N
Output
? D W A5
:n t!e %ir"t call "trcmp ( ) call" t!e t$o "tring" are identical and "trcmp returned a #alue ?&
:n t!e "econd call t!e %ir"t c!aracter o% R_erryT doe"nBt matc! $it! t!e %ir"t c!aracter
R0erryT and t!e re"ult i" D $!ic! i" t!e numeric di%%erence bet$een 'SC:: #alue o% _ and
'SC:: #alue o% 0&
:n t!e t!ird call R_erryT doe"nBt matc! $it! R_erry 9oyT becau"e t!e null c!aracter at t!e
end o% R_erryT doe"nBt matc! t!e blank in R_erry 9oyT& !e #alue returned i" -A5 $!ic! i"
t!e #alue o% null c!aracter minu" t!e #alue o% "pace i&e& X?B minu" X B $!ic! i" eCual to -A5&
T-o 2imensiona Array of Characters !%ampe
Ode%ine 01;28 1
LCC - Quality Computer Education (Since 1998) J?
C Programming Handbook
Ode%ine 2101;28 ?
main( )
L
c!ar ma"terli"tZK[Z1?[ \ L
Rak"!ayT3
RparagT3
RramanT3
R"rini#a"T
RgopalT
RraGe"!T
N,
int i3%lag a,
c!ar yournameZ1?[,
prin%(REnter Eour 2ameSnT),
"can%(R^"T3uourname),
%lag \ 2101;28
%or(i\?,iP\J,iVV?)
L
a \ "trcmp (@ma"terZi[Z?[3 yourname+-T),
i%(a\\?)
L
print%(R6elcome 3 you can enter t!e palaceT),
%lag \ 01;28,
break,
N
N
i%(%lag \\ 2101;28)
print%(RSorry3 you are a tre"pa""erT),
N
Output
Enter Eour 2ame+ - dine"!
Sorry3 you are a tre"pa""er
Enter Eour 2ame+ - "rini#a"
6elcome3 you can enter t!e palace&
LCC - Quality Computer Education (Since 1998) J1
C Programming Handbook
2otice !o$ t!e t$o dimen"ional c!aracter !a" been initiali.ed& !e order o% t!e "ub"cript"
in t!e array declaration i" important& !e %ir"t "ub"cript gi#e" t!e number o% name" in t!e
array3 $!ile t!e "econd "ub"cript gi#e" t!e lengt! o% eac! item in t!e array&
!e name" could !a#e been input t!roug! t!e keyboard a" %ollo$"
%or (i\?,iPJ,iVV)
L
"can% (R^"T3 ma"terli"tZi[ Z?[),
N
6!ile comparing t!e "tring" t!roug! "trcmp ( ) note t!at t!e addre""e" o% t!e "tring" are
being pa""ed to "trcmp ( )& '" "een be%ore i% t$o "tring" matc!3 "trcmp ( ) $ould return a
#alue ?3 ot!er$i"e it $ould return a non .ero #alue&
!e #ariable %lag i" u"ed to keep a record o% $!et!er t!e control did reac! in"ide t!e i% or
not& o begin $it! "et t!e %lag to 2101;28& Later t!roug! t!e loop i" t!e name"
matc!3 %lag i" "et to 01;28& 6!en t!e control reac!e" beyond t!e %or loop3 i% %lag i" "till
"et to 2101;283 it mean" none o% t!e"e name" in t!e ma"terli"t Z[ Z[ matc!ed $it! t!e
one "upplied %rom t!e keyboard&
!e name" $ould be "tored in t!e memory a" "!o$n in t!e %igure& 2ote t!at eac! "tring
end" $it! a XS?B& !e arrangement a" you can appreciate i" "imilar to t!at o% a t$o
dimen"ional array&
a k " ! a y XS?
p a r a g S?
r a m a n S?
" r i n i # a " XS?
g o p a l XS?
r a G e " ! XS?
Here KJDJD3 KJDKD3 KJD>D etc& are t!e ba"e addre""e" o% "ucce""i#e name"& '" "een %rom
t!e abo#e pattern "ome o% t!e name" do not occupy all t!e byte" re"er#ed %or t!em& 0or
e(ample3 e#en t!oug! 1? byte" are re"er#ed %or "toring t!e name3 Xak"!ayB3 it occupie"
only > byte"& !u" A byte" go $a"ter& Similarly3 %or eac! name t!ere i" "ome amount o%
$a"tage& :n %act 3 more t!e number o% name"3 more $ould be t!e $a"tage& !i" can be
a#oided by u"ing R'rray o% Pointer"T&
LCC - Quality Computer Education (Since 1998) J5
KJDJD
KJDKD
KJD>D
KJD8D
KJD9D
KJJ?D
C Programming Handbook
Array of Pointers to ,trings
'" $e kno$ a pointer #ariable al$ay" contain" an addre""& !ere%ore i% $e con"truct an
array o% pointer" it $ould contain a number o% addre""e"& Let u" "ee !o$ t!e name" in t!e
earlier e(ample can be "tored in t!e array o% pointer"&
c!ar Fnaem Z[ \ L Rak"!ayT3
RparagT3
RramanT3
R"rini#a"T
RgopalT3
RraGe"!T
N,
:n t!i" declaration name" Z[ i" an array o% pointer"& :t contain" a ba"e addre""e" i%
re"pecti#e name"& !at i"3 ba"e addre"" o% Rak"!ayT i" "tored in t!e name"Z?[3 ba"e
addre"" o% RparagT i" "tored in name"Z1[ and "o on&
:n t!e 5 d array o% c!aracter"3 t!e "tring" occupied K? byte" $!ere a" in t!i" array o%
pointer"3 t!e "tring" occupy only D1 byte" "a#ing 19 byte"& 1ne o% t!e rea"on to "tore
"tring" in t!e array o% pointer" i" to male a more e%%icient u"e o% a#ailable memory&
'not!er rea"on to u"e an array o% pointer" to "tore "tring" i" to obtain a greater ea"e in
manipulation o% "tring"& !i" i" "!o$n by t!e %ollo$ing program"&
E(ample +- o E(c!ange name" u"ing 5d array o% c!aracter"
#oid main( )
L
c!ar name"Z[Z1?[ \ L Rak"!ayT3
RparagT3
RramanT3
R"rini#a"T3
LCC - Quality Computer Education (Since 1998) JA
C Programming Handbook
RgopalT3
RraGe"!T
N,
int i,
c!ar t,
print% (R1riginal + ^ ^" R3 @name"Z5[Z?[3 @name"ZA[Z?[),
%or(i \ ?, iP\9,iVV)
L
t \ name" Z5[Zi[,
name" Z5[Zi[ \ name"ZA[Zi[,
name" ZA[Zi[ \ t,
N
print% (R2e$ + ^" ^" R3 @name"Z5[Z?[3 @name"ZA[Z?[),
N
Output
1riginal+ raman "rini#a"
2e$ + "rini#a" raman
!e abo#e program i" u"ed to e(c!ange t!e name" $e reCuired to e(c!ange
corre"ponding c!aracter" o% t!e t$o name"& :n e%%ect3 1? e(c!ange" are need to
interc!ange t$o name"&
!%ampe )" To !%change names using pointers
#oid main( )
L
c!ar Fname"Z[Z1?[ \ LRak"!ayT3
RparagT3
RramanT3
R"rini#a"T3
LCC - Quality Computer Education (Since 1998) JD
C Programming Handbook
RgopalT3
RraGe"!T
N,
c!ar Ft,
print% (R1riginal + ^ ^" R3 name"Z5[3 @name"ZA[),
t \ name"Z5[,
name"Z5[ \ name"ZA[,
name"ZA[ \ t,
print%(R2e$ + ^" ^" R3 name"Z5[Z?[3 name"ZA[Z?[),
N
!e output i" "ame a" t!e earlier program& :n t!i" program all t!at $e are reCuired to do i"
e(c!ange t!e addre"" (o% t!e name") "tored in t!e array o% pointer"3 rat!er t!an t!e name"
t!em"el#e"& !u"3 by e%%ecting Gu"t one e(c!ange $e are able to interc!ange name"& !i"
make" !andling "tring" #ery con#enient&
!u" %rom t!e point o% #ie$ o% e%%icient memory u"age and ea"e o% programming3 an
array o% pointer" to "tring" de%initely "core" o#er a t$o dim c!aracter array& !at i" $!y3
e#en t!oug! in principle "tring" can be "tored and !andled t!roug! a t$o dimen"ional
array o% c!aracter"3 in actual practice it i" t!e array o% pointer" to "tring"3 $!ic! i" more
commonly u"ed&
LCC - Quality Computer Education (Since 1998) JJ
C Programming Handbook
Chapter *
,tructures
Topics Covered
Introduction
2ecaring a structure
2ecaring structure variabe
!%ampe of a structure)
!%ampe of a structure variabe decaration
!%ampe of a structure program
Array of structures
!%ampe of an array of structure program
LCC - Quality Computer Education (Since 1998) JK
C Programming Handbook
,tructures
Introduction
C $ouldnBt !a#e been "o popular !ad it been able to !andle only all int"3 %loat and
all c!ar"& :n %act in %act $!en $e !andle real $orld data3 $e donBt u"ually deal
$it! little atom" o% in%ormation like integer"3 c!aracter" and "uc!&
:n"tead $e deal $it! entitie" t!at are collection" o% t!ing"3 eac! t!ing !a#ing it"
o$n attribute"3 Gu"t a" t!e entity $e call a XbookB i" a collection o% t!ing" "uc! a"
title3 aut!or3 call number3 publi"!er3 number o% page"3 date o% publication etc&
'" you can "ee all t!i" data i" di""imilar %or e(ample aut!or i" a "tring3 $!erea"e
number o% page" i" an integer&
C pro#ide" a data type called structure& ' "tructure gat!er" toget!er3 di%%erent
atom o% in%ormation t!at compri"e a gi#en entity&
6e !a#e "een t!at ordinary #ariable can !old one piece o% data and array" can !old
a number o% piece" o% t!e "ame data type& !e"e t$o data type" can !andle a
#ariety o% "ituation"& 9ut $e mo"t o%ten $!en $e deal $it! entitie" t!at are a
collection o% di""imilar data typeB" "tructure" become more u"e%ul&
2ecaring a structure
"truct "tructurename
L
"tructure element 1,
"tructure element 5,
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&,
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&,
N,
2ecaring structure variabe
"truct "tructurename "tructure#ariable,
!%ampe of a structure+
"truct "tudent
L
int rno,
c!ar nameZ5?[,
%loat %ee",
N,
LCC - Quality Computer Education (Since 1998) J>
C Programming Handbook
!%ampe of a structure variabe decaration
"truct "tudent "13"53"A,
!%ampe of a structure program)
OincludeP"tdio&!Q
#oid main( )
L
"truct book,
L
c!ar nameZ5?[,
%loat price,
int page",
N,
"truct book b13b5,
clr"cr ( ),
print%(REnter 2ame3 Price and no o% page" o% 5 book"SnT),
"can%(R^" ^% ^dT3@b1&name3@b1&price3@b1&page"),
"can%(R^" ^% ^dT3@b5&name3@b5&price3@b5&page"),
print%(R!e 9ook detail" entered areSnT),
print%(R^" ^% ^dSnT3b1&name3b1&price3b1&page"),
print%(R^" ^% ^dSnT3b5&name3b5&price3b5&page"),
getc!(),
N
Output)
Enter 2ame3 Price and no o% page" o% 5 book"
' 1??&?? 199
9 5??&?? 1?9
!e 9ook detail" entered are
' 1??&?? 199
9 5??&?? 1?9
Array of structures
LCC - Quality Computer Education (Since 1998) J8
C Programming Handbook
1ur "ample program "!o$ing u"age o% "tructure i" rat!er "imple and it recei#e"
#alue" to #ariou" "tructure element" ad output t!e"e #alue"&
:n our "ample program to "tore data o% 1?? book" $e $ould reCuire to u"e 1??
di%%erent #ariable" %rom b1 to b1?? $!ic! i" de%initely not #ery con#enient& '
better to u"e an array o% "tructure"&
!%ampe of an array of structure program)
OincludeP"tdio&!Q
#oid main( )
L
"truct book,
L
c!ar nameZ5?[,
%loat price,
int page",
N,
"truct book b1ZJ[,
int i,
clr"cr ( ),
%or( i\ ? iP\D,iVV)
L
print%(REnter 2ame3 Price and no o% page" o% ^d bookSnT3iV1),
"can%(R^" ^% ^dT3@bZi[&name3@bZi[&price3@bZi[&page"),
N
print%(R!e 9ook detail" entered areSnT),
%or( i\ ? iP\D,iVV)
L
print%(R^" ^% ^dSnT3bZi[&name3bZi[&price3bZi[&page"),
N
getc!(),
N
LCC - Quality Computer Education (Since 1998) J9
C Programming Handbook
Chapter +
&ies
Topics Covered
Introduction
&ie Operations
&ie !%ampe Programs
LCC - Quality Computer Education (Since 1998) K?
C Programming Handbook
&ies
Introduction
1%ten it i" not enoug! to Gu"t di"play t!e data on t!e "creen& !i" i" becau"e i% t!e
data i" large3 only a limited amount o% it can be "tored in memory and only a
limited amount o% it can be "tored in memory and only a limited amount o% it can
be di"played on t!e "creen&
:t $ould be inappropriate to "tore data in t!e memory becau"e memory i" #olatile
and it" content" $ould get lo"t once t!e program i" terminated&
So i% $e need t!e data again and again it $ould !a#e to be eit!er entered t!roug!
t!e keyboard again& 't "uc! time" it become" nece""ary to "tore t!e data in a
manner t!at can be later retrie#ed and di"played eit!er in part or in $!ole $!ic!
can be ac!ie#ed by "toring t!e data a" %ile" on t!e di"k&
&ie Operations
!ere are di%%erent operation" t!at can be carried out on a %ile& !e"e are
/a0= Creation of a fie pointer=
6!en $orking $it! %ile" t!e %ir"t "tep i" to e"tabli"! a bu%%er area3 $!ere
in%ormation i" temporarily "tored $!ile being tran"%erred bet$een t!e computer"
memory and t!e data %ile& !e bu%%er area i" e"tabli"!ed by $riting
0:LE F ptr#ar,
6!ere 0:LE i" a "pecial "tructure type t!at e"tabli"!ed t!e bu%%er area and ptr#ar
i" a pointer #ariable t!at indicate" t!e beginning o% t!e bu%%er area& !e "tructure
type 0:LE i" de%ined $it!in a "y"tem include %ile "tdio&!&
b0= Opening a fie for reading or -riting=
!e %unction to open a %ile i" %open( )& !i" a""ociate" t!e %ile name $it! t!e
bu%%er area& :t return" a pointer to t!e "tructure 0:LE& !i" "tructure contain"
in%ormation about t!e opening o% t!e %ile and t!e bu%%ering in%ormation t!at i"
u"ed $!ile t!e %ile i" open&
Synta(+
0open(R%ilenameT3 mode),
6!ere %ilename i" t!e name o% t!e %ile %or reading or $riting&
!e 0ile" )ode"
Rr R - 1pened %or reading& Po"itioned at t!e beginning o% a %ile&
LCC - Quality Computer Education (Since 1998) K1
C Programming Handbook
R$T - 1pened %or $riting& !i" mean" t!e %ile i" to be created :% t!e %ile
doe" not e(i"t3 it i" created& Po"itioned at t!e beginning o% t!e %ile&
RaT - 1pened %or appending& Similar a" $ e(cept t!at t!e initial
po"itioning i" at t!e end&
R rVT - 1pened %or update& 0ile i" a#ailable %or reading and $riting& !e
initial po"itioning i" at t!e "tart o% t!e %ile&
R$VT - 1pened %or update& 0ile i" a#ailable %or reading and $riting& !e
initial po"itioning i" at t!e "tart o% t!e %ile& :% t!e %ile doe" not e(i"t
it i" created&
RaVT - 1pened %or update& 0ile i" a#ailable %or reading and $riting& !e
:nitial po"itioning i" at t!e end o% t!e %ile&
/d0= Writing to a fie=
' data %ile mu"t be created be%ore it can be proce""ed& 6!en creating a ne$ data
%ile3 t!e in%ormation i" entered t!roug! t!e keyboard i" $ritten to t!e data %ile& !e
%unction" %or $riting are %putc ( )3 %put" ( ) and %print% ( )& !e"e %unction" reCuire
an additional argument W an output identi%ication i&e&3 t!e %ile to $!ic! t!e data !a"
to be $ritten&
/e0= 5eading from a fie=
!e %unction %getc3 %get" @ %"can% can be u"ed to read %rom a %ile&
/f0= Cosing a fie=
6!en reading or $riting in a %ile t!e %ile mu"t be clo"ed& ' %ile can be clo"ed
u"ing t!e %unction %clo"e( ) &
LCC - Quality Computer Education (Since 1998) K5
C Programming Handbook
&ie !%ampe Programs
1& Program to $rite c!aracter by c!aracter into a %ile
OincludeP"tdio&!Q
#oid main ( )
L
0:LE F%ptr,
c!ar "trptr1Z [ Z [ \ R!i" i" a te"t o% putc S?T,
c!ar Fp,
clr"r( ),
%ptr \ %open (Rc!ar%ile&datT3T$T),
i%(%ptr\\2;LL)
L
print%(R;nable to open t!e %ileSnT),
e(it ( ),
N
%or(p \ "trptr1, Fp4\BS?B,pVV)
L
%putc(Fp3%ptr),
N
getc!( ),
N
Output )
!i" i" a te"t o% putc
Note : -n the abo!e e(ample opens a file charfile.dat. The fputc function copies to the
output file at the current position. The terminating character *+,) is not copied.
LCC - Quality Computer Education (Since 1998) KA
C Programming Handbook
5& Program to read c!aracter by c!aracter %rom a %ile
OincludeP"tdio&!Q
#oid main ( )
L
0:LE F%ptr,
c!ar c!,
clr"cr( ),
%p \ %open(Rc!ar%ile&datT3TrT),
$!ile(1)
L
c! \ %getc(%p),
i% (c!\\E10)
L
break,
N
print%(R^cT3c!),
N
getc!( ),
N
2ote + 1n e(ecution o% t!e program it di"play" t!e content" o% t!e %ile c!ar%ile&dat&
LCC - Quality Computer Education (Since 1998) KD
C Programming Handbook
A& Program to $rite "tring" into a %ile&
OincludeP"tdio&!Q
#oid main ( )
L
clr"cr( ),
0:LE F%p,
c!ar "Z8?[,
%p \ %open(RLCC&<T3T$T),
i%(%p\\2;LL)
L
put"(Cannot 1pen 0ile %or $ritingT),
N
print%(REnter ' 0e$ Line" o% e(tSnT),
$!ile ("trlen (get"(")) Q ?)
L
put" ("3%p),
%put"(RSnT3%p),
N
%clo"e(%p),
N
Note : .ach string terminates by hitting enter. To terminate the e(ecution of the
program, hit enter at the beginning of a line. This creates a string of zero length, which
the program recognizes as the signal to close the file and e(it.
Output)"
Enter ' 0e$ Line" o% e(t
LCC (Since 1998)
5
nd
0loor 3 -eba Comple( 3 =otagiri
!e 2ilgiri" W KDA51>&
LCC - Quality Computer Education (Since 1998) KJ
C Programming Handbook
D& Program to read "tring" %rom a %ile&
OincludeP"tdio&!Q
#oid main ( )
L
clr"cr ( ),
0:LE F%p,
c!ar "Z8?[,
%p \ %open (RLCC&<T3TrT),
i% (%p\\2;LL)
L
put" (Cannot 1pen 0ile %or -eadingT),
N
$!ile (%get" ("3 >93%p) 4\ 2;LL)
L
print%(R^"T3"),
N
%clo"e(%p),
N
Note : The function fgets ta/es three arguments. The first is the address where the
string is stored and the second is the ma(imum length of the string. This argument
pre!ents fgets ( from reading in too long a string and o!erflowing the array. The third
argument as usual is the pointer to the structure 0-1.. 2hen all the lines from the file
ha!e been read, we attempt to read one more line, in which case fgets returns a N311.
Output)"
LCC (Since 1998)
5
nd
0loor 3 -eba Comple( 3 =otagiri
!e 2ilgiri" W KDA51>&
LCC - Quality Computer Education (Since 1998) KK
C Programming Handbook
J& Program to $rite record" into a %ile&
OincludeP"tdio&!Q
#oid main ( )
L
clr"cr (),
0:Le F%p,
c!ar anot!er \ XEB,
"truct emp
L
c!ar nameZD?[,
int age,
%loat b",
N,
"truct emp e,
%p \ %open(RE)L1EEE&8'T3T$T),
i% (%p\\2;LL)
L
put"(RCannot 1pen 0ileT),
e(it ( ),
N
$!ile (anot!er \ \BEB `` anot!er \ \ XyB)
L
print%(REnter 2ame3'ge and 9a"ic SalarySnT),
"can%(R^" ^d ^%T3@e&name3@e&age3@e&b"),
%print%(%p3T^" ^d ^%SnT3e&name3@e&age3@e&b"),
print%(R'dd anot!er record (EH2) R),
%%lu"!("tdin),
anot!er \ getc!e(),
N
%clo"e(%p),
getc!( ),
N
LCC - Quality Computer Education (Since 1998) K>
C Programming Handbook
Note : -n this program we are 4ust reading the data into a structure !ariable using
scanf( and then dumping it into a dis/ file using fprintf(. The user can input as
many records as he desires. The procedure ends when the use supplies *N) for the
5uestion *Add another record (67N.
fprintf( writes the !alues in the structure !ariable to the file. This function is similar
to to printf( e(cept that a 0-1. pointer is included as the first argument.
After supplying data for one employee, we would hit the enter /ey. 2hat scanf( does is
it assigns name, age ad salary to appropriate !ariables and /eeps the enter unread in
the /eyboard buffer. 8o when its time to supply 6 or N for the 5uestion %Another
employee (67N getch( will read the enter /ey from the buffer thin/ing that the user
has entered the enter /ey. To a!oid this problem we use the function fflush( . -t is
designed to remo!e or flush out any data remaining in the buffer. The argument fflush(
must be the buffer which we want to flush out. 9ere we ha!e used *stdin ) which
means buffer related with standard input de!ice : /eyboard.
LCC - Quality Computer Education (Since 1998) K8
C Programming Handbook
K& Program to read #ariable" %rom a %ile&
OincludeP"tdio&!Q
#oid main ( )
L
clr"cr (),
0:Le F%p,
c!ar anot!er \ XEB,
"truct emp
L
c!ar nameZD?[,
int age,
%loat b",
N,
"truct emp e,
%p \ %open(RE)L1EEE&8'T3TrT),
i% (%p\\2;LL)
L
put"(RCannot 1pen 0ileT),
e(it ( ),
N
$!ile (%"can%(%p3T^" ^d ^%T3@e&name3@e&age3@e&b")4\E10)
L
print%(R^" ^d ^%SnT3e&name3e&age3e&b"),
N
%clo"e(%p),
getc!( ),
N
LCC - Quality Computer Education (Since 1998) K9
C Programming Handbook
Te%t 1s Binary mode)
So %ar $e !a#e "een t!e %ile" in te(t mode& !e only %unction a#ailable %or "toring
number" in a di"k i" u"ing %print( )&
:t i" important to under"tand !o$ numerical data i" "tored on t!e di"k by %print( )&
e(t and c!aracter" are "tored one c!aracter per byte&
2umber" "tored in memory are t$o byte" %or an integer3 %our byte" %or a %loat&
2umber" are "troed a" "tring o% c!aracter"&
!u" 15AD e#en t!oug! i" occupie" t$o byte" in memory $!en tran"%erred to t!e
di"k u"ing %print( ) $ould occupy %our byte"3 one byte per c!aracter&
Similarly %or "toring %loating pointer number 15AD&JK $ould occupy > byte"&
!u" number" $it! more digit" occupy more "pace&
Hence i% large amount o% numerical data i" to be "tored in a di"k %ile3 u"ing te(t
mode many turn out to be ine%%icient&
!e "olution i" to open t!e %ile in binary mode and u"e t!o"e %unction" %read() and
%$rite () to "tore number" in binary mode&
:t mean" eac! number $ould occupy "ame number o% byte" on a di"k at it
occupie" in memory&
LCC - Quality Computer Education (Since 1998) >?
C Programming Handbook
>& Program to $rite a record into a %ile in binary mode&
OincludeP"tdio&!Q
#oid main ( )
L
clr"cr (),
0:Le F%p,
c!ar anot!er \ XEB,
"truct emp
L
c!ar nameZD?[,
int age,
%loat b",
N,
"truct emp e,
%p \ %open(RE)L1EEE&8'T3T$bT),
i% (%p\\2;LL)
L
put"(RCannot 1pen 0ileT),
e(it ( ),
N
$!ile (anot!er \ \BEB `` anot!er \ \ XyB)
L
print%(REnter 2ame3'ge and 9a"ic SalarySnT),
"can%(R^" ^d ^%T3@e&name3@e&age3@e&b"),
%$rite(@e3 "i.eo%(e)313%p),
print%(R'dd anot!er record (EH2) R),
%%lu"!("tdin),
anot!er \ getc!e(),
N
%clo"e(%p),
getc!( ),
N
LCC - Quality Computer Education (Since 1998) >1
C Programming Handbook
Note : fwrite ;<e,sizeof(e,=,fp; the first argument is the address of the structure to
be written to the dis/.
The second argument is the size of the structure in bytes. -nstead of counting the bytes
occupied by the structure, we let the program do it for us by using the sizeof( operator.
The sizeof( operator gi!es the size of the size of the !ariable in bytes. This /eeps the
program unchanged in e!ent of change in the elements of the structure.
The third argument is the number of such structures that we want to write at one time.
-n this case we want to write only one structure at a time.
The last argument is the pointer to the file we want to write to&
LCC - Quality Computer Education (Since 1998) >5
C Programming Handbook
8& Program to read a record %rom a binary %ile&
OincludeP"tdio&!Q
#oid main ( )
L
clr"cr (),
0:Le F%p,
c!ar anot!er \ XEB,
"truct emp
L
c!ar nameZD?[,
int age,
%loat b",
N,
"truct emp e,
%p \ %open(RE)L1EEE&8'T3TrbT),
i% (%p\\2;LL)
L
put"(RCannot 1pen 0ileT),
e(it ( ),
N
$!ile (%read (@e3"i.eo%(e)313%p)\\1)
L
print%(R^" ^d ^%T3e&name3e&age3e&b"),
N
%clo"e(%p),
getc!( ),
N
2ote + !e %read() %unction cau"e" t!e data read %rom t!e di"k to be placed in t!e "tructure
#ariable e& !e %ormat o% %read () i" "ame a" t!at o% %$ite( )& !e %unction %read( ) return"
t!e number o% record" read& :% $e !a#e read t!e end o% t!e %ile3 "ince %read( ) cannot read
anyt!ing it return" a ?& 9y te"ting t!i" "ituation3 $e kno$ $!en to "top reading&
LCC - Quality Computer Education (Since 1998) >A
C Programming Handbook
#ist of C Programs to be soved
1& Program to %ind "um o% t$o number"&
5& Program to %ind area and circum%erence o% circle&
A& Program to %ind t!e "imple intere"t&
D& Program to con#ert temperature %rom degree centigrade to 0a!ren!eit&
J& Program to calculate "um o% J "ubGect" @ %ind percentage&
K& Program to "!o$ "$ap o% t$o noB" $it!out u"ing t!ird #ariable&
>& Program to re#er"e a gi#en number&
8& Program to %ind gro"" "alary&
9& Program to print a table o% any number&
1?& Program to %ind greate"t in A number"&
11& Program to %ind bigge"t o% t$o noB" (u"e conditional operator)&
15& Program to %ind t!at entered year i" leap year or not&
1A& Program to %ind $!et!er gi#en no i" e#en or odd&
1D& Program to u"e "$itc! "tatement& 8i"play )onday to Sunday&
1J& Program to di"play arit!metic operator u"ing "$itc! ca"e&
1K& Program to di"play %ir"t 1? natural no @ t!eir "um&
1>& Program to print "tar" SeCuence&
18& Program to print number" "eCuence"&
19& Program to print 0ibonacci "erie" up to 1??&
5?& Program to %ind %actorial o% a number&
51& Program to %ind $!et!er gi#en no i" a prime no or not&
55& Program to di"play "um o% "erie" 1V1H5V1HAV]]]&V1Hn&
5A& Program to di"play "erie" and %ind "um o% 1VAVJV]]&&Vn&
5D& Program to add t$o number u"ing pointer&
5J& Program to "!o$ "um o% 1? element" o% array @ "!o$ t!e a#erage&
5K& Program to %ind t!e ma(imum no in an array&
5>& Program to di"play a matri(&
58& Program to %ind "um o% t$o matrice"&
59& Program to %ind "ubtraction o% t$o matrice"&
A?& Program to %ind multiplication o% t$o matrice"&
A1& Program to %ind tran"po"e o% a matri(&
LCC - Quality Computer Education (Since 1998) >D
C Programming Handbook
A5& Program to %ind t!e ma(imum number in array u"ing pointer&
AA& Program to re#er"e a number u"ing pointer&
AD& Program to "!o$ input and output o% a "tring&
AJ& Program to %ind "Cuare o% a number u"ing %unction"&
AK& Program to "$ap t$o number" u"ing %unction"&
A>& Program to %ind %actorial o% a number u"ing %unction"&
A8& Program to "!o$ table o% a number u"ing %unction"&
A9& Program to "!o$ call by #alue&
D?& Program to "!o$ call by re%erence&
D1& Program to %ind large"t o% t$o number" u"ing %unction"&
D5& Program to %ind %actorial o% a number u"ing recur"ion&
DA& Program to %ind $!et!er a "tring i" palindrome or not&
DD& Program to "ort a one dimen"ional array&
DJ& Program to print only t!e diagonal element" o% a matri(&
DK& Program to %ind IC8 o% t$o po"iti#e integer"&
D>& Program to create a "tructure $it! "tudent detail" and print it&
D8& Program to read and $rite data into a %ile&
D9& Program to copy data %rom one %ile to anot!er&
J?& )enu dri#en C program u"ing %ile" to 'dd3 Li"t3 )odi%y and 8elete record"&
LCC - Quality Computer Education (Since 1998) >J