emeronTI1 ³
sBaØaNdMbUgénPasa
C bc©úb,nñenHmanPasasresrkmμviFIkMritx<s; (High Level Language)CaeRcIn dUcCa C, Pascal, Basic, Java .l. kñúgenaH C manlkçN³BiessmYycMnYn dUcxageRkam ³ + C CaPasasresrkmμviFImYyEdlmanGnuPaB nigPaBrs;revIkx<s;. C RtUv)aneK eRbIedIm,IbegáItRbB½n§dMeNIrkar (OS – Operating System) sMrab;kuMBüÚT½r/ kmμviFIbkERbPasa sresrkmμviFI (Compiler Program) rYmTaMgkmμviFIeRbIR)as;mYy cMnYneTot. + eKeRbIPasa C edIm,IeFVIbNþakargarxagelIenHk¾eRBaHEtvamanRbsiT§ PaBx<s;eRkay eBlEdl)an Compile rYc. kmμviFI C mYyeRkayeBl Compile rYc manrhUteTAdl; 80% énkUdedIm RtUv)anbMElgeTACaPasakMuBüÚT½r. + C CaPasaEdlpþll ; T§PaBeGayeyIgGacbMlas;T)I any:aggayRsYl )ann½yfa ebImankmμviFI C mYyRtUv)aneKsresrbegátI enAelIma: sIun IBM enaHeyIg GacykvaeTAeRbI elIm:asIunepSgeTotdUcCa VAX k¾)an edayRKan;EtGacmankar EksMrYlbnþicbnþÜcb:ueNÑaH.
Pasa PasadéTeTot. +
C
manBaküKnøHw
(Key
Word)
tictYcb:eu NÑaH ebIeRbobeFobCamYy
pþll; T§PaBeGayeyIgGacsresrbegátI CakmμviFIrg (Sub-program) EtmYyRbePTKt;KW RbePTGnuKmn_ (Function) ehIyeyIgGaceRbIR)as;GnuKmn_Edl )anbegátI enaHCaeRcInelIkeRcInsarenAkñúgkmμvFi EI tmYy b¤kmμvFi N I amYyepSgeTot. +
C
I.
bNþaktþacaM)ac;enAkñúgPasa
I.1
kareRbIR)as;tYGkSrenAkñúgPasa
C
C
RKb;PasasresrkmμviFITaMgGs; suT§Et)anekIteLIgedaysarsMNMuénGkSr NamYy EdltYGkSrnImYy²RtUv)anpÁMúcUlKñatamTMrg;kMNt;edIm,IbegáIt)anCaBakü. eRkayeBl)anCaBaküehIy eTIbBaküTaMgenaHRtUv)aneKdMerobtamlMnaMNamYyeTot begáIteTACaXøabBa¢a (Statement). cMENkÉkmμviFIvij rYmmaneRcIn Statement Edl Statement TaMgenaHRtUv)andMerobeTAtamlMnaMNamYyedIm,IsMEdgeGaykar edaHRsaybBaða b¤cMeNaT. Pasa C RtUv)aneKbegáIteLIgenAelIsMNMutYGkSrxag eRkam rYmman ³ 26 GkSrFM (Upper case) : A, B, C, ..., Z 26 GkSrtUc (Lower case) : a, b, c, ..., z 10 tYelx (Number) : 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 sBaØaBiess : , . ; : / ? [ ] { } ! @ # $ % ^ & * ( ) + = - < > ‘ “ ” sBaØa Underscore : _ bNþasBaØaEdlKμanrUbrag (emIlmineXIj) dUcCa : Enter, Space, Tab cUrGñkkt;cN M aMfa ³ kñgú Pasa C mankarEbgEckGkSrtUc nigGkSrFM GñkminGac eRbIvalayLMKña)aneT. ]TahrN_dUcCa printf enaHBMuGnuBaØateGayGñksresrfa PRINTF b¤k¾ Printf )aneLIy.
14
I.2
BaküKnøwH
(Key word)
BaküKnøwHKWCabNþaBaküEdlmanGtßn½yy:agsMxan; nigCak;lak;bMput vaman muxgarsMrab;EteFVIGVImYyb:ueNÑaH BMuGnuBaØateGayeyIgeRbIvaeTAkñúgeKaledAepSg )aneLIy. CaFmμta BaküKnøwHEtgRtUv)aneKeRbIeFVICaRbePTTinnñ y½ b¤eRbICakarI b¤k¾eRbICabBa¢aNamYydac;xatEtmþg. xageRkamenHCabNþaBaküKnøwHEdlmankñúg Pasa C : asm
else
interrupt
struct
break
enum
long
switch
case
extern
near
typedef
cdecl
far
pascal
union
char
float
register
unsigned
const
for
return
void
continue
goto
short
volatile
default
huge
signed
while
do
if
sizeof
double
int
static
cMENkÉrebobeRbIR)as;rbs;bNþaBaküKnøHw xagelI mþgmYy²cab;BIemeroneRkayenHeTA. I.3
eyIgnwgelIkykmksikSa
karkMNt;eQμaH
kñúgkarsresrkmμviFI eyIgEtgEteCosminputBIkardak;eQμaHeGaybrimaN GVImYy dUcCadak;eQμaHeGay Gefr/ cMnYnefr/ GnuKmn_/ pøaksBaØa .l. EdleQμaH TaMgenaHRtUvEtbegáIttamlMnaMxageRkam ³ + eQμaHKWCaes‘rI Character EdlGacmanlayLMKañ rvagtYGkSr (Letter) nig tYelx (Number) b¤GacmanlayCamYynwgsBaØa Underscore EfmeTot. 15
eQμaHRtUvEtcab;epþImeLIgedaytYGkSr (Letter) b¤sBaØa Underscore + eQμaHBMuGnuBaØateGayCan;KñaCamYyBaküKnøwH + cMnYn Character Gtibrimarbs;eQμaHRtUv)ankMNt;eTAtamtMélEdl )ankMNtk; úñgRbGb; Identifier length (Menu Option > Compiler > Source) Edl CaFmμtatMélmanRsab;rbs;vaKW 32. xageRkamenHCabNþaeQμaHEdlRtUv)aneKdak;RtwmRtUv ³ +
a_1
BETAx1
Delta_7_x1
_2Me
xageRkamenHCabNþaeQμaHBMuRtwmRtUv ³ 3XYZ_7 (cab;epþImeLIgedaytYelx) r#3 (maneRbIsBaØaBiess #) f(x) (maneRbIsBaØavg;Rkck) case (Can;CamYyBaküKnøwH) be ta (maneRbIR)as;sBaØadkXøa Space) X-1 (maneRbIR)as;sBaØadk)
cMNaM ³
C mankarEbgEckGkSrtUcFM )ann½yfa _2Me xusBI _2me nigxusBI _2mE
II.
begáItkmμviFIdMbUg
eBlenHdl;eBlEdlGñkRtUvcab;epþmI EsVgyl;GMBkI arbegátI kmμvFi eI hIy. xag eRkamenHnwgbgðajGMBIlMnaMénkarbegáItkmμviFId¾samBaØmYy EdlkmμviFIenHman muxgarRKan;EtbgðajXøa “Hello IIC” elIeGRkg;b:ueNÑaH.
16
kñúgkmμviFIxagelIrYmman ³ • # CaGgÁcg¥úlbgðaj Compiler eGayGnuvtþkargarcaM)ac;NamYysin muneBl Compile EdleKGacehAmü:ageTotfaCa GgÁkMNt;Tis (Pre-processor b¤k¾ Directive). • include eRbIsMrab;ENnaMeGay Compiler eTAGanykGtßny ½ rbs; File Edl)ankMNt;enAxageRkayva (File EdlRtUv)aneKdak;kñúgsBaØa < > b¤sBaØa “ ”) mkdak;bBa©Úl File EdlkMBug Compile edIm,IeFVIkar Compile eTACamYyKña. • stdio.h Ca Header File mYyEdlsresrkat;mkBIBakü Standard Input / Output nigGtßn½yrbs; File enHmanpÞúkkUdrbs;GnuKmn_mYycMnYndUcCa printf, putc, gets.l. enAkñúgkmμvFi maneRbI I Header File “stdio.h” BIeRBaHkñúgtY GnuKmn_ main mankarehAeTAdl;GnuKmn_ printf EdlkUdrbs;GKumn_enHRtUv)an eKRbkasenAkñúg File “stdio.h”. •
void main( ) { }
enHCaGnuKmn_ main EdlRtUvEtmanvtþmanCadac;xatenARKb;kmμviFI TaMgGs;EdlsresredayPasa C. eBlkmμviFImYyRtUv)andak;eGaydMeNIrkar elIkMuBüÚT½r enaHGnuKmn_enHKWCacMNuccab;epþIménkmμviFI )ann½yfa kñúgxN³Edl kmμviFImaneRcInGnuKmn_enaH main RtUv)anehAmkeRbImuneK bnÞab;mkeTIbQaneTA dl;bNþaGnuKmn_epSg²eTotEdl)anehAeRbIenAkñúgGnuKmn_ main (eyIgnwgniyay lMGitenAkñúgemeronTI 5). • XøabBa¢a (Statement) dUcEdlGñk)anCYbmþgehIynUvBakü {XøabBa¢a - Statement} Edl mann½yfa KWCabNþMúénBaküEdlRtUv)aneKdMerobeTAtamlMnaMNamYy nigmanmuxgar sMrab;GnuvtþkargarNamYyenaH. Statement rYmmanBIrRbePTKW Statement eTal (Single statement) nigbNþMú Statement (Block statement b¤ Compound statement) :
17
eTal ³ CabNþa Statement EdlKμanpÞúk Statement NamYyepSgeTot ]TahrN_dUcCa Statement P¢ab;tMéleGayGefrNamYy b¤ Statement RbkasGnuKmn_ b¤ Statement elatKμanlkçxNÐ goto CaedIm. + bNþMú Statement : CabNþa Statement TaMgLayNaEdlman pÞúk Statement epSg²eTotenAxagkñúgtYxøÜnva ]TahrN_dUcCa ³ +
Statement
if (. . .) else { ... }
b¤ for( . . . ) { ... }
GtßaFib,ay (Comment) manmuxgareRbIsMrab;GtßaFib,ay b¤sresrkMNt;sMKal;NamYyrbs; GñksresrkmμvFi .I vaBMuEmnCaEpñkNamYyénkmμvFi eI LIy BIeRBaHeBlEdl Compiler eFVIkar Compile mkCYbnwgsBaØaebIkGtßaFib,ayenH enaH Compiler nwgrMlgeTA Compile bnþeTotcab;BIsBaØabiTGtßaFib,ay. )ann½yfabNþaBakü b¤ Statement TaMgLayNaEdlsßitenAkñúgsBaØaGtßaFib,ayenH nwgBMuRtUv)an Compiler eFVIkar Compile eLIy. sBaØaGtßaFib,ayKW /* */ ]TahrN_dUcCa ³ •
/* This is a note in my program */ Compiler nwgmin Compile bNþaBaküEdlsßitkñúgcenøaHsBaØa /* */ enaHeLIy.
III.
bBa¢ÚnkmμviFIrbs;GñkeLIgelIkMuBüÚT½r
18
edIm,IbegáIt)anCakmμviFImYyBIPasa C enAelIkMuBüÚT½rrbs;Gñk)an enaHGñkRtUv GnuvtþbNþakargarxageRkam ³ 9 begáIt File pÞúkkUdedImEdlman Extension .C (sresrbegáItkUdedIm) 9 Compile + Link edIm,IbegáItCa File rYmmYyEdlGacdMeNIrkarelI kMuBüÚT½r)an (*.EXE). 9 dMeNIrkarkmμviFI Â ]bmafa TURBO C RtUv)andMeLIgdak;kñúg C:\TC (viFIdMeLIg TURBO C nwgRtUv)anniyayenAkñúgesckþIbEnßménesovePAenH TMB½rTI 522). III.1
begáIt
File
pÞúkkUdedImrbs;kmμviFI
edIm,IcUleTAdl;mCÄdæanRbmUlpþMú TURBO C enaHenA Command prompt rbs; DOS GñkRtUvvaydUcxageRkam ³ C:\TC>TC nigcuc Enter eBlenaHmCÄdæanRbmUlpþMú TURBO C )anelc ecjCamYynwgkarbgðajbNþa Menu xageRkam ³ File Edit
Run Compile Project Option Debug Break/Watch
RbsinGñkRtUvkareRbI Menu File enaHRtUvcucpÁÜb Menu File )anFøak;cuH nigbgðajGtßn½ydUcxageRkam ³ Load Pick New Save Write to Directory Chang dir OS shell Quit
F3 Alt-F3 F2
Alt-X
19
Key ALT
CamYynwg
F
eBlenaH
edIm,IsresrkUdedmI rbs;kmμvFi I enaHCadMbUgGñkRtUvcUl Menu File bnÞab;mk eRCIserIsyk New edIm,IbegáIt File fμIsMrab;pÞúkkUdkmμviFI. eBlenHmuxgar New RtUv)anGnuvtþ nigkMBu üÚTr½ kMBgu rg;caMTTYlkarvaybBa©lÚ nUvbNþa Statement rbs;kmμvFi .I cUrGñkvaybBa©ÚlnUvbNþa Statement dUc]TahrN_kmμviFIxagelI bnÞab;mkRtUvdak; eQμaHeGay File pÞúkkUdedIménkmμviFIenH edaycucpÁÜbKñarvag Key ALT CamYy F edIm,IcUleTA Menu File vijrYcehIyeRCIserIsykmuxgar Write To eBlenaHpÞaMg Write to k¾)anbgðajelIeGRkg;. New Name
eyIgcaM)ac;RtUvkMNt;eQμaH File pÞúkkUdkmμviFIenH ]TahrN_fa TEST1 bnÞab;mkRtUvvaybBa©Úl TEST1 enaHeTAkñúgRbGb; New Name xagelIrYcehIycuc Enter. eBlenHkUdkmμviFIrbs;GñkRtUv)anrkSaTukenAelIfasrwgkñúg File Edlman eQμaHfa “TEST1.C”. dl;RtwmenHkarsresrkUdénkmμviFIrbs;GñkGaccat;TukfaRtUv)ancb;. cMNaM ³ eyIgGacEk File pÞúkkUdNamYyeLIgvijk¾)an edaycUleTA Menu File (cucpÁbÜ Key ALT CamYy F) rYcehIyeRCIserIsykmuxgar File-Load edIm,IebIk yk File cas;EdlRtUvEkenaHmkbgðajelIeGRkg;. enAeBlvasYreQμaH File enaHcUrvaynUveQμaH File EdlRtUvebIkedIm,IEkenaH rYcehIy Enter. eBlvayeQμaH File RtUvebIkenaH RbsinebIGñkmin)anbBa¢ak; Extension rbs; File eGay)anc,as; las;eT kmμviFInwgyl;fa Extension rbs; File enaHKW C.
20
III.2
begáItCa
File
EdlGacdMeNIrkar)an
edIm,IbegáIt)anCa File GacdMeNIrkar)an enaHGñkcaM)ac;RtUvEteRbI Menu Compile. edIm,IclU eTAdl; Menu Compile GñkRtUvcucpÁbÜ Kñarvag Key ALT nig C enaH Menu Compile RtUv)anbgðajCamYybNþamuxgardUcxageRkam ³ Compile to OBJ Make EXE file Link EXE file Build all Primary C file Get into
cUreRCIserIsykmuxgar Primary C file rbs; Menu Compile edIm,IkMNt; eQμaH File EdlnwgRtUv Compile eBlenaHpÞaMg Primary file )anbgðajdUcrUbxag eRkam ³ Primary File *.C
cUrvaybBa©ÚleQμaH File RtUv Compile EdlkñúgkrNIenHeQμaH File enaHKW TEST1 rYcehIycuc Enter. bnÞab;mkeTotcUrGñkeRCIserIsykmuxgar Make EXE file edIm,IbkERbkUdedIménkmμviFIeGayeTACaPasam:asIun ehIytP¢ab;CamYybNþa Library Function mYycMnnY eTotedIm,IbegátI eTACa EXE File. eRkayeBleRCIserIs ykmuxgar Make EXE file rYcenaHkMuBüÚT½rnwgcab;epþÍmGnuvtþkargareTAtammuxgar dUc)anniyaymunenH ehIyeBlenaHRbsinebIkñúgkUdkmμviFImankMhusNamYy enaH kMhusTaMgenaHnwgRtUv)anbgðaj ehIyGñkRtUvRtlb;eTAcMNucTImYyvijedIm,IeFVIkar EksMrYl. b:uEnþRbsinebIKμankMhusNaTaMgGs; enaHeyIgnwgTTYl)ankmμvFi RI twmRtUv mYyEdl)anrkSaTukelIfaskñúg File eQμaH “TEST1.EXE”. File Edlman Extension *.EXE KWCa File EdlGacdMeNIrkarelIkMuBüÚT½r)an. eBlenHkarbegáIt File dMeNIrkar Gaccat;TukfaRtUv)anbBa©bR; twmenH dUecñHGñkGaccakecjBImCÄdæan RbmUlpþMú TURBO C eday ³ 21
cUldl; Menu File edayycuc ALT+F + eRCIserIsykmuxgar Quit rbs; Menu File eBlenaHGñkGacnwgcak ecj)anPøambnÞab;BIcuc Enter. edIm,Icakecj)anelOnbMput GñkRKan;Etcuc Alt+X CakareRsc. srubeLIgvijTaMgBIrcMNucxagelI eyIgTTYl)an File BIrKW TEST1.C nig TEST1.EXE. edaysarkuMBüÚTr½ kMBugeFVkI arkñúg Directory “C:\TC” dUecñH File TaMgBIr xagelIk¾RtUv)anTukkñúg Directory “C:\TC” Edr. +
III.3
dMeNIrkarkmμviFI
edIm,IdMeNIrkar File kmμvFi N I amYy CadMbUgGñkRtUvcUleTAdl; Command prompt rbs; DOS bnÞab;mkeTotvayeQμaH File kmμviFIenaH ]TahrN_dUcCa TEST1.EXE CaedIm ehIycuc Enter eBlenaHkmμviFI TEST1 nwgRtUv)andMeNIrkar elIkuMBüÚT½r. cMNaM ³ + GñkGacdMeNIrkarkmμviFI (eRkayeBlEdl)ansresrkUdvarYc) enAkñúg mCÄdæanRbmUlpþMú TURBO C (mincaM)ac;Rtlb;eTA Command prompt rbs; DOS) edayRKan;EtcucpÁÜbKñarvag Key CTRL nig F9 b:ueNÑaH. + BIkúñgmCÄdæanRbmUlpþMú TURBO C eyIgGacRtlb;eTAkan; Command prompt rbs; DOS )antamBIrrebobKW ³ • eRbImuxgar Quit rbs; Menu File enaHGñknwgRtUv)ancakecjBI mCÄdæanRbmUlpþMú TURBO C Rtlb;eTAkan; DOS Prompt. enAeBlcg;cUl eTAdlm; CÄdaæ nRbmUlpþMú TURBO C vji enaH BI Command prompt rbs; DOS GñkRtUvvay C:\TC>TC rYcehIy Enter. • GñkGaceRbImuxgar File-OS Shell edIm,IcakecjBImCÄdæan RbmUlpþMú TURBO C eTAkan; DOS Prompt k¾)an. muxgarenHeRbIsMrab; cakecjBImCÄdæanRbmUlpþMú
22
cUleTA DOS Prompt CabeNþaH GasnñehIyenAeBlNaRtUvkarRtlb;mkkan;mCÄdæanRbmUlpþMú TURBO C vijenaH enAkñúg Command prompt rbs; DOS GñkRKan;EtvaybBa¢a EXIT rYc Enter CakareRsc. TURBO
C
sMNYr GnuvtþsresrkmμvFi EI dl)anski SaenAkñúgcMNuc II GMBIlT§plEdlTTYl)an. 2. cUrsresrkmμviFIbgðajelIeGRkg;nUvBIrXøa xageRkam ³ 1.
rYcehIycUrEsVgyl;
“Hello Cambodia” “Welcome to visit Phnom Penh” 3.
cUrsresrkmμviFIxageRkamelIkMuBüÚT½r ³ #include <stdio.h> #include <conio.h> void main( ) { printf(“\nThis is my first program !”); printf(“\nPress any key to exit...”); getch(); }
cUrBinti üemIllT§plelIeGRkg; ehIyeFVkI areRbobeFobCamYyBIrkmμviFI Edl)anbegáItmunenH etIkmμviFITaMgbIenHmanGVIepSgKña? 4. cUlsresrnUvBakü Good morning , Good afternoon , Good Evening , mkelI Screen Output : # include < stdio.h> # include < conio.h> main ( )
23
{ clrscr(); printf (“Good Morning .\n”) ; printf (“Good Afternoon .\n”) ; printf (“Good Evening.\n”) ; getch( ) ; return (0) ; } 5.
sresrkmμviFIKNna b¤skaer:éncMnYnKt;mYy ³ # include <stdio.h> # include <conio.h> # include <math.h> main ( ) { int i ; float x; printf (“\nEnter a number :”); scanf (“%d”,&i); x = sqrt (i); printf (“\n Square Root of %d is %10.3f \n”,i, x); getch ( ); return (0); }
ehtuGVI GñkRtUvsÁal; nig caMbNþa Keyword rbs;Pasa 7. Compiler program manb:unμanRbePT ? 8. Source program CaGVI? 9. Linker Program mannaTICaGVI ? 6.
===== *=====
24
C?