L06 function and macro

Page 1

emeronTI6 ³

GnuKmn_ nig m:aRkU Function & Macro I. sBaØaNTUeTAén SubProgram

kñgú karsresrkmμvFi I eyIgEtgCYbRbTHEpñkmYycMnYnrbs;kmμviFIRtUv)ansresr mþgehIymþgeTotCaeRcInelIkeRcInsarenAtamkEnøgxus²Kña. edIm,IeCosvagkar sresreRcIndgnUvkMNat;kmμviFIdEdlenH eyIgGacykkMNat;kmμvFi TI aMgenHeTAbegátI CakmμviFIrg (Subprogram) ehIyykeQμaHrbs;kmμviFIrgenaHeTACMnYsenAtam kEnøgEdlRtUveRbIkMNat;kmμvFi eI nH. mann½yfa eBlNacaM)ac;RtUveRbIkMNat;kmμviFI munenH eyIgRKan;EtehAeQμaHrbs;kmμvFi rI gsmmUlnwgvamkeRbICakareRsc eday mincaM)ac;sresrkMNat;kmμvFi TI aMgenaHeLIgvijCaeRcInelIkeRcInsaeLIy. ]TahrN_ faeBleFVlI Mhat;KNna cos b¤ sin enaHeyIgRtUvKNnaCaeRcInelIkeRcInsanUv sin rbs;mMuNamYy dUecñHeyIgRtUvbegáItkmμviFIrgmYymaneQμaHfa sin nig):ar:aEmRt smmUlrbs;vaKW x (Edl x TIenHKWCamMuRtUvKNna). bNþakmμviFIrgTaMgGs;enHRtUv )aneKbegáIteLIgrYcCaeRsc nigsresrRbkasvaenAkñúg library file rbs;Pasa sresrkmμviFImYycMnYndUcCa C, Pascal, Basic .l. dUcenHeyIgGacehAkmμviFIrg TaMgenH)anfaCaGnuKmn_EdlmanRsab; b¤ Standard Subprogram . kñúg Turbo C bNþa Subprogram TaMgenHRtUv)anEbgEckeTACaRkum ehIyTukenAkñúg file epSg²BIKñadUcCa stdio.h , conio.h , math.h CaedIm.


ehtuplmYyeTotEdlRtUvbegáItkmμviFIrgKW enAeBlsresrkmμviFIFMmYyenaH PaBsμúRKsμajCaeRcIn)anekIteLIg ehIyeBlenaHkmμviFIGacmanRbEvgEvgGnøay EdlbNþaleGaymankarBi)aky:agxøaMgkñúgkarEsVgrkkMhus nigEksMrYl. dUecñH eyIgGacpþac;bBaðasμúKsμajTaMgenaHeTACacMENktUc² KWbegáIteTACakmμviFIrg b¤Ca bNþúM (Block) b¤Cam:UDul edIm,IgayRsYlkñúgkarRtYtBinitü nigEsVgrkkMhus tambMENknImYy²rbs;kmμvFi rI g bnÞab;mkeTIbRbmUlkmμviFIrgTaMgenaHmkpMÁúKñaedIm,I begáItCakmμviFIFMxagelIvij. kmμviFIrgRtUv)aneKeRbIR)as;ya:gTUlMTUlay dUecñHcMeNHdwgGMBIkareRbI nig begáItkmμviFIrg KWCaPaBcaM)ac;bMputsMrab;Gñk kñúgkarsresrkmμviFI. kñúgbNþaPasa sresrkmμviFImYycMnYndUcCa Basic b¤ Pascal CaedIm mankarEbgEckkmμviFIrg CaBIRbePTKW Function nig Procedure Etkñúg C/C++ vijKWmanEt Function mYyKt;. II. GnuKmn_ (Function )

kñúgPasasresrkmμviFIkMuBüÚT½r GnuKmn_KWCaTMrg;EbbbTénkargarNamYy EdlRtUv)ankMNt;eQμaH nwgRtUv)anrkSaTukkñúg Memory edIm,IeqøIytbeGayGñk eRbInvU tMélEdlmanRbePTNamYy. ]TahrN_dcU Ca GnuKmn_ NPV rbs; MS-Exel vanwgpþle; GayGñknUvtMélbc©úb,nñ (Present Value) KWbnu: μan? eRkayeBlEdlGñk)an bBa©ÚlTinñn½yeGayvamYycMnYndUcCa GMLúgeBl (Period) GRtakarR)ak; (Rate) nig tMélGnaKt (Future Value). GnuKmn_RtUv)aneKeRbICamYybNþakenSamepSg²)an EtBMuGaccat;TukvafaCa Statement )aneT. GnuKmn_ykTinñn½ycUltamry³):ar:aEmRt nwge)aHtMélecjmkxageRkAvij tamry³eQμaHrbs;va. kñúg C eyIgGaceRbIGnuKmn_tamEbbEpnepSg²Kña ³ • tMélrbs;GnuKmn_ eyIgGacnwgBMuykvaeRbIk)¾ an Cak;EsþgdUcCaGnuKmn_ printf nig scanf EdlGnuKmn_TaMgenaHsuTE§ tpþlt; MélmkvijmanRbePTCacMnYnKt;. • GnuKmn_xHø GacKμantMélNamYyRtUv)anP¢ab;eGayeQμaHrbs;va (GnuKmn_ RbePT void).

108


eQμaHGnuKmn_ Gacpþl;tMélCaGasydæan Memory Edlva)ankMBug Etcg¥úleTArk (GnuKmn_RbePT Array b¤ Pointer). • GnuKmn_ Gacpøas;bþÚr b¤eFVIeGayERbRbYltMéledImrbs;GaKuym:g;. •

eTaHbICaehAfaGnuKmn_Emn b:uEnþ C min)ankMNt;lkçxNÐénrebobeRbIR)as; eT. eRkABIenHeTot C enApþl;lT§PaBeGayeyIgGac Compile m:UDulnImYy²dac; edayELkBIKña)an EdlbBaðaenHmansar³sMxan;bMputenAeBlEdlcMeNaTFMmYy RtUv)anbMEbkeTACam:UDultUc² nigsresredayÉkrakCü. ]TahrN¾ ³ KNnaGnuKmn_KNitviTüaxageRkam ³ y = f(x) = f_ex(x) = x2 + bx + c

EdlkñúgenaH x CacMnYnBit cMENkÉ b nig c CacMnYnKt; nigmaneRbI f_ex CaeQμaHrbs;GnuKmn_. /*----- kmμviFI Prog5_1.C -----*/ #include <stdio.h> #include <conio.h> void main( ) {

RbkasTMrg;GnuKmn_muneBleRbIva */

float f_ex(float , int , int); /* float x = 1.5; float y , z ; int n = 3 , p = 5 , q = 10; /*

ehAGnuKmn_ f_ex edIm,IKNna rYcehIyP¢ab;lT§pleGayGefr y

*/

y = f_ex(x, n, p); clrscr( );

bgðajtMélrbs; y elIeGRkg; */ /*ehAGnuKmn_ f_ex edIm,IKNna rYcehIyP¢ab;lT§pleGayGefr z */

