2. www.kompongsom4u.blogspot.com IT Book C Programming
?????????? 070 908 739
?????????????????
& ???????? ? ? ??????????? ???? C PROGRAMMING '
&'
?.?. ???????
C PROGRAMMING ???? Program Language ???????????????????????????????????????????????
????????????? ???????????????????????????? ?
?.?. ?????????????????? Dev C++
?????????????????????????????????????? Dev C++ ??????????????????????????????
?. ??? Start Menu
?. All Program
?. Blooddshed Dev C++
?. ??? Tick ?????????? Don?t Deplay tips as Startup
?. ??? Close
3. www.kompongsom4u.blogspot.com IT Book C Programming
?????????? 070 908 739
?????????????????
?.?. ??????????? Source File
???????????? Source File ???????????????????? ?
?. ??? File Man u
?. New
?. Source File
?.?. ???????? Source File
?????????? Source File ???????????????????? ?
?. ??? File Man u
?. Save ? Save As
?.??? File Name ??????????? ?? (nak.cpp)
?.??????????????????????????? File
?. ??? Button Save
?.?. ???????? Cord ???????
Source Code: Output
#include<stdio.h>
#include<conio.h>
main(){
printf ("Hello chhynak.n") ;
printf ("How Are You.n") ;
getch();
return (0);
}
Hello chhaynak
How are you
4. www.kompongsom4u.blogspot.com IT Book C Programming
?????????? 070 908 739
?????????????????
?.?. ????????????? Source Code
- #include<stdio.h> : ?????? Standard Input/output ??????????????? Function ???
?? Printf ????????????? Prinf ?????????????? #include<stdio.h> ???? ???????????
Header file ???????? ?
- #include<conio.h> : ?????? Function??????????? Getch();??????? Prinf ?????????
????? #include<stdio.h>
- main(){} : ?????? Main Fucntion ?????????? ? ???? Code ?????? Program ?????? ?
- Printf : ?????????? ??????????????????? Command CMD
- Getch(): ?????????????? ? ???? Code ?????? Press Anykey ??????????? ? ????????
?.?. ??????????? CORD
Ex.
?.?. ????????? Character
Escape character ? Escape code ??????????????????????????? source code ? ???????? ????????? ??
??????? ?????? ?
Code ???????? Code
t tab ???????? ??
a alert (beep)
n Newline ????????? ??
' single quote (')
? question mark (?)
#include<stdio.h>
#include<conio.h>
main(){
Statement;
..........
getch();
return (0);
}
#include<stdio.h>
#include<conio.h>
main(){
printf ("Hello chhynak.n") ;
printf ("How Are You.n") ;
getch();
return (0);
}
5. www.kompongsom4u.blogspot.com IT Book C Programming
?????????? 070 908 739
?????????????????
?.
Output
?.?. ???????????? COMMENT
?????????????????? Command ?????????????????????? ??????? Programer ???????? Command
???????????????????? Code ??? ?? ?? ????????????????????????
?.?. ?????????????
???????????????????????????????????? ??????????????????????? ?
?. ??? ??????????
?.???
?.????? ??
?.?????????????? ?
?.???????? ?
?.???????
?.??????????
&??????????? ?'
#include <stdio.h>
#include<conio.h>
main(){
printf("Hello ! this is:t "chhaynak"n");
printf("What's are you doing?n");
printf("Working");
getch();
return (0);
}
Hello ! this is chhaynak
what¡¯s are you doing?
Working
/* this command for tell
you what is this > */
6. www.kompongsom4u.blogspot.com IT Book C Programming
?????????? 070 908 739
?????????????????
& ???????? ? ? ???? ??????????????? '
&'
?.? ????????????
?????????????????????????? ????????????????????????? ? ?????????????????????????
???????????????????????? ?
Syntax : Data type Variable
Ex. int a;
?????????????????????????? ?
Name Description Size Range
Short Short integer 2byte Signed:-32768 to 32767
Int integer 4byte Signed:2147483648 to 214748347
Long Long integer 4byte Signed:2147483648 to 214748347
Float Float point number 4 byte -3.4 x 10-38
TO 3.4 x 10-38
Double Double precision floating point number 8 byte +/-1.7e+/-308(~15digits)
Boolean Boolean value 1 byte True or False
Char Character or small integer 1byte Signed:-128 to 127
Code ?????????????????? Test ??????????? Data type
#include<stdio.h>
#include<conio.h>
main( ) {
printf( ¡°Fundamental of size of operator.nn¡± );
printf( ¡°Size of char %3d byte.n¡±, sizeof( char ) );
printf( ¡°Size of int %3d byte.n¡±, sizeof( int ) );
printf( ¡°Size of long %3d byte.n¡±, sizeof( long ) );
printf( ¡°Size of short %3d byte.n¡±, sizeof( short ) );
printf( ¡°Size of signed %3d byte.n¡±, sizeof( signed ) );
printf( ¡°Size of unsigned %3d byte.n¡±, sizeof( unsigned ) );
printf( ¡°Size of float %3d byte.n¡±, sizeof( float ) );
printf( ¡°Size of double %3d byte.n¡±, sizeof( double ) );
printf( ¡°Size of long double %3d byte.n¡±, sizeof( long double ) );
printf( ¡°nPress < Enter to Exit ! >¡± );
getch( );
return ( 0 );
}
7. www.kompongsom4u.blogspot.com IT Book C Programming
?????????? 070 908 739
?????????????????
?.?. ??? ??????
??????????? ?????????????????????????????????? ( Data )???????????? ?????????????? ? ?????
??????????????????????????????? ?
-??????????? A ?? Z ( ??? ?? ?? ) ??? ??? Underscore
-???????? ??????????????? ? Underscore
-???????? ? ?? ??????? Variable Name ?? ????????? ??????? ????????????
-??????????????????? Keyword ? Keyword ???????? ?
Array for return break function sizeof
case goto if constants switch continue
labels pointer union
??????????????????? ?????????????????????????? ?
-total$ : (Illegal character $) ??????????????? $
-2nd_sums : (Begin with a digit) ????????????????????????
-second sum : (Cannot use blank as a character) ?????????????
-Total-Cars : (Illegal character -) ??????????????????? ¡°-¡±
????????????? Output ???????????????? ??????? Printf(); Fucntion ???????? Formation Control Data Type
??????????? ???????????????????? ?
Quantity Data Type
%d, %i Integer (int) ????????
%c Single character (char) ????????????
%s Character string (char) ??????????????
%f Fractional number (float) ???????????
%lf Double fractional number (double) ???????????
%ld Long Integer (long) ????????
?.?.????????????
????????????????????? ????????????? ?????????????????????????????????? ? ????????????
??? ???????????????????????? ??????????????????????? ? ??????????????????????????? ?
Ex. int a,b,sum;
float c;
??????????????? ??????????????????????????? ?????????????? ???????????? ?
Ex char c = ?A?;
int i = 0;
int k = i + 1;
8. www.kompongsom4u.blogspot.com IT Book C Programming
?????????? 070 908 739
?????????????????
?.?.????????????????
1. Arithmetic Operator: ???? Operator ???????? ??????????????????????????????????????????
?????? Program .? Arithmetic Operator ????
Operator Meaning
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulus [remainder of an integer division.]
Arithmetic Operator ???????? ? ???? ??? ??? ???? ?????????????????????????????????? ????
????????????? Modulus ???????????????????????????????????? ?
??????? int a=1;
int b=6;
int c= a+b;
2 ¨C Relational and Equality Operator: ??????????????????????????????????????????????????
????????????????? ???????????????????? ??????? Loop Statement , if-else Staement ,.....?
Relational and Equality Operator ???????? ?
< ?????? ( Less than ) <= ???????????? ( Less than or Equal )
>????? ( Greater than ) >= ??????????? ( Greater than or Equal )
== ????? !=????????
3 ¨C Logical Operator: ????????? ?????????????????? ?????? ?????????
Logical Operator ???????? ?
&& ?? ?????
|| ?? ???
4. Increment and Decrement Operator : ???????? C ?????????????????????????????????????
???????????????????? ( Increment ) ??? ???? ( Decrementing ) ????????????? ? ????????? Increment
(++) ??????????????? ??????????????????? ? ????????? Decrement (--) ???????????????
??????????????????? ?
???????????????????????????????????????????????? ?? ? ?????????????? Constant ?????????
? ??????????????????????????? ?????????????????????? (++n)????? ????????????????? ( n++)?
????? ???? -(++n) ??????????????????????????? ( Ex. n=5 , x=++n , ???????? x=6 )
-(n++)???????????????????????????? ( Ex. x=n++, x=5, n=6)
]TahrN_2?
9. www.kompongsom4u.blogspot.com IT Book C Programming
?????????? 070 908 739
?????????????????
#include<stdio.h>
main ( ) {
int a = 5;
int b = 5;
printf( ¡°Value of a: %d and b: %dn¡±, a, b );
printf( ¡°Value of a: %dn¡±, ++a );
printf( ¡°Value of b: %dn¡±, b++ );
getch();
return ( 0 );
}
RbmaNviFI Prefix nig Postfix nwgdUcK?aenAeBlEdleyIgeRbIvaEt?g
5. Assignment operator: eKmanRbmaNvIFImYycMnYneTotenAelIRbmaNviFIFm?ta EdleKeRbIsMrab; Assigned
(=) [ep?rtM¨¦l]b¡èKNnakenSamepSg?.RbmaNviFIenHKW ?
Operator Simple Equal Meaning
+= a+=b a = a + b a ???????? b ? ?? a ????????????
-= a-=b a = a ¨C b a ??????? b ? ?? a ????????????
*= a*=b a = a * b a ?? ????? b? ??a ????????????
/= a/=b a = a / b a ???????? b ? ?? a ????????????
%= a=b%b a = a % b
a ??????? b ? ?? a
???????? ?????????????? a
??? b
]TahrN_3 ?
#include<stdio.h>
main ( ) {
int a = 1;
int b = 5;
printf( ¡°Value of a: %d and b: %dn¡±, a, b );
a += b;
printf( ¡°Value of a: %dn¡±, a );
b %= 3;
printf( ¡°Value of b: %dn¡±, b );
b *= a + 1;
printf( ¡°Value of b: %dn¡±, b );
b /= a - 1;
printf( ¡°Value of b: %dn¡±, b );
getch();
return ( 0 );
}
?????? ? ???????? ??? Output ??? Program ???????? ?
1. ?????????????????????????? ?
-???????????????????? ( S=a*b)
-?????????? S=a+b+d+d
-?????????????????? S=a*a
10. www.kompongsom4u.blogspot.com IT Book C Programming
?????????? 070 908 739
?????????????????
& ???????? ? ? Control Structure '
&'
?.?.If Statement
?????? Expression ??? (Expression ??????????????????? ) Statement ?????????????? ???????????
????? ??? Expression ?????? ( Expression ???????????????? ) ????????????? Statement 2 ? ????????????
C Programing Statement ????????????? ??????????? Semicolon ¡° ; ¡± ?????????? Block Statement ?????
????????????? Braces ¡°{???}¡± ????????????????? ? ??????? if Statement ??? Syntax ??????????? ?
Syntax1:
Syntax2:
??????? ? ?
#include<stdio.h>
main( ) {
int x;
clrscr( );
printf( ¡°Input x:¡± );
scanf( ¡°%d¡±, &x );
if ( x == 10 ) {
printf( ¡°n This is a good student.¡± );
}
return ( 0 );
}
??????? ? ?
#include<stdio.h>
#include<stdlib>
main( ) {
int magic, guess;
printf( ¡°nPlease input Guess value: ¡° );
scanf( ¡°%d¡±, &guess );
magic = rand( );
if ( guess == magic )
printf( ¡°n You are right.¡± );
if ( guess < magic )
printf( ¡°n You are too low.¡± );
if ( guess > magic )
printf( ¡°n You are too high.¡± );
printf( ¡°n %d is the magic value¡±, magic );
getch( );
return 0;
}
if(expression )Statement;
Statement2;
if(expression ){
Statement1;
Statement2;
..........
..........
}
17. www.kompongsom4u.blogspot.com IT Book C Programming
?????????? 070 908 739
?????????????????
Ex 1.
Ex 2.
#include<stdio.h>
#include<conio.h>
void main(){
clrscr();
int n;
printf("Input N=");
scanf("%d",&n);
switch(n){
case 1:
printf("Number one");
break;
case 2:
printf("Number two");
break;
case 3:
printf("Number three");
break;
case 4:
printf("Number four");
break;
default:
printf("The Number biggest is=%d");
}
getch();
}
18. www.kompongsom4u.blogspot.com IT Book C Programming
?????????? 070 908 739
?????????????????
?.? .??????
?. ???????? Outpute Code ???????? ?
int x,y;
x=6;
y=1;
if(x<2)
printf(¡°%dn¡±,x);
else
printf(¡°%dn¡±,y);
?. ???????????????????????
-??????????????????????? Ax+b=0
-?????????????????????????? C ?????????????????????????????????????? ????????? ?????? ?
-????????????????????? Switch ?????????????????????????
&??????????? ?'
19. www.kompongsom4u.blogspot.com IT Book C Programming
?????????? 070 908 739
?????????????????
& ???????? ? ? Loop Control Structure '
&'
???????????????? ???????????? ???????? ???? Repeat Statement ? ??? Block Statement ????????
???????????? ???? Loop ? ?????????? C Loop ????? ????? ?? ?
?. While Loop Statement
?. do_while Loop Statement
?. For Loop Statement
Loop ???????????? Instruction Computer ?????? ???????????????????? ???????????????
?????????????????? Control Variable ??????? ?
?.? .While Loop Statement
??????? While Loop ???????????????? Expression ??????????? ????? Statement ???? While Loop
???????????????( Execute ) ???????????????????? Expression ??????? ????????????????? ????? Statement
? Block Statement ?????????? Execute ???? ??????? Expression ?????? ?
20. www.kompongsom4u.blogspot.com IT Book C Programming
?????????? 070 908 739
?????????????????
??????? ?
?.? .Do-While- Loop Statement
??????? do While Loop ?? Execute ??? Statement ????????????? Loop ?????????????????????
???? Expression ? ????????????????????? Expression ??????? ????? Statement ?????? Execute ???
????????????? ?? ????????????????????? Expression ?????? ?
34. www.kompongsom4u.blogspot.com IT Book C Programming
?????????? 070 908 739
?????????????????
?????????????????? ???????????????????????
&??????????? ?'
36. www.kompongsom4u.blogspot.com IT Book C Programming
?????????? 070 908 739
?????????????????
?.? Pointer to Pointer
??????????? Pointer ?????????????????????? ???? ?????????????????????????? ?? Pointer
???????????? ????? Pointer ?????????????????????????????? ?
??????? ?
?.? Pointer to Function
??????? Pointer ????????????? Address ??????????????? ???????????? Adress ???????????? ?
??????????????????? ????????????
??????? ? ?
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int x = 10;
int *a;
int **b;
int ***c;
a = &x;
b = &a;
c = &b;
printf("value of x = %dn",x);
printf("value of x by using a = %dn",*a);
printf("value of x by using b = %dn",**b);
printf("value of x by using c = %dn",***c);
getch();
}
#include<stdio.h>
#include<conio.h>
void change(int,int*);
void main()
{
clrscr();
int a = 10 , b = 20;
printf("Before calling function.n");
printf("a = %d n",a);
printf("b = %d n",b);
change(a,&b);
printf("After calling function.n");
printf("a = %d n",a);
printf("b = %d n",b);
getch();
}
void change(int x,int *y)
{ x = x * 5;
*y = *y + x;
}
37. www.kompongsom4u.blogspot.com IT Book C Programming
?????????? 070 908 739
?????????????????
??????? ? ?
&??????????? ?'
#include<stdio.h>
#include<conio.h>
int (*psum)(int,int);
int sum(int,int);
void main()
{
clrscr();
int a = 40 , b = 20 ;
psum=∑
printf("Result = %d",(*psum)(a,b));
getch();
}
int sum(int m , int n)
{
int s;
s = m + n ;
return (s);
}
38. www.kompongsom4u.blogspot.com IT Book C Programming
?????????? 070 908 739
?????????????????
& ???????? ? ? File '
?.? ???????
????????? ? ????? ????????????????????????????????????? ??????? ??????????? ????? Hard
Disk , Flash ?????? ? ????????????? C Program File ???? Byte ????????? ????????? ???? ?????? ??????????
???????????????????? ?? Text File ??? Binary File ?
?.? ?????????? File
?.? ?????????? Text File
???? File ????????????????????????? Graphic Character ????????????????????? FIle ???????????
???????????????????????????????? ? ???????? ???????? ? ????????????? File ????????????
File Pointer ????????????????????????? File ???????????? ???????????? ?????????? ? ??????????????
??????? fopen() ?
??????
-FileName : ??????? ?????? File ?????? FileName ??? Execute ???????????????????
-Model : ???? String ?????????????????????????????????????????????????????????
Mod ??????
r/rt ??? Text File ???????????????? , ????? ? FIle ????? error
w/wt ??? Text File ?????????????????? , ????? ? FIle ??????????????
a/at ??? Text File ??????????????????,????? ? FIle??????????????
r+/r+t
??? Text File ??????????? ???????? ? ??????????w+/w+t
a+/a+t
fp=fopen(¡°FileName¡±,¡±Model¡±)
39. www.kompongsom4u.blogspot.com IT Book C Programming
?????????? 070 908 739
?????????????????
?.?.? ???? File ??????????
?????????????????????????????????????????????????????? file ??? ? ??????? putc(), fputc();
??????????????????????? ????????????????????????? File ????? File Pointer ?
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
char ch;
FILE *fp;
fp=fopen("Test1.txt","w");
if(fp==NULL)
printf("Opening file was Error");
else
{ printf("Input character store infile.n");
printf("Press <Esc to stop>n");
do
{
ch=getch();
if(ch!=27)
{
putchar(ch);
fputc(ch,fp);
}
}
while(ch!=27);
}
fclose(fp);
}
40. www.kompongsom4u.blogspot.com IT Book C Programming
?????????? 070 908 739
?????????????????
?.?.? ???? File ?????????
???????????????????????????????????????????????????? file ??? ? ??????? getc(), fgetc();
????????????????????????????????????????????? File ????? File Pointer ?
#include<stdio.h>
#include<conio.h>
void main()
{ clrscr();
char ch;
FILE *fp;
fp=fopen("Test1.txt","r");
if(fp==NULL)
printf("Opening file was Error.");
else
{ printf("Read data from Filen");
ch=getc(fp);
while(ch!=EOF)
{ putchar(ch);
ch=getc(fp);
}
}
fclose(fp);
getch();
}
41. www.kompongsom4u.blogspot.com IT Book C Programming
?????????? 070 908 739
?????????????????
?.?.? ???? Text File ??????????????????
?????????????????????????????????????????????????????? file ??? ? ??????? putc(), fputc();
??????????????????????????????????????????????? File ????? File Pointer ?
#include<stdio.h>
#include<conio.h>
void main()
{ clrscr();
char ch;
FILE *fp;
fp=fopen("Test1.txt","a");
if(fp==NULL)
printf("Opening file was Error");
else
{ printf("Input character to append in
file.n");
printf("Press <Esc to stop>n");
do
{ ch=getch();
if(ch!=27)
{ putchar(ch);
fputc(ch,fp);
}
}while(ch!=27);
}
fclose(fp);
}
42. www.kompongsom4u.blogspot.com IT Book C Programming
?????????? 070 908 739
?????????????????
?.? Binary File
Binary File ??????????????????????? Internal Format ????????????????????? File ?????????????
????????????????? int , long , double , float ?????? ? ??????????? ???????? ? ????????????? File
????????? ??? Text File ??? ???????????????? mode ?????? ?
&??????????? ?'