0% found this document useful (0 votes)
29 views

C Language Notes-2

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
29 views

C Language Notes-2

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 40
fat as Gormat Spectfier ¢ “Frofeam tnt { p= tps p> variable name Ink tps i q Ee vets of var a ra 2000 8000 —s address of vor P= a5 : ee eat ; tnt a= toe one Clk oe S 000” $000 f / Te ti TU Nudsighed fete oe (ave values. oly ) main () | inta=to, tp, p=Sas | print® Cased" ads [lacie prntt (“as ‘bat "pds Mazi ig print® ("a eeu" Lays Na- 2000 prot (“asehu" py iy ot 2000 return 06 ; address of variable value ot that address Advantages + we can directly access the data . 4+ dynamic memory altocation and deallecat on > resize the memory . Disadvartaqe 7 FP value ts net asstgned to pointer, segmentation fault, Tt will show m wWiicle © Pregram=te odd tise au bbe, using ‘potsters APincludecstdfoshS tht maine) { tnt a=lo b= 20; Tete a: paka?y qe kbs printt (“sum = 7d" pata) return Oo; . cinters. ¥ write a C program to swap two numbers using P: abinclude | Trt main ¢) : , { Tint a=to, b=20,*p tq « pr tas qe sos “pe “pata s “y= *p fq s “Pi Sp nto print€ (Nase/ed\n bethd” €p qs return 0% Nop PoinTeR Tt can hold auy datatype &i tntas a Rloat bs i) void *P Fy 3 p=%a lor) pe tb 3 q= 2a tor) yetbs “ write'e C program to print ftoat and character type using pointers : Htinclude < stdrosh> tnt main () { Float b= 20.6 : char ch = ‘a’, vofd tp tq 5 2 paxbs gq = &ch 5 printf (“be 8 \n che foo”, tp, tg); return Os ; \ ‘01 09-22 . 2 ContRou Statements rogram based on condit onal \ > Flow of execution of a P and uncond?Honal statements, : A a woe my > one part of prograri'“lo another ‘part Sf program ContRoL a Stitements > ao Onconditional statements break goto Oectercn / ae control loop / a * . atu . telection stad errenls Heralian Statemens Statement eersions statements © © Simpre ze > me-wiay selection Statement’ Syritox + Statement—x s & Ceondition) ' Statements + : Statementiy ¢ Flow chart + pragram + Abinclude < stdio-h > : int main) { tnt x = tos re Cx > 0) { x4 printf ("x value is ha " mays) préntf ("this main prodrar”) 2.5, return 0; : rym doing meg i 4 «CC tesy w He Shag gif-else STATEMENT : > two-way seleckon statement 3 SES an extension for ciple 2 Syrtax : Statement -x $ d f2 Ccondition) I] state ments + y else { Y |] Statements : * Write a C program do check whether the given year 2 leap year or nots ttnclude ¢ stdiorh S int smain () { int year » erint® (* eater year\n") 2 feet” Fed", Kyear) 3 € ( year 7s ye od) , printf [: Leap year”) : else print C" Nota leap year Ne return 0; j () else-fe Lapper Syntax ¢ Re ech 1) i Statements 3 3 else 7# Cton-2) t Statemente ; y - else Statements : 3 ce (oor S Nite a Co pregram to print the colours of VEBBYOR, include int main 4 char ch ¢ erinté (“-thter aces \n")s Seanf (“%e", $eh) TE (che = Sv’ [| che =sy') print€ (“Wiolet") ¢ else i£- (che || ch==“i!) print€ (“Traigo") ¢ else tf (ch= = ‘6! || ch ==‘b') printé (“Btue") » else 8 (ch== ‘6! [] ch=='9/) printf (“ Green") 7 Neéstep f-else Syntox : else 72 (ch==‘Y' Il ch= ety’) print Cyettow") ¢ ie else t@ (ch oir ch= : a : 2 prenté. (" Orange") a else 2 (chet 'R! [che a'r) + print C’Red") we , i e else : : a print? (“Tvabid") > T \tew tall return 0; eee. & $0 (condition 1) if t2 Ceondition 2) { Statements 2 oA i 5 : - else f Stotemenis's r # write a C program to check ehether: the aver pomber: pettus or negetive number, tfneludle < stdfo-h> fat main ¢) int m ; We Scant ("%/-d" an) 5 € Cn>=0) TE Cn>o) print€ ("positive no.) else ) print C2ero") ; ‘ else oe a printf (“negetive no." )3 3 return Oy ® ulrite a C program to “find the greatest of ee nombers . AW iheluce kt) - ; : : else. { shy U Cbocy print®-( “vd ts greatest”, b) ; else : printé (“vd ts greatest | els 3 return o; SwitTCH vault? wey election Atatement Syntax Sustteh (exp) { case Cabek_t2 g M Statements break 5 case Cabel_2 If Statements + break ¢ ease bab ¢- de@ault + [| stotements ms break ¢ loop STATEMENTS loop —s set of PnPdwections (or) stolements evecuted repeadily unttl the condttion be-fole, tom Ww wed. > kntry~ controlled loop ( pre- checking loop 4) Ly vote, for > Gil~ controlled (oop ( pot teat / pod = checking loop } Ly do-schite uthile Loop Syntox + fnttialfratfon : while Coondition ) I body of Statement : therement / decrement s louchart + tri tal fealion Statumenly incre [deere en program Winclucts <6tdéoh S ofp: A - e fmt main) : 4 c S int Pats while (P< 10) f 2 7 : print? (“%/ed\n”, &) 9 C++ s to i return o 5 DIFFERENCE ByyO WHI AND foR white. for te alt three fn « We write fitialiadtion, > we ae = conditfon aust frere/deere, one. step - in three diff. steps. foe ; for ( vuthialiaatisn ¢ eoudilion ; ducrewut /dscreusen Syvitox + orc ue oe A act of darlrudious . propia Nthelue €etdto dhs ul matin { : fot ? 5 for (tats few erty { print (edn Ys } Co " wn Oe relorn 07 % Write aC program “lo (ind aum of fodtutduod, digtts of nomber. .: Aktnclude ¢ atdiorh> tnt main O { tae inl nr, sums Of print ("-Cnter ns") ¢ Sean (“Ad : %n) : uthile (n> 0) f 1 als Re nZlo ¢ Sum= Sumi h=n/io; printf (“som 1 /-d", sum) return 0; g write a C program to check whether the given number 16 palindrome or not: avinelude ¢ etdiorh> frit math O t tnt nm, ©, sum-o0 ying. preate Center ne 2 4 Seang Ly d" an) 2 tne ao whfle Ens. 0) { A re n%lo¢ Som= sumtiot r 2 n=n/io; cy 3 Sana parte (tn = = som) oo print C“ poltndrome") else printé c" i “halindreme! yy vetérn Oy ide in baa) ¥ bite a 0 program “to check whether the given number Ys armstrong number or not Abinclucte < stdforh> tnt matn () i tnt n, +, dum=0,4n 2 print (“Onter for bey 7 Seon€ ("Yed" $n) ae Ane=ns whl. (n>o) “on re n%lor nm Bom = somt (r¥r &r) » N= n/lo ¢ 3 C2 Ctn== 0m) prenté (" armstrong no: ie else prime (“not armstrong vio") = + return © 3 * ulrite a C program lo print -the table of given number. ak include < stdoh> tht main () f ° unt Lc Print ("enter number ") s Sean€ ("Ag "ands for (a1 otce { loz tt+4) wen*?, print (Md ™ Yd = pdm, or): 5 rek 3 urn Oo aol fon Mette 6 CO progeaue to prank (otlownng! Polen * £ eg et ” * % 4% ye vey, ALincluds¢ stdiosh > fat moin ¢) f ° tnt v7 Vd ' _ for(fateitzetod tir) { print C“eted +"); printf Xn"); 3 return O¢ Al ticluds ¢ eteliohs val usiain cy t wtp. ' for (bet Pent 3 by) { i hor (jets feng gas) f ; printe (dt) 2 priate ("\n") + 3 return O; , a t 2 (23 2s 4 teas 4 & Atindlude ¢ stdiv:h > tht matn¢) { tnt OF for (fats Cc=t0; ett) f for (jet a Jeet s fet) f printe (°%-d" js ernte (Sang } . ; 5 relirn oO; i w. « ek & Kee Ke & eee Ee KE we eK Kk kw kK & gptnclude < stded-h >, ed maine) ' int rows, &, JK <0 : print€ ("tnter novot rows: ")s z dcan€ ('x%d" Srows): lor ( Fats fe =rows » £444,120) { for fats jenrows ; far) | pract? (" yi 5 while (k I= (2*F-1)) { pratt CR") s Keres ++ky 0 (7, ' ~{ > hivan aa? "9 m ie=ey printE COXn") 5:4 J return 07 yr —————_—_ | __ ‘ numbe ane given r mw write a Cc program ‘to check whedher M4 & perlect number or not: Winclude =t { 7 Tf oct» Lact? s Scant (#7 4 t--¢ do _ f Z \ prénté Ud" Lact) + vetunu oO; Pe 4 oo os C++ 2 7 } white (fZ=n) UNCONDITIONAL Stateme NTS > break. : : j | ~ continue wed w loop So atateusits | . return cud execchon a values, variables, expresstons Bdom 4 tnt main 5 t tt num, ¢t 2 ao print€ (" enter number”) 2 Scan ("/-d", num) ¢ for (fats Cari0 7 C44) ; | t : Yo: 3 7 t€ (ebm =<) : : break + 3 print eCteeg”, nom) relorn 0; 4 2 oS 4 o 7 & q ao t (num - 2) Of: % coutinus 4 putt ("2a" wun)s retoru 0% Stk: sorléets te=moms fr) Se { fF (mow = =5) goto Att » —ferayS ——° > store collection of data'or elements: with Aame.+ dotatype- > Store homogenous eillettion of data : Syntax: dual 7 datatype arrname[ atze] : tht als); , to | 20] 80 | 4o] 50 fF Memory becuphed : 5x2 ato} att) ale} ats] alu) sto bytes. ADVANTAGES ° 0 Stores homogenous collection o€ data 2 Code optimteation Ls tess codento access the data. 3) Memory ts allocated continuously . DISADVANTAGES ——o Bese 0 Array Atze u €iked 2 Memory iy wasted, 3) jn Cnsertean ied deletisn no o€ elements "es dr@edicutt Arrays ‘ : i t Dimenstonal.. > 9- Dimensional * Multidimensional. oe ae 10 Array datatype Sree rere : tnt a= fig) : tnt a fs] = {t0,20,30, 4o, 50} 3 int als) = fo} : tnt alS] = fto, 20,30} — automaticalty takes other variables as Zero. int al] ={ te,20, 86,40,50, 604 °, 2D Array) iT 4, dotatype arrname [sooaize][colomnsize) » o Multi - Dfmemtonal Array fieatiags datatype arrname [sized] [sitter] _-.[steen] 5 Tiitfalledon- [Ce int afs'] = 10,20, 30, 40, 504 ofS) = [te,20,30f — other Aeuoim O- char name [20] ; « char nau [10] = Kavi” » ? char mame( J =" Wekouts ©” o 2 char name(s} = TR Al yl Gey * Writ a ¢ program # dead tre foot a elements aad Print: ‘of loctt array elameuts . tat matn() t loot tts) ; print ("Snter array) chant for (084 the sty Seont ("7.e%, sareq), for (i=0 ; fee, fe) printé ("Ze ay ‘return Os “4 & ulrfte a C program to print personal details pea student [Information t'ke student name, student'rell ae) Student marks a] 6 subjects, total, average J — arinelude 9 33 | at ent main 0) { char ch[201 ¢ a F tnt roll ¢ tfoae £=05 S tnt marks [6] + ust CF ; / . float avg 4 sp nes eu eee prnt€ ‘(Center student name, roll noe 7 ')'54 seane (“%S%/-d", ¥ch / AOblezroll) » printf (“enter marks +") ¢ : for (feo, feeb: TH+), a scanf (Sa/ad", Kot marks [é] ) 2 for be on; bate 5 THI t= t4marks(i] 5 ovg = t/6 5 printe (es\n %dX\n')¢ for( f=0 + fee. y fat) print€ ("7-4") marks C7)) s print€ (Xn total! Ad \n average: HO % t,0vg)2 retorn 0s i t te program to read cGPA 4 to Atudev dud Print them. AP tucluds < etdioh > Gal main 0 f float egpal to] 2 tat €; Priel (Order egpa af 10 xtudinls 7 Seon . for fo 5 Pe 10 7 t44) sean€ ("7-6") ogra de, i for (f=0'; ¢kr0; U44) |. print ("*¢Sne, egpa él) + relurn Os U5 phe p 202% tor strug, Ahere u mo need ed: we tw ean: statement . ex: scant ("*/-s") mawe) + TSeco2, the string B imteral’ poste mr. Ot dire ely pownters the characters. CEoncTions we write'a: complex prigrau : 7 His Mucuit for readability, te & also diffucult for ceusabititi/ 2 jAts's debaggiig " iy 4) 0 oe A block o€ code which performs specific Hosie wher fe & Cmvokted Cor) called . Functions are alse called moclule design cor subprograr Cor) user-defined functions 2 Aovantages oF functions __ o D Readabrttty 2 Reaumabiltty of code 3) Bebuaging]t eary/ 4) | dtvideag large program into mo-of modules lor) Lunetfons , “types of functions __o . ° WW Butlt—f function (or) Standard. Ubrar Lunétion = in unero! y @ these Lunetvons are already digined and storeal th the Comelter a i Any modi€fcation done by user will not reflect Deter We donkh know tnternal wor king of bul ti tn £unction , ©) User-defined function Created by wher. Any modification canbe done by user , User can kow fnternal working of function , cetl() —> round off the Floating Valus. ; : ganelrons has three acpects < ucer- ded ine! une. eallid by a Punetton declaratvon | rmaint) dune: 2) Catting a fonction eee : [otuay (3) Function de€%ton, operating Basten. Syntax, a . : dey returntype funename ( parameter (at) 2 J wo. clatot yp e arguments \ ( tt, float, char, Als valuet (or) verd ) oe * Varfables (or) data. pos ed throug b function . etevalios cs —fanitidn dat (Aum ( tht, Pint) Before maini), we have to declare the function , 4 ‘ fs . Tanetion de€inttion ° function header’ * selurntype tunename ( datatype varts datalype var2) + { Syntax i logite : nition b I body of function; % “anener dy ase sthiuclude < stdio+h > det aul tnt, aE) = Y) Lundlion declebalion int main () { fnt a,b,c ¢ Scant (“Zdef.d') ga, 9b) + peduel ——s pore" ula re = sum(a,b) 2 Puff callin 9 function print (“ex ted" e) 5 endef J formal Prey : ROE cat eve : a um ( ee uty) lv burel fey, dienihen return keys > relurn to call, 1g durtiou CLASSIFICATION oe SOLUNG, A Punetionl . i (WIth parameters 9 with vetorn value ) with parameters e owithout retorn value (3) without Parameters ®» with relorn value, CH) without parameters # without retuen value. mM Sep 22 ’ aw prograu £ Au of boo fai . “ot sum ( tnt @, tnt b) fit main () : eV do { tht a,b 363 Scanf (“%dlefed", ka, xb) 3 C= Aum (a,b) ¢ . ! printf (Saeult © fd" ¢) 2 retorn 05 : fat Aum ( int, tnt &) cetureath< tnt oe g CHKtY Ty » returnc - om B) votd au (rut dat) fut ain O { Pat a,b + Sean (“*a¥/.g", 80,96) 5 dow (a,b) 5 reluos ] void sum Cint x, ent y) £ a Tut ce c= kty 5! printe (8.4 e)s ° 10 Gab a Pbi"e + CHath . 3 Print€ (* dui’ = “/.d", fc) > uw ° wt sun ( ds void main ¢) ° tnt Aesult : result = ~zum( ) 3 Pinte ("result © yd" seule) ‘ Botd “autin (. iy ita be 3 C=ath. 3 result return es J) Passing Pavameters Techniques 8 +3 colt by value (data. ) 4 calt by seference (#) = | & void swap ( tnt, int) s tnt main 0) { Vea A PRP VY int ay, b> z 2 ae i pointe Ge enter a,b values hk ae ‘sean€ (* a ed * ", da, 3b) 5 4 brinte cC Sefore Awapptag : a= td, b=d" af): * swap (a, by; retorn—O; print? ("Aéter Swapping saz yb=/d", a,b): vord swap ( int x, int y) t tnt ts t=xe ae : t Coe Print C"ARter sunpping 1 MeM/-d,ty= "had x Y)s Call by value > copy value to the cated funct or, > any changes made tn the called functions will Mot e€fect tar cating Runelion .” Can by ference void swap ( ft » pint x) ft mart f t tnt a,b : printf ("( Sa, %b) 5 : ' ‘pri? (* Aeter Swapping Lastd, be7.d" a,b), retomn oy us void swap ( eat *x int *y) ¢ tt t 9 ta 6 ay ih 5 Hy

You might also like