printf(“Value of y = %f \n”, y); /*

z = f_ex(x+0.5 , q , n - 1); printf(“Value of z = %f \n”, z); /* getch();

bgðajtMélrbs; z elIeGRkg; */

}

109


RbkasGnuKmn_

/* – Declaring Function */ float f_ex(float x , int b , int c ) {

RbkasGefrdMbn; */ /* KNnaGnuKmn_ x +bx+c rYcyklT§plP¢ab;eGayGefr value */ float value ; /*

2

value = x*x + b*x + c;

e)aHtMélEdlkMBugEtpÞúkkñúgGefr value eGayeTAeQμaHGnuKmn_ */

/* return value ; }

lT§plelIeGRkg;KW ³ Value of y = 11.750000 Value of z = 26.000000

viPaKelIkmμviFI Prog5_1.C GnuKmn_ f_ex manbI):ara:EmRtsMrab;ykTinñn½ycUleTAkñúgtYGnuKmn_. muneBlehAGnuKmn_eRbI CadMbUgRtUvsresrRbkasTMrg;GnuKmn_ (Prototype) enA xagedIménkmμviFIsin (GackñúgtYGnuKmn_ main b¤k¾GacRbkasBImux main k¾)an Edr) EdlkarRbkasenHrYmman eQμaHrbs;GnuKmn_CamYyRbePTTinñn½y nigbNþa )a:ra:EmRtrbs;GnuKmn_ (sMrab;kmμviFI Prog5_1.C enH karRbkasTMrg;GnuKmn_ KWenAbnÞat; float f_ex(float , int , int); /*RbkasTMrg;GnuKmn_muneBleRbIva */ ). cMENkÉkargarrbs;GnuKmn_vijeyIgminTan;dwgenAeLIyeT enAxagcugénkmμviFI eTIbmankarRbkasGnuKmn_Bti R)akd. enAkñúgtYxnÜø rbs;GnuKmn_ main eyIg)anehA GnuKmn_ f_ex cMnYnBIrdgCamYy Argument epSg²Kña ³ Â

karehAelIkTI 1 : 110


y = f_ex(x, n, p);

kñúgkarehAmYyenH Argument manbIKW x, n, p Edl Argument TaMgbIenH RtUvcMlgtMéleGayeTA)a:ra:Em:Rtrbs;GnuKmn_tamlMdab;KW ³ Argument x cMlgtMéleGayeTA):ara:EmRt x rbs;GnuKmn_ Argument n cMlgtMéleGayeTA):ara:EmRt b rbs;GnuKmn_ Argument p cMlgtMéleGayeTA):ara:EmRt c rbs;GnuKmn_

karehAelIkTI 2 : y = f_ex(x+0.5 , q , n-1);

eBlenH Argument manbIKW x+0.5 , q , n-1 Edl Argument TaMgbIenH RtUvcMlgtMéleGayeTA)a:ra:Em:Rtrbs;GnuKmn_tamlMdab;KW ³ Argument x+0.5 cMlgtMéleGayeTA):ara:EmRt x rbs;GnuKmn_ Argument q cMlgtMéleGayeTA):ara:EmRt b rbs;GnuKmn_ Argument n-1 cMlgtMéleGayeTA):ara:EmRt c rbs;GnuKmn_ srubmklMnaMénkarehAGnuKmn_ f_ex mkeRbIkúñgGnuKmn_ main RtUv)anbgðaj bEnßmedayrUbxageRkam ³ void main() { float f_ex(float , int , int); ...... y = f_ex(x, n, p); ...... } float f_ex(float x , int b , int c ) { float value ; value = x*x + b*x + c; return value ; }

111


8

karRbkasGnuKmn_

cMNaM

cMNaM ³ cugbnÞat;KμansBaØacucek,ós (;) eT (KWdUcKñaCamYykarRbkas main Edr) nigbNþa)a:ra:EmRtrbs;GnuKmn_RtUvEckdac;BIKñaedaysBaØaek,ós (,) . kmμvFi EI dlGñk)ansresrtaMgBImnu rhUtmkdl;eBlenH suTE§ tmanGnuKmn_myY Canice© naHKW main EdlKμan)a:ra:EmRt nigk¾KμansBaØacucek,ósenAxagcugbnÞat;Edr. kñúgtYxøÜnGnuKmn_ f_ex eyIgeXIjfa GefrEdleK)anRbkassMrab;pÞúk lT§plGnuKmn_ (Gefr value) RtUv)aneKehAfaCaGefrdMbn; (Local Variable) eRBaHGefrenHGaceRbI)anEtenAkñúgxøÜnrbs;GnuKmn_ f_ex b:ueNÑaH. Gefr value enaHRtUv)aneKeRbIsMrab;pÞúklT§plKNnarbs;kenSam x*x + b*x +c CabeNþaH Gasnñ edIm,IP¢ab;lT§plenaHeGayeTAeQμaHrbs;GnuKmn_tamry³BaküKnøHw return. tMélrbs;GnuKmn_manGtßn½ydUcxageRkam ³ eQμaHGnuKmn_ = tMél b¤mann½yfa ³ f_ex = tMél = value

112


III. GnuKmn_kñúgPasa C

nig Argument ³ bNþa parameter eRbIenAeBl Declaration Function ehAfa parameter . bNþa parameter Edlpþl;eGay Function (kñúg main program) enAeBleyIgehA Function ehAfa Argument . parameter BitGacCakenSammYyk¾)an/ Et parameter rUbPaBminGacCakenSam)aneT . + parameter

+ return Statement : - return :

Gacpþl;tMélénkenSammYyeTAeGayeQμaH Function )an dUecñH Function

xagelIGacsresrCa ³ / * Declaration */ float f_Exam (float x, int b, int c) { return ( x * x + b * x + c ); }

)ann½yfaeyIgmincaM)ac; Declaration local Variable eT . - return GacsresreRcIndgkñúg Function . Example : double tt (double a, double b ) { double s ; s=a+b if ( s> 0 ) return (s) ; else return (-s); } -

-

ebIRbePTrbs;kenSamkñúg return pÞúyBIRbePTrbs;eQμaH Function enaH Turbo C nig bþÚr RbePTsmmUleGayvij . eyIgGacmineRbItMéllTæplrbs; Function )an . Example printf ( ), scanf ( ) . krNI Function KμantMéltbtvij ¬tMélpþl;eGayeQμaH Function) Function mYyEdlKμantMél tbtvijenaHeyIgeRbI Keyword void edIm,I Declaration va .

Example : void Function_Name (int n);

113


RbePT Void enHKμan Statement return eT . - krNI Function Kμan parameter : man Function mYycMnYnKμan parameter eT . dUecñH Declaration Function enaHeyIgeRbI -

Function

Keyword void : Ex: float Exam_para (void );

enAmankrNIepSgeTotdUcxageRkam ³ void Exam_display (void )

)ann½yfa KμantMéltbt/ nigKμan parameter eT . cUrBinitübNþa Example xageRkam ³ # include <stdio.h> # include <conio.h> main ( ) { /* prototype */ void display_square ( int, int ); void ONError (void ); /* have got a Error */ int Start1 = 56, End1 = 10; ... display_square (start1, End1); ... if (...) ONError ( ); /* Error */ } void display_square (int d, int f ) { int i; for ( i = d, i <= f; i ++ ) printf (“%d square root is %d \n”, i, i * i ); } void ONError (void ) /* Error */ { printf (“*** Error *** \n”); }

krNI Function Kμan Declaration Value : enaH Function, nwgmantMél int . Ex: Declaration Function EdlKμanRbePT Function Name (float x );

114


IV. Declaration nigbNþa +

]TahrN¾ ³

lMdab;kñúgkar declaration : Binitü]TahrN’xageRkam ³ Example : # include <stdio.h> # include <conio.h> /* Declaration Function before Function main */ float f_Exam (float x, int b, int c) { float value ; /* Declaration local variable */ value = x * x + b * x + c; return (value ); } main ( ) { /* float f_Exam (float, int, int );

min)ac;man prototype eT */

float x = 1.5; float y , z; int n = 3 , p =5, q =10 ; y = f_Exam ( y, n, p ) ; printf (" Value of y = %f \n”,y); z = f_Exam (x + 0.5, q , n-1); printf (“Value of y = %f \n”,z ); getch ( ); return (0) } + Example

KNna n! = 1.2.3.... .(n-1).n

# include <stdio.h> # include <conio.h> long int factorial (int n) { int i; long int FF = 1; if (n>1 ) for (i = 2; i < =n; i ++) FF * = i; return (FF); } main ( ) { int n; /* Read number n */ printf (“\n n = ”); scanf ("%d", &n ) ;

115


/* printf the result */ printf (“\n n! = %ld \n “, factorial (n) ); getch ( ); return (0) ; } + Example :

bþÚrBIGkSrFmμtaeTACaGkSrBum</edayeRbIRbmaNviFIlkçx½NÐ .

# include <stdio.h> # include <conio.h> char converttocapital (char ch ) { char c2; c2 = ( ch> = ‘a’ && ch < = ‘z’) ? ( ‘A’- ‘a’ + ch ): ch; return (c2); } main ( ) { char smallchar , capital; clrscr ( ); printf (“Enter a character :”); scanf (“%c”, &smallchar ); capital = converttocapital (smallchar ); printf ( “\n the capital is %c \n \n”, capital); getch ( ); return (0 ); } Example : convert capitaltosmallchar : (

edayeRbI if statement )

# include <stdio.h> # include <conio.h> char converttosmallchar (char ch ) { if (ch > = ‘a’&& ch < = ‘z’) return ( ‘A’ – ‘a’ + ch ); else return (ch); } Example :

GnuKmn¾ess KU ³ kMNt;cMnYnmYy etICacMnYness b¤ KU even (n) int n; { int result; if ( (n %2 ) = = 0) result = 1;

116


else result = 0; return ( result ); }

b¤mYykMNt;cMnYnmYyCacMnYnKU b¤ cMnYness/ eyIgGnuvtþn¾RbmaNviFI

AND

tam bit vij ³

if ( (n&0 x 01 ) ==0 ) result = 0; else result = 1;

V. bBa©Úl parameter

eGay sub-program :

karbBa©Úl parameter eGay Function kñúg Turbo C RtUv)anGnuvtþn¾tamrebobEtmYyKt; ³ + bBa©Úl Value parameter, mann½yfa tMélrbs; parameter Bit/ munnwgeRkayeBlehA Function enaHminERbRbYltMéleT . Binitü]TahrN’xageRkam ³ # include <stdio.h> # include <conio.h> main ( ) { void swap ( int a, int b); /* prototype */ int n = 10, p = 20; printf ( "\n Before call Function : %d %d \n”, n, p); swap ( n, p); printf ( “\ n After call Function: %d %d \n”, n, p); getch ( ); return (0); } void swap (int a, int b); { int t; printf ( “Befor swap : %d %d \n”, n, b); t = a; a = b; b = t; printf (“After swap : %d %d \n”, a, b ); } Before call Function : 10 20 Before swap : 10 20 After swap : 20 10

117


After call Function :

10

20

minERbRbYltMél ebIeFobnwgtMélmuneBl

Call

Function

viPaK ³ Function swap TTYltMélBIrBI parameter Bit (n,p) EdlsmmUl nig parameter rUbPaBBIreTotKW (a, b) . vanwgGnuvtþn¾karbþÚtMélBI parameter (a, b) xagelI . EtbnÞab;mkeyIgeXIjfa enAeBlRtLb;eTA main program vijenaHtMélrbs; parameter BitTaMgBIrenAd¾EdledayminERbRbYltMéleT (n,p ) . - dUecñHenAeBl call sub program, tMél n nig p nwgRtUv)anbBa©ÚneTAeGay parameter rUbPaB a, b CabeNþaHGasnñsin/ ehIy Function swap nwgbþÚrtMél a nig b CamYyKñaedayx©I variable CYykñúgkarbþÚr tMélenH . bnÞab;mktMélrbs; n, p minRtUv)anERbRbYleT/ enHCalkçN³mintMrUveTAtamtMrUvkarrbs;eyIg eRBaHeyIgcg;bþÚrtMél n nig p minEmncg;bþÚrtMél parameter rUbPaB a, b enaHeT . - eyIgnigeXIjfakarbþÚrtMélénBIr ¬b¤eRcIn ¦ parameter edayeRbI Function GacRbRBwteTA)an tamkarbBa¢ÚnCatMél Address rbs; parameter BiteTAeGay parameter rUbPaB ¬minEmnbBa©Úl Value eT¦. enaHKWkñúgkrNI Function scanf . ebIcg; Read tMél x eyIgRtUveRbI Address rbs; x edIm,IbBa¢ÚneTAeGay Function scanf : scanf ( “%f”, &x ); munnigeRkaykar call Function scanf, tMél Address rbs; Variable KμanERbRbYl/ EteyIgeRbItMél Address enHedIm,IERbRbYltMélxagkñúgva . eyIgnigniyayeLIgvij nUvbNþabBaðaenH enAeBleyIgeron pointer . EtedIm,IeGayeyIgecHeRbI Function swap dUecñHeyIgsresr Function enHenAkñúg Chapter enHeTaHbImaneRbI pointer k¾eday ³ Example : sresrkmμviFIbþÚrtMélBIr Variable n,p edayeRbIkarbBa¢Ún Value Address eTAeGay Function : +

# Include <stdio.h> # Include <conio.h> main ( ) { void swap (int *a, int *b );

118


int n = 10; p = 20; clrscr ( ) ; printf (" \n Before Call Function : %d %d \n”, n, p); swap ( &n, &p); printf ( “\n After Call Function : %d %d \n”, n, p ); getch ( ); return (0); } void swap (int *a, int *b) { int t. printf ( “\n Before swap : %d %d”, *a, *b); t = *a; *a = *b; *b = t; printf (“\n After swap : %d %d”, *a, *b); }

Before Call Function

10

20

Before swap

10

20

After swap

20

10

After Call Function

20

10

eBlenHeTIbRtUv)anbþÚrtMélén Variable n, p BitR)akdEmn . Note:

CasBaØaéntMélEdlrkSaTukkñúg Memory nig man Address a . &a CasBaØaén Address memory pÞúktMél a . *a

119


IV. EdnkMnt;rbs;Gefr

³

GefreRkA (Global Variable) KWCaGefrEdlGaceGayeyIgeRbIR)as;)anRKb;TIkEnøgTaMgGs;kñúgkmμviFI. cUr BinitüemIlkmμviFI Prog5_7.C xageRkam ³ /* kmμviFI Prog5_7.C */

#include <stdio.h> #include <conio.h>

int n ; /*RbkasGefreRkA n*/ void main() { void myfunction(void); /* n = 1; /* clrscr();

RbkasTMrg;TUeTArbs; myfunction */

P¢ab;tMéleGay n = 1 */

eRbIrgVil

edIm,IehA

/* while myfunction while(n<5) myfunction();

cMnYn 4 dg */

bgðajtMél eRkayeBlehA

/* n myfunction */ printf(“\nAfter called myfunction n = %d”, n); getch(); }

RbkasrUbragBitrbs;GnuKmn_ */

/* void myfunction(void) {

bgðajtMél elIeGRkg;

/* n */ printf(“\n The value of global variable n = %d”,n); n++; /*

begáIn n eLIgEfmmYytMéleTot */

}

lT§plelIeGRkg;KW ³ The value of global variable n = 1 The value of global variable n = 2 The value of global variable n = 3 The value of global variable n = 4 After called myfunction n = 5

120


kñúgkmμviFI Prog5_7.C xagelIenHmanGefr n CaGefreRkA (Global Variable) eRBaHvaRtUv)anRbkasenAxageRkAGnuKmn_TaMgGs; EdlrYmTaMgGnuKmn_ main pgEdr (KWKμansßitenAkñúgGnuKmn_NamYyeLIy). kñúgenaHGñkk¾)aneXIjEdr fa GñkmanlT§PaBGaceRbIGefr n kñúgGnuKmn_ myfunction edayKμankMhusGVI TaMgGs; EdlenHKWCacMNucl¥pg nigminl¥pg. cMNucl¥Rtg;fa eyIgGacbþÚrtMél rbs;GefrenAeRkayeBlehAGnuKmn_mkeRbI nigminl¥Rtg;fa edaysarEtPaBGac bþÚrtMél)anenARKb;TIkEnøgEbbenHehIy EdlGaceFVIeGaymanbBaðaenA eBleRkay CaBiesskñúgkmμviFId¾FMmYy ebIsinCaGñkeRbI n enAkñúgGnuKmn_NamYy enaH tMélrbs; n nwgmankarERbRbYl EdlenHCaehtuGacQaneTAdl;karman kMhuskñúgkmμviFI edayBMu)andwgfa etItMél n eBlenHRtUv)anERbRbYledaysar GnuKmn_mYyNa. EdnkMnt;rbs;GefreRkA EdnkMNt;rbs;Gefr)ann½yfa CatMbn;EdlGefrenaHGacmanskmμPaBeTA dl;)an. EdnkMNt;rbs;GefreRkAKW eBjkmμviFIrbs;GñkedayKitcab;BIkEnøgEdl )anRbkasGefrenaHrhUteTAdl;xagcugbBa©b;énkmμviFI. ]TahrN_ ³ void main() { ------------------}

RbkasGefreRkA */

int n ; /* float x ; function1(---) { -------} function2(---) { -------}

121


sMrab;eBlenHeyIgeXIjfa Gefr n nig x GaceRbIenAkñúg function1 nig function2 )an EtBMuGaceRbIenAkñúg main )aneLIy eRBaHvaRtUv)anRbkasenAxag eRkayGnuKmn_ main. eyIgGacRbkasGefreRkAenARtg;kEnøgNamYyénkmμviFIk¾)an EtedIm,Igay RsYldl;karBinitüemIl nigEsVgrk GñksresrkmμviFIKYrEtRbkasGefreRkAenAxag edIménkmμviFI ( EpñkxagelIénkmμviFIEdlsßitenAeRkamkarRbkas header file). GefrdMbn; (Local Variable) CaGefrEdlmanskmμPaB (b¤GaceRbI)an) EtenAkñúgGnuKmn_Edl)anRbkas vaEtb:ueNÑaH eRkayeBlGnuKmn_RtUv)anbBa©b; enaHbNþaGefrdMbn;Edl)anRbkas kñúgGnuKn_enaH k¾RtUv)anrMedaHecjBI Memory vijEdr. cUrBinti üemIlkmμvFi I Prog5_8.C xageRkamenH manbgðajGMBkI areRbIR)as;Gefr dMbn; (Local Variable). /*----- kmμvF i I Prog5_8.C-----*/ #include <stdio.h> #include <conio.h>

RbkasGefreRkA */

/* int x; float y = 5.5;

RbkasTMrg;TUeTArbs;GnuKmn_

/* */ void F1(); void F2(); void main() { clrscr(); x = 10; printf(“x = %d y = %f”, x, y); F1(); F2(); printf(“\n\nAfter called functions x = %d”, x); getch(); } void F1() { printf(“\nCall in F1, x gloable = %d”, x);

122


printf(“\nCall in F1, y gloable = %f”, y); } void F2() { int a = 50; printf(“\nCall in F2, a + x = %d + %d = %d”, a, x, a+x); x = x + a; /*

pøas;bþÚrtMélrbs;GefreRkA x */

}

lT§plelIeGRkg; ³ x = 10 y = 5.500000 Call in F1, x gloable = 10 Call in F1, y gloable = 5.500000 Call in F2, a + x = 50 + 10 = 60 After called functions x = 60

kñúgkmμviFIxagelIeyIgeXIjfa GefreRkA x nig y GacykeTAeRbI)anRKb;TI kEnøg ()anRKb;kñúgGnKmn_) edayELkGefrdMbn; a Edl)anRbkaskñúgGnuKmn_ F2 vij BMuRtUv)anGnuBaØateGayykeTAeRbIenAkñúgGnuKmn_epSgdUcCa F1 b¤k¾ main )aneT KWGaceRbI)anEtkñúgGnuKmn_Edl)anRbkasvaEtb:ueNÑaH. kñúgkrNIeQμaHdUcKña etIGefrdMbn;GacRcLMCamYyGefreRkAEdrb¤eT? edIm,IbkRsayeGaysMNYrenH cUrBinitüemIlkmμviFI Prog5_9.C xageRkam EdlkñúgenaHmankarRbkasGefr m nig n CaGefreRkApg nigCaGefrdMbn;rbs; GnKmn_epSg²eTotpg. kmμviFI

/*-----Prog5_9.C ------*/ #include"stdio.h" #include"conio.h"

RbkasGefreRkA n nig m */

/* int n = 20; int m = 50; void main() {

RbkasTMrg;TUeTArbs;GnuKmn_ */

/* void fun1();

123


void fun2(); void fun3();

RbkasGefrdMbn;

m */ int m = 10; /* clrscr(); printf("\nm in main = %d", m); fun1(); fun2(); fun3(); getch(); } void fun1() { int m = 11; printf("\nm in fun1 = %d",m); } void fun2() { int n = 30; printf("\nn in fun2= %d",n); } void fun3() { printf("\nm globle = %d",m); printf("\nn globle = %d",n); }

lT§plelIeGRkg;KW ³ m in main = 10 m in fun1= 11 n in fun2 = 30 m globle = 50 n globle = 20

tamry³]TahrN¾xagelIenH Gefr n nig m Edl)anRbkaskñúgGnuKmn_ fun1 nig fun2 KWman\T§iBlEtkñúgGnuKmn_enaHEtb:ueNÑaH )ann½yfaeyIgBMuGac ykGefrTaMgenaHeTAeRbIenAxageRkAEdnkMnt;rbs;va)aneLIy. kñúgGnuKmn_ fun1 manRbkasGefr m dUecñHeBlenHRKb;eQμaH m kñúgGnuKmn_ fun1 KWCaGefrdMbn; rbs; fun1 minEmnCaGefrdMbn;rbs;GnuKmn_ main eT ehIyk¾minEmnCaGefreRkA Edr. 124


edIm,ICakargayyl;eyIgGacniyayfa enAkñúgRsukmYymanmnusSbInak; EdlmaneQμaHdUcKñaKW {k} Etrs;enApÞHepSgKña]bmafapÞH A, B nig C. dUecñHRbsin ebIkñúgpÞH A mankarehAeQμaH {k} enaHR)akdCasMedAeTAelIeQμaH {k} Edlrs; enAkñúgpÞH A enaHehIy KWminEmnsMedAeTAelIGñkEdlrs;kñúgpÞHepSgenaHeT. kñgú kmμvFi dI cU xagelI etIGefreRkA m nigGefrdMbn; m GacRcLMKña)anEdrb¤eT? karehAeQμaH m mkeRbIkñúgkrNIenH RbsinebIeQμaH m enaH)anRbkaskñúg GnuKmn_enaH naMeGayvanwgeRbIGefrdMbn; m enaHEtmþg ()ann½yfaeBl Compile vanwgCMnUskEnøg m kñúgGnuKmn_enaHedayGasydæan Memory rbs;GefrdMbn; m) Cak;EsþgdUckñúgGnuKmn_ fun1 nig fun2. RbsinebImaneRbIeQμaHGefr m enaH Etmin)anRbkaskñúgGnuKn_enaHeT enaH Compiler nwgBinitürkemIlfa etIeQμaH m enaHmanRbkasCaGefreRkAEdrb¤eT ebImanvanwgeRbIGrfreRkA m enaH (Cak; EsþgdUckñúgGnuKmn_ fun3 maneRbIGefr m nig n) EtebIKμaneTvanwgR)ab;kMhusPøam (undefined symbol m in module…). GefrdMbn;sþaTik (Static local variable) eyIgGaceGaykMuBüÚT½rpÁt;pÁg; Memory kñúgdMbn; Static eGayeTAGefrdMbn; edayRKan;EteRbIBaküKnøwH static BIxagmuxkarRbkasGefrdMbn;enaHEtb:ueNÑaH. eBlenHTinñn½yrbs;GefrdMbn;enaHnwgRtUv)anrkSaTukkñúg Memory rhUteTaHCa GnuKmn_)anbBa©b;k¾eday. edIm,IEsVgyl;GMBIBaküKnøwH static enHeGaykan;Et c,as;las; enaHcUremIlkñúgcMNucTI I.3 énemeronTI 12 (TMB½rTI 482). kmμviFI Prog5_10.C xageRkamenH bgðajGMBIdMeNIrkarrbs;GefrdMbn; sþaTik (Static Local Variable). /*--------- kmμviFI Prog5_10.C ---------*/ #include <stdio.h> #include <conio.h> void main()

RbkasTMrg;TUeTArbs;GnuKmn_ fun */ int n ; /* Gefrrab;rgVilCMu for */

{ void fun(void); /*

125


eRbIrgVil edIm,IehAGnuKmn_ fun cMnYn 5 dg */

/* for for(n = 1 ; n <=5 ; n++) fun(); getch( ); } void fun(void)

RbkasGefrdMbn;sþaTik i */

{ static int i; /* i++; printf("\nCall in the %d time", i); }

lT§plelIeGRkg;KW ³ Call in the 1 time Call in the 2 time Call in the 3 time Call in the 4 time Call in the 5 time

kñúgkmμviFI Prog5_10.C xagelIenH Gefr i RtUv)anRbkasCaGefrdMbn; sþaTikkñúgGnuKmn_ fun dUecñHehIy)anCaeyIgeXIjfa karehAGnuKmn_ fun cMnYn 5 dgedayGnuKmn_ main )aneFVIeGaytMélrbs;GefrdMbn;sþaTik i ERbRbYlCanic© (edaysar Statement i++; kñúgGnuKmn_ fun). CaFmμtacMeBaHGefrdMbn;kñúg GnuKmn_NamYy eRkayeBlEdlGnuKmn_enaHRtUv)anGnuvtþcb; enaHbNþaGefr dMbn;rbs;GnuKmn_enaHk¾RtUv)anrMedaHecjBI Memory edaysVy½ RbvtþEi dr b¤)ann½y fa bNþaTinñn½yEdlkMBugEtrkSaTukedaybNþaGefrdMbn;TaMgenaHRtUv)an)at;bg;. cMeBaHGefrdMbn;sþaTikvij manlkçN³xusBIGefrdMbn;bnþicRtg;fa Memory Edl)anpÁt;pÁg;eGayGefrdMbn;sþaTik nwgBMuRtUv)anrMedaHecjvijeLIy eTaHbICa GnuKmn_Edl)anRbkasGefrdMbn;sþaTikenaH)anGnuvtþcb;k¾eday b¤)ann½yfa Tinñn½yEdlRtUv)anrkSaTukedayGefrdMbn;sþaTik nwgBMuRtUv)at;bg;eLIykñúgxN³ EdlGnuKmn_Edl)anRbkasGefrdMbn;sþaTikenaHBMRu tUv)anGnuvtþk¾eday ehIykuBM üÚT½r nwgrkSaTukTinñn½yenaHdrabNakmμviFITaMgmUlRtUv)anbBa©b;. dUcEdlGñk)aneXIj lT§plrbs;kmμviFI Prog5_10.C xagelIenH enAeBlEdl i RtUv)anRbkasCa GefrdMbn;sþaTik nigminTan;)anP¢ab;tMélkMNt;NamYyeGayvaenaH BaküKnøHw static nigP¢ab;tMélsUnüeGayeTA i 126


edaysV½yRbvtþi luHdl;eBlCYb i++; enaHtMélrbs; i RtUv)anekIneLIgmYytMélEfmeTot (eBlenH i=1). eRkaykarehAGnuKmn_ fun eday main enACMuTImYy tMélrbs; i RtUv)anERbRbYlesμI 1 luHdl;eBlCYbkarehA CafμIeTotenACMuTIBIr tMélrbs; i k¾RtUvbUkEfmmYyeTot EdleBlenHeFVIeGay i = 2 (eRBaHeRkaykarehAelIkTImYy i = 1) ehIyCabnþbnÞab;enACMuTI 3, 4 nig 5 tMél rbs; i k¾RtUv)anekIneLIgtamlMdab;KW 3, 4, 5 Edr. tMélrbs; i eBlenHKW 5 ehIy tMélenHnwgrkSaTukkñúg Memory rhUtdl;eBlEdlkmμviFI)anbBa©b;.

cMNaM ³ GefreRkAGacrkSaTukTinñn½y)aneBjdMeNIrkarénkmμviFI b¤)ann½yfa Memory Edl)anpÁt;pÁg;eGayGefreRkA BMuRtUv)anrMedaHvijeLIydrabNakmμviFI enAEtdMeNIrkar. eyIgGaceRbIGefreRkA)anenARKb;TkI EnøgTaMgGs;énkmμvFi I eday cab;BIkEnøgEdl)anRbkasvaeTA. GefrdMbn;GnuBaØateGayeRbI)anEtenAkñúgtYGnuKmn_Edl)anRbkasvaEtb:ueNÑaH ehIyTinñn½yEdlva)anrkSaTuknwgRtUv)at;bg;enAeBlNaEdlGnuKmn_ Edl)anRbkasGefrdMbn;enaH)anbBa©b;. GefrdMbn;saþ Tik GnuBaØateGayeyIgeRbIva)anEtkñúgtYGnuKmn_Edl)an RbkasvaEt b:ueNÑaH nigTinñn½yEdlvakMBugrkSaTukBMuRtUv)an)at;bg;eLIy KWTinñn½y enaHnwgRtUv)anrkSaTuk rhUtdl;eBlNaEdlkmμviFITaMgmUlRtUv)anbBa©b;.

127


VII.

lkçN³ Recursion én

Sub-program :

GacehAxøÜnÉgmkeRbI)an/ lkçN³enHehAfa Recursion . ]TahrN’ ³ KNnatMél N! Function

n ! = 1. 2. 3....(n-1) . n

b¤tamniymn½y 1

eBl n = 0

n!=

eBl n >= 1 dUecñH Function Factorial GackMNt;dUcxageRkam ³ (n-1) !.n

int Factorial (int n ) { if ( n ==0 ) return (1); else return ( n* Factorial (n-1)); } Example: # include < stdio.h> # include <conio.h> /* Function prototype */ long int Factorial (int n ); main () { int n ; printf (“\n n =”); scanf ("%d", &n) ; printf (“ n! = %ld \n”, Factorial (n)); getch(); return (0 ); } long int Factorial (int n) { if ( n == 0) return (1); else return ( n* Factorial ( n – 1)); }

lkçN³ Recursion RtUveRbI memory RbePT LIFO ( Last In, First Out, stack ) edIm,IpÞúklTæpl beNþaHGasnñ . Example : KNna PGCD énBIrcMnYn x, y tamniymn½yxageRkam ³ PGCD (x, y) = x ebI y = 0 *Note :

128


= PGCD (y, x/y)

ebI

y<>0

dUecñH PGCD GacKNnadUcxageRkam ³ int PGCD (int x, int y) { if ( y == 0) return (x) ; else return (PGCD ( y, x %y)); }

Et program xageRkamenHmanl¥Cagxag Algorithme, eTaHbICavamanCagk¾eday ³ int PGCD (int x, int y ) { int number;

/*

sMNl; */

while ( y != 0) { number = x %y; x = y; y = number; } return (x); }; +

dUecñHeyIgBMuKm,IeRbI Recursion enAeBlEdleyIgGaceRbI looping edIm,IKNnalMhat;enaH .

VIII.

ma:RkU

(Macro)

eRkABIGnuKmn_ Pasa C enAmanGgÁkMNt;Tis (Directive Compiler) mYy RbePTEdlGacCYyeGayeyIgcakputBIPaBsμúKsμaj nigGacsnSM)annUveBlevla enaHKIkareRbIm:aRkU.

VIII.1. niymn½y (Macro)

m:aRkUGacCasMNuMén Statement nigkenSam EdlRtUv)antMNagedayeQμaH NamYy. kñúglMnaMénkar Compile ebIsinCa Compiler )anCYbbN þaeQμaHm:aRkU enaHvanwgCMnYskEnøgeQμaHm:aRkUenaHedaybNþa Statement edImrbs;va rYceTIb Gnuvtþn_kar Compile CaeRkay.

129


VIII.2. rebobeRbI

m:aRkU

(How to Use Macro)

TMrg;TUeTAénkarRbkasm:aRkU ³ #define9

eQμaHm:aRkU9bNþMúén Statement

cUrBinitüemIlbNþa]TahrN_xageRkamenH suT§EtCakmμviFIbgðajGMBIrebob eRbIR)as;m:aRkU. kmμviFI

/*-----Prog5_11.C -------*/ #include <conio.h> #include <stdio.h>

Rbkasm:aRkU

/* Msg*/ #define Msg printf(“\nWelcome to you”)

Rbkasm:aRkU

/* Ext*/ #define Ext “\nPress any key to exit the program !” void main() { clrscr(); Msg; printf(“\nHow to use macro in C”); printf(Ext); getch(); }

lT§plelIeGRkg;KW ³ Welcome to you How to use macro in C Press any key to exit the program !

enAeBl C Compiler cab;epþmI kar Compile enaHCYbCamYykarRbkasm:aRkU BIrKW Msg tMNageGayXøa printf(“\nWelcome to you”) nig Ext tMNageGay “\nPress any key to exit the program !”. enAeBl Compile mkdl;kñúgGnuKmn_ main 130


k¾)anCYbCamYykarehAeQμaHm:aRkUmkeRbIKW Msg; nig printf(Ext);. enAeBl CYb Msg enaH Compiler BMuTan; Compile eT vaRtUvCMnYssMNMu Statement edImKW printf(“\nWelcome to you”); sin (EdlsMNMu Statement Edl)anCMnYscUleBl enHKWCa Statement EdlmanmuxgarbgðajXøa “Welcome to you” elIeGRkg;) rYc eTIb Compile bnþeTot. kñúgxN³Edl Compile mkdl;kareRbIma: RkU Ext eBlenaH Compiler k¾RtUvGnuvtþdcU eBlCYbeQμaHm:aRkUmnu enHEdr KWCnM sY eQμaH Ext edaysMNuM Statement edIm EdleBlenHKW “\nPress any key to exit the program !”. eRkay eBlCMnYscUlrYceTIb Compiler eFVIkar Compile Statement enaH. edIm,Ikan;Et c,as;EfmeTotGMBIkarCMnYsm:aRkUedaysMNMu Statement edImenaH cUrBinitüemIlrUb xageRkamenH ³

xageRkamenHCakmμviFImYyeTot EdlbgðajGMBIkareRbIm:aRkUkñúgTMrg;mYy Rbhak;RbEhleTAnwgkareRbIGnuKmn_. kmμviFI

/*----Prog5_12.C ------*/ #include <stdio.h> #include <conio.h> #define num 3 #define print(a) printf(“The result of calculation is %d”, a) #define calculate(y) ((y)*(y)*(y)) void main()

RbkasGefr x nig z */ x = num; /* eBlenHeyIg)an x = 3 */

{ int x , z ; /*

131


z = calculate(x) ; /*

ehAm:aRkU calculate eyIg)an z = 27 */

clrscr(); print(z) ; /* getch();

ehAm:aRkU printf */

}

lT§plelIeGRkg;KW ³ The result of calculation is 27

enAkñúgkmμviFI Prog5_12.C xagelIenHmankarRbkasm:aRkUcMnYnbIKW num, print(a) nig calculate(y). kñg ú GnuKmn_ main karehAm:aRkUelIkTImYyKW x = num EdleBlenaH Compiler RtUvCMnYskEnøg num enaHedaytMél 3 rYcehIyk¾P¢ab; eGayeTA x eTIbnaMeGay x = 3. CamYym:aRkUBIreTotKW ³ 1.

print(a)

Ö

enaHGgÁkMNt;Tis #define nwgbMElgeTACa printf(“The result of calculation is %d”, a)

]bmafaeKehAm:aRkUenHeRbIdUcxageRkam ³ print(3)

eBlenaHGgÁkMNt;Tis #define nwgbMElgeTACa ³ printf(“The result of calculation is %d”, 3) 2.

calculate(y)

Ö

enaHGgÁkMNt;Tis #define nwgbMElgeTACa ((y)*(y)*(y))

]bmafaeKehAm:aRkUenHeRbIdUcxageRkam ³ calculate(3);

eBlenaHGgÁkMNt;Tis #define nwgbMElgeTACa ³ ((3)*(3)*(3))

eBlenHeyIgeXIjfa CamYynwgkareRbIm:aRkUEbbenH KWmanlkçN³Rbhak; RbEhleTAnwgkareRbIR)as;GnuKmn_Edr k¾b:uEnþcUrkMucat;Tukfa enHKWCaGnuKmn_eGay esaH. 132


Gñkk¾GacRbkaseRbIm:aRkU)andUcTMrg;xageRkamenH ³ ]TahrN_fa ³ #define product(m, n) ((m)*(n))

]bmafaeRkayeBlRbkasrYc kñúgkmμviFIGñk)aneRbI ³ result = product(x, y+1)

enAeBlenaHGgÁkMNt;TisnwgeGay Compiler bMElgeTACa ³ result = ((x)*(y+1))

ª

cUrRby½tñkñúgkareRbIR)as;m:aRkU ³

enAxagcugbMputrbs;GgÁkMNt;Tsi #define KμansBaØacucek,ós (;) eT. GñkRtUvsresrFatunImYy²enAkñúgrgVg;RkckeGay)anc,as; minsresrkñúgrgVg;RkckeT enaHGacnaMeGaymankarCMnYsxus. ]bmafaGñkRbkas ³

ebIsinCa

#define calculate(y) (y*y*y)

bnÞab;mkGñkmanehAeRbI ³ calculate(a+b)

enAeBlenaHGgÁkMNt;Tis)anbBa¢aeGay Compiler bMElgeTACa ³ a+b*a+b*a+b

EdllT§plenHxusBIkarcg;)anrbs;Gñk BIeRBaHlT§plEdlGñkrgcaMKW ³ (a+b)*(a+b)*(a+b)

ª

m:aRkUmYycMnYnEdleKeRcIneRbI ³

kñúgkarsresrkmμviFI manm:aRkUmYycMnYnEdlEtgEtCYbRbTHjwkjab;CageK enaH RtUv)anbgðajenAxageRkamenH ³ #define ESC 0x1B /* begáItm:aRkU ESC tMNageGay Key ESC edlman ASCII = 0x1B Hex = 27 Dec*/

begáItm:aRkU TRUE tMNageGayPaBBitrbs;tká */ #define FALSE 0 /* begáItm:aRkU FALSE tMNageGayPaBminBitrbs;tká */ #define PI 3.14159 /* begáItm:aRkU PI tMNageGaytMél 3.13159 */

#define TRUE 1 /*

133


begáItm:aRkU ON tMNageGaysPaBebIkrbs; Transistor */ #define OFF 0 /* begáItm:aRkU OFF tMNageGaysPaBbiTrbs; Transistor */

#define ON 1 /*

VIII.3. etIKYeRbI

GnuKmn_ b¤ ma:RkU

lkçN³dUcKñarvagm:aRkU nigGnuKmn_ ³ kareRbIeQμaHrbs;GnuKmn_b¤k¾m:aRkU eFVIeGayGñksresrkmμviFIGaceFVIkargar )anya:geRcIn EdlminRtwmEtsnSM)annUveBlevlla EfmTaMgGaceCosvag)ankMhsu mYycMnYn nigmüa:geTotvaeFIVeGaykmμviFImanlkçN³gayemIl gayyl;.l. PaBxusKñarvagm:aRkU nigGnuKmn_ + kmμvFi EI dlsresrCamYym:aRkU enaHRtUvcMNay Memory sMrab; Code Segment eRcInCagkmμviFIEdlsresrCamYyGnuKmn_ BIeRBaHeBlCYbeQμaHm:aRkU enAkEnøgNa enaH Compiler ng w CMnYskEnøgenaHedaybNþa Statement edImrbs;va. dUecñHebIkúñgkmμvFi CI YbeQμaHm:aRkU n dg enaHk¾RtUvCMnYs Statement edImrbs;vacMnYn n dgEdr EdlenHCamUlehtunaMeGayeRkayeBl Compile rYc kUdrbs;kmμvFi mI anRbEvg EvgCagkmμvFi EI dlsresrCamYyGnuKmn_ BIeRBaHGnuKmn_RtUv)an Compile EtmþgKt; bnÞab;mkeBlCYbkarehAGnuKmn_enaH Compiler RKan;EtCMnYskEngø ehAeQμaH GnuKmn_enaHedayGasydæankñúg Code Segment rbs;vaCakareRsc. + kmμviFIEdleRbIm:aRkUdMeNIrkarelOnCagkmμviFIEdleRbIGnuKmn_bnþic BIeRBaHvaKμancMNayeBlsMrab;karehA (cUrEsVgyl; dUckmμviFIEdleRbIGnuKmn_ GMBIdMeNIrkarrbs; CPU Instruction CALL enaHGñknwg)aneXIjfa enAeBlehA GnuKmn_ etI CPU eFVk I ary:agdUcemþc? GñkGacrkemIlva)ankñúgbNþaesovePAniyay GMBIPasa Assembly). + ya:gNamij GñkKYreRbIma: RkUEtCamYybNþMú Statement gay²)anehIy ebIbNþMú Statement EvgeBk enaH #define nwgKμan\T§iBl (minGaceRbI)an) eLIy.

134


IX. Statement library rbs; function :

k¾dUcCabNþaPasasresrkmμviFIepSgeTotEdr/ Turbo C manbNþa function CaeRcInEdlRtUv)ankMNt; niymn½yCaeRsckñúg C . eKEckbNþa Function TaMgenaHCabNþaRkum² ehIyTukvakñúgbNþa file header, xageRkamenH CabNþa file standard library : stdio.h library énbNþa function Input / output math.h library énbNþa function KNitviTüa stdlib.h library énbNþa function update memory string.h library énbNþa function rbs; string ctype.h library énbNþa function convert character . X.

bNþaGnuKmn_ EdlmanRsab; X.1. random

(Build-in Function)

function

+ Syntax : int random (num) :

Ca function begáItbNþacMnYn Random enAcenøaHBI

0 dl; num–1 begáItcMnYn Random enAcenøaH 0 dl; 32767 + Syntax : randomize ( ): eFVIeGaybNþa function random ( ) nig rand ( )begáItnUvbNþa elxepSg²Kña . + Syntax : rand ( ):

Example : # include <stdlib.h> # include <stdio.h> # include <time.h> int main (void) { randomize ( ) ; printf (" \n random in range ( 0 – 99 ): %d \n” random (100)); return (0); }

135


bNþa GnuKmn_ énry³eBl (DateTime Function) ³ CabNþa Function begáItnig Read ry³eBl/ begáItnig Read éf¶ - Ex . bNþa Function enHpÞúk kñúg <DOS.H> . kñúg Turbo C kMNt;niymn½y TMrg;rbs;éf¶/ Ex nigry³eBldUcxageRkam ³ * éf¶/ Ex ³ X.2.

struct date { int year; char date; char month; };

/* Current year */ /* date of the month */ /* month ( 1 = Jan ) */

¬ykéf¶/ ExBI System ) ¬ kMnt;éf¶Ex ¦ void setdate (struct date * datep ); Example : ykéf¶/ ExBI System rbs;kMuBüÚT½r ³

void getdate ( struct date * datep );

# include < dos.h> # include <stdio.h> # include <conio.h> int main (void ) { struct date d ; getdate (&d) ; printf (“The current year is %d \n”, d.da_year ); printf (“The current date is %d \n”, d.da_day ); printf (“The current month is: %d \n”, d.da_mon ); getch ( ); return (0); }

kMNt;éf¶/ ExeGaykMuBüÚT½r ³

Example : # include <process.h> # include <conio.h> # include < dos.h> int main (void) { struct date reset ; struct date save_date ; getdate ( &save_date); printf ( “day, month source : \n”); system ( “date”); reset.da_year = 2001 reset.da_day = 1; reset.da_month = 1;

136


setdate (&reset ); printf ( “day month have set : \n”); system ( “date”); setdate (&save_date ); printf (“return to day month source : \n”); system ( “date”); getch ( ); return (0); }

Ca Function call statement DOS . Example : System (“dir”) eRbI call statement dir rbs; dos . Function RtUv call :

Note: Function System

# include <stdio.h> # include <process.h> 10. c)

TMrg; Variable énry³eBl nig bNþa Function EdlmanTMnak;TMngCamYy struct time { unsigned char unsigned char unsigned char ti_hund; unsigned char ti_sec; };

/* minutes */ /* hours */ /*hundredths of seconds */ /* Seconds */

CamYy Function Read nig set ry³eBl ³ ry³eBl ¦ settime (struct time * timep); ( Set ry³eBl ¦ Example : Function Read ry³eBl ³

void gettime (struct time * timep ); (Read void

# include <conio.h> # include <stdio.h> # include <dos.h> int main (void ) { struct time t ; gettime (&t); printf ( “Current time is : %2d : %02d : % 02d. %02d \n”), t.ti_hour, t.ti_min, t.ti_sec, t.ti_hund ); getch ( ); return (0 ); } Example : Function set time: # include <stdio.h > # include <conio.h>

137


# include <dos.h> int main (void ) { struct time t ; gettime (&t) printf (“current minutes is %d \n”, t.ti_min); printf (“current hour is %d \n”, t.ti_hour); printf( “current seconds is %d \n”, t.ti_sec ); printf ( “hundredths of seconds \n”, t.ti_hund); /* add 1 minute and call setting */ t.ti_min ++; settime (&t); getch ( ). return (0); }

=====*=====

138


lMhat;Gnuvtþn_ cUrsresrkmμviFIedayeRbI Function, edaHRsay rWssmIkardWeRkTI 2 ax Function smmUlnwgkrNI delta positive, delta negative, delta zero . 2/ cUrsresrkmμviFIedayeRbI Functin, sresrtarag n! Edl n = 1... 14 1/

n 1 2 3 4 5 6 7

2

+ bx + c = 0

n! 1 2 6 24 120 720 ...

eRkaymkcUrsakl,gemIlRbePT int etIGacKNna)anb:unμantY ? ebI long int etIGacKNna )anb:unμantY ? 3/ sresrkmμviFIedayeRbI Function KNna a Edl a: Real; n : CacMnYnKt;viC¢man/ tamBIrrebob ³ + KNnaedaypÞal;/ mincaM)ac;eRbI recursion + KNnatam recursion 4/ sresrkmμviFIedayeRbI Function KNnaRklaépÞénbNþakaer:/ rgVg;/ ctuekaNEkg/ edaybegáItCa menu edIm,I select KNnadUcxageRkam ³ n

0. Exit program 1. 2. 3.

KNnaRklaépÞkaer: KNnaRklaépÞrgVg; KNnaRklaépÞctuekaNEkg]

“press select ” 5/

cUrsresr Function KNna n! eRkaymksresrkmμviFIeBjeljedIm,IKNna ³ Cnk = n! / ( k! * (n-k)!)

6/

begáItsVIt Fibonacci sVIt Fibonacci CasVIt

F1, F2, F3, ... Fn

RtUvbegáItedayeRbIrUbmnþ ³

Fn = Fn-1 + F n-2

139


Edl F1 = 1, F2 = 1 ]TahrN’ 1, 1, 2, 3,5, 8, 13, 21 cUrserskmμviFIedayeRbI Function KNna Maximum nig

minimum

én #cMnYn Read

from Keyboard 8/

eKeGayGnuKmn¾ kñúgenaH

f(x,t) = |a-b| (a³ + b-3) a = log3 (x² + Sin² (x) + |tg (x)| + s)

EpñkKt;rbs; y) nig x,y Ca Variable RbePT Real . a. cUrsresr Function KNna f(x,y) b. KNna f(x,y) Edl x =1.234, y = 2. 345 etIkarbegáItGnuKmn_kñúgkarsresrkmμviFI manRbeyaCn_y:agdUcemþc? 9/ 10/ GVIeTAEdlehAfa )a:ra:EmRt nigGaKuym:g; (Argument) ? cUrniyay GMBIkaredaHdUr Tinñn½yrvag)a:ra:EmRt nigGaKuym:g;. 11/ etIBaküKnøwH return manmuxgardUcemþcEdr? 12/ cUreGayniymn½yeTAelI ³ GefreRkA (Gloable Variable) GefrdMbn; (Local Variable) GefrdMbn;sþaTik (Static local variable) 13/ GVIeTAKWCam:aRkUkñúgPasa C? etIm:aRkUxusBIGnuKmn_dUcemþcEdr? 14/ cUrbegáItGnuKmn_edIm,IedaHRsayRbB½n§smIkarBIrGBaØtþixageRkam ³ b =(

⎧ax + by = c ⎨ ⎩dx + ey = f

kñúgenaH a, b, c, d, e, f KWCa)a:ra:EmRtEdlRtUvbBa©ÚltMéleGayva. =====*=====

140


Turn static files into dynamic content formats.

Create a flipbook
Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.