Page 1

ภภ ภ (Part I) ภ

 Computer Programming     . http://student.sut.ac.th/comclub

 ภ ภภ!! Computer Programming # $!$% ภ &'(ภ)   * )*ภ+ภ ภ ,   ภ ,) * $ ภ (+!  . )ภ(ภ)/0 12*& ,) << !&    !)ภ+ภ  (,) 2* !&,* ??)    $*ภ*4)  ภ/ (&5 (* â&#x20AC;&#x153;&55â&#x20AC;?) *+*! & 4* (6&*)   ,) ,*,  )... (& ,) )) ภ/ (*!&! ) ( (*!&! )  )&  4*! &  ( *!ภ ภ  ภ ! ภ   && ( *!ภ 4*,)  *!&! & ),) . !  (& ),) ภ) 8555 ... ภ+! ภ,& &)1 ... )&,(*!& 855 ภ&  #!  & &( *!ภ  *& ภ( *!ภ  ภ*1 +55 ,) + !, ภ.,(...*& , ) , !)  !).ภ , &ภ+!. )) )!0 ).     !"# ภ$  ( !))& &55 & ! ,*   ...  &  )!*   1. !9'&0:; (ภ!ภ(ภ*:;) 2. ภ) C (Introduction to C) ()&4 < ,) + ภ ... )'=</+) 3. !( ( !ภ (... ,)!* ภ ) 4. !+ ( ภ! 2 (...  ภภ) 5. >: ภ0 (,) * ภ+  ) ภ&  ) 6. )?!( (855 +**ภ4ภ/0* ) ) (Array) 7.  !@<+ , 30    ภ+* 855 -_-!!

" % 2 7 12 21 30 34 38

$%" %ŕ¸

)&ภ*! $*ภ&* 4*,4 &ภ ภ/4 &*,*!&  A! 4 &  ,) *+ภ

Version 1.0.0

ภภ 

10/16/2008 11:01:00 PM


- 2 of 40 -

10/16/2008 11:01:00 PM

ภภ ภ (Part I) ภ

 Computer Programming

" %& 1 !9'&0; :  (ภ!ภ(ภ*:;)

ภ &  !*! *!&ภ ภ!ภ(ภ*,:;$ ภ& ,) ,*24 '(ภ)+!* ()* ( *&)ภ) ภ4 !9 ภ) :; ( (ภ*,:;$ ŕ¸

12*& KAPONG CPE#11 Com-Club Homepage [http://student.sut.ac.th/comclub]


- 3 of 40 -

10/16/2008 11:01:00 PM

ภภ ภ (Part I) ภ

 Computer Programming

'ภ ()" (ภ ภภ%()") ภ(ภ*:;$ !))4  )<&0 ( 55)  ) )<&0 )ภ(ภ*,:;&2 ภ!  (  &  (+B:*!& ...)!!) ( ภ(ภ*,:;ภ* * $ภ(ภ*,:;( &ภ&  ! $(') ( ,) $&   , ( ,) ) ภ.* !9+ภ+*!  ภภภ& , *!&!9 5 $  4 ) ( *! ภ&  .! ,)/ ภ. ภ(ภ*:; ภภ *! 4*4)!  ภ*!& , ภ. !  â&#x20AC;&#x153;ภ.&+ ภ &!5555 ภ â&#x20AC;? 855 ( ภ(ภ*:; @ภ< &+ + )!0 1) &,*&ภ&0  ภ!  +*) !  )!0& )' )ภ55  ),) )4  ,* * &C  +*)   * ) !)+* '  ,* (    * )ภ $ภ!ภ &ภ!&    " 'ภภ%  $, (&ภ!&  6&55 ,) * #&) 1. * 2. ภ*!*!&* 3. &ภ* 4. 4*& ,)  ภภ+* ภ,* 3 ภ( ภ! 4* &  ... 855!&  6&55) 5. *!+*+ +ภ6.  &!*! 7. 4**!& ,) &+*ภ,+* 6 ภ 8. ภ*! 9. 4**!& ,) )+*ภ,)* 1 ภ .,*!  !9ภภ*!($ภ!ภ )   ( )ภ!ภ ภ&* *( &ภ!  ภ& Algorithm ภ&$ภ!ภ) ( ( )   *!& Algorithm * * & ,) ภ ภ Bug ภ,*  &5 & * 5 ) (&* !   *!+ ภ,*,   5&  ,) ,**ภ(855   )  ภ!  4*ภภ&& , ( )ภ!!&!  !   ภ!  ) ,) 4* & ,   (& 5...& ,) ) Algorithm * ภ &ภ!    ภ/   5  ภ/0+*  ภ(ภ*,:;,*)  !) (  & ภ& Algorithm ()2* ภ+*( !  ภ!ภ(ภ*,:; &( *!ภ ... ,) ,*(?) ภ& Algorithm ,)  :&(ภ )4(ภ*,:;+ ภ/  5 ,*)ภ*&( , +)&4 !).!+ภ  Algorithm $ !)ภ!*   ภ/  5 ภภ/0 )* !  Algorithm ( ภ+; , .ภ855 # )&4 ภ) :;(

!), (& &....  )2ภ 12*& KAPONG CPE#11 Com-Club Homepage [http://student.sut.ac.th/comclub]


- 4 of 40 -

10/16/2008 11:01:00 PM

ภภ ภ (Part I) ภ

 Computer Programming   Algorithm  ภ#")-./0ภ&ภ!&     & Algorithm  ภ )ภ !*) 10 ! )* !) ภ!* 5 Algorithm ภ! )ภ!* 5& , * Algorithm ภ%1 1.   ! 10 ! 2. &&! 10  )ภ 3. ( )ภ Algorithm  / 1.   ! 2. 3. 4. 5. 6.

* 1  Algorithm (ภ!* 5 4*& ,)  10 !+*ภ,* 1 * 1  Algorithm (ภ!* 5 +* (ภ$ )ภ * 2  Algorithm (ภ!* 5 && 4,ภ )ภ: 4* )  )ภภ! +* $ )ภ * 2  Algorithm (ภ!* 5 4*& &&,)  10 !+*ภ,* 4 * 2  Algorithm (ภ!* 5 ( )ภ * 3  Algorithm (ภ!* 5

.!  Algorithm )ภ) (ภ!* 5 (ภ!* 5  4*+* ภ.,*'&') #   !))4@< )<&0ภ !,!** * ) 2! $ภ&( &5 ( ( $5)ภ!ภ (   & ,)   * )  &+* &   .,*ภAlgorithm ( & Algorithm * * ) ภ(ภ*:;ภ2( ,*! Algorithm ภ%1 1.   !( ! ( && +*  10 ! 2. ( )ภ Algorithm  / 1.   ( +*  $ )ภ 2.  4, 3. && 4,ภ )ภ: 4* )  )ภภ! +* $ )ภ 4. 4*& &&,)  10 !+*ภ,* 2-3 +) 5. ( )ภ

12*& KAPONG CPE#11 Com-Club Homepage [http://student.sut.ac.th/comclub]


- 5 of 40 -

10/16/2008 11:01:00 PM

ภภ ภ (Part I) ภ

 Computer Programming * ภ!  Algorithm ( 2  *&ภ((ภ( 4*) +(ภ&'(ภ)&+!*45,

 ! )**&  ภ ( ,*1

90 (Output) )ภ ((ภ+*! (+ภภ.)ภภ! ( )4 !()+*,*+ &  ( +*! (*&ภ!  ( ,) )4!(!ภภ)ภ)+*,* ,) ,*42ภภ.ภ,) +* )ภ  ... 4)... ( *!,ภ! ภ ... &   !  +'(ภ)+*6  )ภ   &5!  Algorithm 2 ภ!  '&0,* ( +*! (*&ภ!  ( Algorithm 1 ,) + ! ,) ภ!  4*'&0)) Algorithm 1 )4!(5,+* ,*+/ Algorithm 2 *  & Algorithm ( *! )4,+*,*  23ภ" 1.  &ภ!ภภ*)))  2.  &ภ!ภ1 !)   ( 1 4 N ) N $!*) 3.  * !) , ( *!&$ Algorithm

"ภ&4)@ภ< ) 2!*)+* ภ&ภ ภ ภ&ภ ภ !)  2ภภ&   ภ 22!  ภ&ภ &,*( *! &! <,&ภ 2!'&4) ภ&! ภ&)  <,&,) 4*+ ,) )+ *&*&, ภ)  )ภ&*&( *! +* ภ&D * 35 ( *! * 35-36 ภ& *)ภ   ภ+* (BEภ 5 4*+  (BEภ,) ,* +*ภ,  * 35 - 36 +) 4*,*(BEภ . ( *!(BEภ)  2 " 4.  &ภ!ภ ภ(&ภ(&!  '&ภ!  N )ภ12*+* 4* N )4( ,*+ ภ&! +*( !  One-Digit  &!ภ 4*  ภ( ) ภ+*( $ Two-Digit ,Three-Digit )6( *!+*( !  Over-Three-Digit 5.  &ภ!ภ ภ(&ภ(&!  '&ภ!  N )ภ12*+* 4* N $  2 +*( !  â&#x20AC;&#x153;It is an even numberâ&#x20AC;? 4*$  +*)0!  â&#x20AC;&#x153;It is an odd numberâ&#x20AC;? ภ ((4)) 1. ) 9  )ภ +*+2  ภ* 4 * +*1  9  '&,) &ภภภ (..ภ 

!)  )

12*& KAPONG CPE#11 Com-Club Homepage [http://student.sut.ac.th/comclub]


- 6 of 40 -

10/16/2008 11:01:00 PM

ภภ ภ (Part I) ภ

 Computer Programming 2.  ! ))( + ABCDE +*,*)ภ 2

3. :;  ) )'&0&2 ! )&;&2 8 &; $&; ) 1 &;# )ภภ! &;

  !9+ภ &; )'&ภ *!& 2 (& 2  ! !  ภ,*  4*& ,)  ,) (+* + !ภ  ( 4* ( *!ภ. .!   (BEภ( '&0 $ภ+*) !) *  0+ภ(ภ*:; ภภ !) ))  ( )ภ4 & ภ)&  $& $ !*+ *.4 ภ!9ภ(ภ*:; '&0*!&ภ&ภ)&  $& ) ( (

)   

& ,) . )ภภ*+)ภ+!* 5,  4*(BEภภภ)0& ,) +  ))4))),*+!.,#0 )) 

12*& KAPONG CPE#11 Com-Club Homepage [http://student.sut.ac.th/comclub]


- 7 of 40 -

10/16/2008 11:01:00 PM

ภภ ภ (Part I) ภ

 Computer Programming

" %& 2 ภ) C (Introduction)

ภ &  !* ภ&!ภ < C ภ  @ภ< (!  *   !* ภ( ( *!ภ &

12*& KAPONG CPE#11 Com-Club Homepage [http://student.sut.ac.th/comclub]


- 8 of 40 -

10/16/2008 11:01:00 PM

ภภ ภ (Part I) ภ

 Computer Programming " $ภ!& C

<# $ < )!0 < + < )!0 & < (&) <#)ภ 42ภ+*$

<)*)+) $ <)     ภ )42  ,*.! )> : ภ0.2 ( )4  ภ( +ภ&,* &( (! , ( ภ.  ) <#4 ! $ <+ภ # !) / ภ</  < +2 ภ **!&ภ <#$ <'  * (Structured Language)  12*&)4( (&ภ'(ภ)$ !5 ,* '&(  !$ ภ ( ภ) : ) <G ภ#)ภ)& (  5 ( & +*  C &2 ภ.) C++, C# (ภ,!*1+  4*& &ภ &2 ) ...   ! â&#x20AC;&#x153; !â&#x20AC;? 2* () ภ& < )!0$* @&:&&2 2 &  &  (ภ  Syntax 5)&4 !9ภ& '(ภ) ภHภภ)&  5 ภ !  Algorithm (Logic)   ภ!ภ  ภ!ภ  '(ภ)    !* )&2+ !! &'(ภ),#ภ&   4*,) )( *! ... & ),) )4&'(ภ),* 4* & )ภ   (ภ  )0 ภ  2 ,*!&) Syntax )ภภภ ภ! ,,* ,,) ,* ภ* ภภภ  ! Algorithm ภ.)ภ  !  ภ)0&  ,+* ! ! (1& , ,)/ (  5( *!& )ภ !   Trick &2 ภ! ))ภ*&& + 4*&  ))ภภ.)4(ภ*:;'&ภ )+*ภ'&0 ,*)ภ($9)) ( &#&! & ,) ) & 855) - ภ 4, (&  &) #include <stdio.h>

int main(){ //This is one line comment /*This is multi-line comment Na-Ja */ printf(â&#x20AC;&#x153;Hello World\nâ&#x20AC;?); return 0; }

 !)! 1 ภ ),!*ภ@,  55   ภ'   .2 (,) * & #include) ภ&)  &  (#define)  ภ...**!& #  !.!  5  !&2 * main  !,!*ภ ภ@ * ภ+* main 2*ภ&2 ภ! & & & &  ,&,!*  main ()/!  & , ภ.* ภ@  !  main $ !) * '(ภ)!  Run '(ภ)  ภ ! ) Comment $ !,!* '& C ,) ! $  **- &$ภ   % ; 5%&$ภ

 )ภ1ภ  !,!*ภ& &  ภ@,!* * main

12*& KAPONG CPE#11 Com-Club Homepage [http://student.sut.ac.th/comclub]


- 9 of 40 -

10/16/2008 11:01:00 PM

ภภ ภ (Part I) ภ

 Computer Programming ( &)& ! ,)* ) * main ... ) 855 ... 4*,),* ,*1 & 855 )ภ) '(ภ) * *&  ,) *+ภ.,) $,  +&.5 6 !  % (6*5 ... 4 & < ภI<) 1. Source Code )&4  ! &*,(  855 ' *  2. Compile )&4 ภ! Source Code ( ( $,> 0)4 Run ,* 3. Compiler )&4 !+ * Compile (ภ! ( ภ.  )   Turbo C, Anjuta 4. Error )&4 *1  '(ภ)+*,) )4'(ภ),*&  42ภ* a. Syntax Error  ภ& Syntax 1   ))0 ;     ,) )4'(ภ),*  &4*,) (ภ*+*42ภ* Compiler $!ภ! 1 , (ภJ ) b. Logic Error  ภ&'(ภ)1 ภ !ภ   '(ภ),* (  ภภ/0,) B: ( 2*)'(ภ)* 855 c. User Error )&4 !'(ภ)  42ภ ( ! ภ+ *)2 1 855  ภ+*+ !  ภ,+ !ภ< ,)/ # * &ภ*!& 5. Warning )&4 *1  â&#x20AC;&#x153;â&#x20AC;? ภภ'(ภ) ( &5!  Compiler  &!  +*ภ Error ),* ( ภ  ภ &K ! ภ)( *!ภ(85 +*+( ภ A!&( *!) ) ภ57  ('(ภ)(ภ) ))!*( *!ภ    Hello World!!! (!!' ภ) ภ   & )ภ !  ภ*&  80 **!&'(ภ) (ภภ/0) #include <stdio.h> // '   %ภ,> 0 stdio.h (standard input output header file) int main(){ // )* main printf(â&#x20AC;&#x153;Hello World!!!â&#x20AC;?); // ( 1 ภ  !  Hello world return 0; //  ภ ('(ภ)) K&! &! ภ+!*  } //  main

1 ภ Hello World!!!

$, *  '(ภ)(ภภ  +ภ&'(ภ)+*& Pattern , ภ  $2()* &

12*& KAPONG CPE#11 Com-Club Homepage [http://student.sut.ac.th/comclub]


- 10 of 40 -

10/16/2008 11:01:00 PM

ภภ ภ (Part I) ภ

 Computer Programming

#include <stdio.h> int main(){ //    5 '(ภ) return 0; }

&ภ(printf) printf $  +*+ภ( 1 ภ * '&)%  $+*1

90ภ)) +,!* (  ,) ,* +ภ.$,*) )22(ภภ!  printf(â&#x20AC;&#x153;* !) ! )â&#x20AC;?[,!(   5]); [,!(   5] )&4 + ,) + ภ.,* + ภ  ภ.,* (()ภ &)

 - printf(â&#x20AC;&#x153;Testing textâ&#x20AC;?); ภ   : Testing text printf(â&#x20AC;&#x153;word1\nword2â&#x20AC;?); ภ   : word1 word2

+ภ ! )ภ( 1    )ภ@<+ * ! )&2 !* &  ( \n .)&4 ... )  &ภ!  )) ภ & ( *   )L& ,) 2*) ภ 6,  " ภ 6,  " \n \t Tab (!*! 8  ) +) \r \a ภ,& ( (ภ & Beep (  2)   \\ %s %c

\

(  ( !ภ<

\â&#x20AC;? %d %f

â&#x20AC;&#x153;

( !.) ( @&)

& )ภ &  ( ( ภ ( *!ภ &$ &,ภ!  (!  ) % * $ภ  ! ( +   * !) ! ))+ )2! &  ภภ! 

 - 4 Code printf(â&#x20AC;&#x153;My Name is %s\nâ&#x20AC;?,â&#x20AC;&#x153;Pongâ&#x20AC;?); My Name is Pong printf(â&#x20AC;&#x153;Age : %d\t GPAX : %f\nâ&#x20AC;?,20,3.94); Age : 20 GPAX : 3.94 printf(â&#x20AC;&#x153;\â&#x20AC;?%c\â&#x20AC;?â&#x20AC;?,â&#x20AC;&#x2122;Aâ&#x20AC;&#x2122;); â&#x20AC;&#x153;Aâ&#x20AC;?

* ภ 4*+* %s  ภ! +* â&#x20AC;&#x153;  ! %c +* â&#x20AC;&#x2DC; (K&!ภ& &+!*4,( *!ภ) ( 2*( ภ ,) && 12*& KAPONG CPE#11 Com-Club Homepage [http://student.sut.ac.th/comclub]


- 11 of 40 -

10/16/2008 11:01:00 PM

ภภ ภ (Part I) ภ

 Computer Programming  23ภ"ภ/" % (&   &! !*(   ... 2*) 1.  &'(ภ)( & & ! *&5ภ*  printf (  )

& ,) #+ภ+ J )4 ! )ภ( 1  printf *!&   ภ &  %d )4+ +2( %[-][0]d [-] )&4 + - ,) ภ.,* %s )4+ +2( %[-][0]s [0] )&4 + ! ,,) ภ.,* %f )4+ +2( %[-][0][.0]f [.0] )&4 + @&),) ภ.,*  ) ภ! ) ภ *   *&ภ  )2 !))& &( *!ภ   %-5d )&4 +*! )+ #*& (-) (    &  *&+ !  5   (5) %15s )&4 +*+  + *)! (,) ) -) (    &  *&+  15   (15) %4.2f )&4    &  *& 4   ( ( @&) 2 ( (.2) 4 Code printf(â&#x20AC;&#x153;My Name is %15s\nâ&#x20AC;?,â&#x20AC;&#x153;Pongâ&#x20AC;?); My Name is Pong printf(â&#x20AC;&#x153;Age : %5d|\t GPAX : %4.2f\nâ&#x20AC;?,20,3.94); Age : 20 | GPAX : 3.94 ภ!&  +* . *   ) !)*+ '&0 ภ  23ภ" 1.  &'(ภ)( 1

90$ 12345678901234567890123456789012345 NAME LAST NAME AGE SEX Somchai Jaidee 20 M Pong Pean 21 M Pattara Jintana 22 F 2.  ! '(ภ)( 1 &  , (& ภ ...  &)ภ ),) ,*,) #include<stdio.h> int main(){ printf(â&#x20AC;&#x153;%15s\tâ&#x20AC;?,â&#x20AC;?Test Line 1â&#x20AC;?); printf(â&#x20AC;&#x153;%-15s\nâ&#x20AC;? ,â&#x20AC;?Test Line 2â&#x20AC;?); printf(â&#x20AC;&#x153;%4sâ&#x20AC;? ,â&#x20AC;?Test Line 3â&#x20AC;?); printf(â&#x20AC;&#x153;\n%-4s\n\nâ&#x20AC;? ,â&#x20AC;?Test Line 4â&#x20AC;?); printf(â&#x20AC;&#x153;%|%20.4|sâ&#x20AC;? ,2.5); }

12*& KAPONG CPE#11 Com-Club Homepage [http://student.sut.ac.th/comclub]


- 12 of 40 -

10/16/2008 11:01:00 PM

ภภ ภ (Part I) ภ

 Computer Programming

" %& 3 !( ( !ภ (Variable and Operator)

ภ &  !*))ภ&'(ภ))ภ&  * ภ&   &( *!ภ!  * Focus &2 !( (  !ภ +*)ภ4*%+ ,)  +*,) *++!*4,,*

12*& KAPONG CPE#11 Com-Club Homepage [http://student.sut.ac.th/comclub]


- 13 of 40 -

10/16/2008 11:01:00 PM

ภภ ภ (Part I) ภ

 Computer Programming

  (Variable) !( , (  4)&8 &) !(   *  +*ภ.*)2 )*)2  &2(   !ภ< * !) !.) !@&) (  ภ ( + <# *)2 42ภภ.+2 !   '&) ภ( $ 2 ภ</  ()  )& (Signed) (  (,) )  )& (Unsigned) ภภ</  ( $ !.) ภ @&) (( 5 5  & ) ( +)*&ภ+*) ()ภภ!  !( ( ภ$ 3    1. !.) ,*(ภint (  long (& ภ .ภ,+; ) 2. @&) ,*(ภfloat ( double 3. !ภ< ,*(ภchar (  String (char[]) ภ9&  !((  * )2!9ภ@!( ( ภ !(ภภ ภภ6    + *+ภภ@!(   !( !( (= )*) [,!( (= )*)];  !()&2 * ( *!  ! )*+ ภ.,*,) + ภ.,*   ภ+ &)2!(ภภภ!  ภ  !( +*) * $ +*+ภภ &  !( * )ภHภภ)& (& ) +ภ  *!&   1. !  +* !))& ((&  (  &) 2. ภ<* * $ a 4 z  A 4 Z   )&*+* ( _ ) ( <<< * ) 3. ภ< , $!ภ< !    )&*+* ภ.,* ( "%)  !  &+ 4. )&  ;& 32 !(ภ  ( &5  ,) !ภ 31 !  ... + , 4 ) 5. !ภ<+*$ Case-sensitive  ! a   ภ A 6. *** ! ภ & +* _ !( !  +* _ ภภภ,*'&,) 2*! 7. "%  ภ  ! (Reserved Keywords) # )   ,  !+ <# asm auto break case cdecl char const continue default do

double else enum extern far float for goto huge if

int interrupt long near pascal register return short signed sizeof

static struct switch typedef union unsigned volatile void while

_cs _ds _es _ss _AH _AL _AX _BH _BL _BX

_BP _CH _CL _CX _DH _DL _DX _DI _SI _SP

12*& KAPONG CPE#11 Com-Club Homepage [http://student.sut.ac.th/comclub]


- 14 of 40 -

10/16/2008 11:01:00 PM

ภภ ภ (Part I) ภ

 Computer Programming

 -ภ%   /ภ !&   89ภ%

:  int a,b,sum=0; int sizeof; //  ! float gpa; float 2g; //**!&!  char grade=â&#x20AC;&#x2122;Aâ&#x20AC;&#x2122;; char name[50]; char this grade=â&#x20AC;&#x2122;Aâ&#x20AC;&#x2122;; //)!*!   %9/! ; ( !2*ภ+) 1

1. int  *)2 +*ภ.!.) +* !& !)+ภภ. 2 ,0 )4ภ.!.),*

 ( -32,768 4 32,768  2  ) unsigned int  *&ภ ( ภ.!.)  ( 0 4 65,535 (4*) signed ภ.!.)!ภ)$ 2   ( ภ.! ,) ,*) 2. char  *)2 + *ภ.!ภ< 1 !   â&#x20AC;&#x2DC;Aâ&#x20AC;&#x2122;,â&#x20AC;&#x2122;câ&#x20AC;&#x2122;,â&#x20AC;&#x2122;5â&#x20AC;&#x2122; ( ภ! +* â&#x20AC;&#x2DC; $!ภ! $ char) ( + 2

Compiler ,) ) $!ภ< ( ภ.$! ( '&+*)% !  ASCII  !ภ<  5 3.

4. 5. 6.

7.

+*+ ภภ. 1 ,0 ( -127 4 127) char[] (String).-  9#-  ! ; 4,7 ( $* $*)2 % !2* +* ภ.* !)   â&#x20AC;&#x153;Pongâ&#x20AC;? , â&#x20AC;&#x153;Hello Worldâ&#x20AC;? ,)/  ภ! +* â&#x20AC;&#x153; $!ภ! $ String K&! &2* !))& 5+!* 6 ( *!ภ  float  *)2  ภ.! @&) +* !& !) 4 ,0 ภ. ,* ( 3.4E-38 4 3.4E+38 (ภ.@&)42ภ* )/ 8  ภ) double  *)2  *& float ( +*$ 2    float ภ.*)2 ,*  ( 1.7E-308 4 1.7E+308 ภ.@&)42ภ* )/ 15  ภlong int  int &! (ภ!) +*ภ.*)2 )ภภ!  int 2     4 ,0 ภ.*)2 ,* ( -2,014,153,648 4 2,014,153,648 (!0)& ) ( *!  2!  2 ภ)  unsigned long int ภ. ,* ( 0 4 4,294,486,648 )/ & long double  double &! +* 10 ,0 ภ.*)2 ,* ( 3.4E-4032 4 1.1E+4032 ภ. @&),*4ภ2 * )/ 18  ภ(   ,)  $ 5 (! & ,() &)

ภภ" -#"%ภ   !( = * ภภ;   x=5; )& !)!    5 ,ภ.+!( x *5 &ภ! +* x )  $ 5

1 2

ŕ¸

Compiler ภ   Turbo C American Standard for Information Interchange

12*& KAPONG CPE#11 Com-Club Homepage [http://student.sut.ac.th/comclub]


- 15 of 40 -

10/16/2008 11:01:00 PM

ภภ ภ (Part I) ภ

 Computer Programming

 - #include <stdio.h> int main(){ char name[20]= â&#x20AC;&#x153;Pongâ&#x20AC;?; int age=20; float gpa; gpa = 3.94; printf(â&#x20AC;&#x153;My Name is %15s\nâ&#x20AC;?,name); printf(â&#x20AC;&#x153;Age : %5d|\t GPAX : %4.2f\nâ&#x20AC;?,age,gpa); return 0; }

1 ภ My Name is Age : 20 |

Pong GPAX : 3.94

-& (Constant Variable) ภ* ภ*  +5 ,) )ภ & !*   !9ภ*  &2 + !  ! )! 1 ภ ( !&!ภ #include) +!*(ภ5  ภ*  * +*   #define )2(   #define !(     * ภภ.  ภ ,) ) ;  #include <stdio.h> #define PI 3.1415926 int main(){ printf(â&#x20AC;&#x153;PI is %f\nâ&#x20AC;?,PI); return 0; }

1 ภ PI is 3.141593

& %9/ (scanf , gets) scanf   + *+ภ*)2 ภ&00 '& *)&2 ภ* !) ! ) gets   + ** !)ภ&00 (* !)&  &!) '&ภ.+!(,!* scanf(â&#x20AC;&#x153;* !) ! )â&#x20AC;?[,!(]); gets(!( char[] ); * !) ! ) +* %s,%f,%d $ ภ( $)ภ * ,) ภ &) !() 4*$ &!ภ char[] .- % & %" % 8%.-#-/% #"%#- & " %%  ( &  ,ภ)

12*& KAPONG CPE#11 Com-Club Homepage [http://student.sut.ac.th/comclub]


- 16 of 40 -

10/16/2008 11:01:00 PM

ภภ ภ (Part I) ภ

 Computer Programming

 -#include <stdio.h> int main(){ char name[20],school[30]; int age; printf(â&#x20AC;&#x153;Please Enter Your Name : â&#x20AC;?); scanf(â&#x20AC;&#x153;%sâ&#x20AC;?,name); printf(â&#x20AC;&#x153;Please Enter Your School : â&#x20AC;?); gets(school); printf(â&#x20AC;&#x153;Please Enter Your Age : â&#x20AC;?); scanf(â&#x20AC;&#x153;%dâ&#x20AC;?,&age); printf(â&#x20AC;&#x153;Name : %s\nâ&#x20AC;?,name); printf(â&#x20AC;&#x153;School : %s\nâ&#x20AC;?,school); printf(â&#x20AC;&#x153;Age : %dâ&#x20AC;?,age); return 0; }

1 ภ Please Enter Your Name : Pong Please Enter Your School : SUT University Please Enter Your Age : 20 Name : Pong School : SUT University Age : 20

 +  '&+*)ภ!*! *!&+2  ( *! ภ1

90 ! ภ, +*     ,) ภ1

ภ ( )!9(ภ*  4** ภ+* scanf  )ภ gets #"%#%%   $< â&#x20AC;&#x153;%[^\n]â&#x20AC;?  â&#x20AC;&#x153;%sâ&#x20AC;? ภ&  @<+ +  scanf    4** ภ+*6!ภ<+ +*+ ,+ [ ]   4** ภ 6ภ< B,0,1,2,3,4,5,6,7,8,9 ภ.+** !) ! )!  â&#x20AC;&#x153;%[B0123456789]â&#x20AC;? + ภ</ *)4*,) * ภ+* +* ^ *   4*,) * ภ+*)! a +* !) * +** !) ! )$ â&#x20AC;&#x153;%[^a]â&#x20AC;? )/  ( *!4* scanf ภ< .- %ภ )ภ&  2  

)ภ, !  ! *   ภภ@  name[20] )&4 )4ภ.* !),* 19 !ภ<   4** ภ+*ภ.,* 20 ! ภ.* + $ 21 (  (** ภ... & ...,) ,*ภ,)  23ภ" 1.  &'(ภ) ภ12*+* '&$!.) !@&) (  &   1 ! ( *!ภ

( 1

90ภ * *) ภ! $!(, 2.  &'(ภ)  !(!.) 2 ! ( *!( 1 ภ  !  !(( *!( 1 ภ (& 'ภ) 12*& KAPONG CPE#11 Com-Club Homepage [http://student.sut.ac.th/comclub]


- 17 of 40 -

10/16/2008 11:01:00 PM

ภภ ภ (Part I) ภ

 Computer Programming

  ภ ),*! ()( *! )( *!( 1  & )ภ.ภ,&2  ) * )!ภภ* '&! ภ 1)&ภ( $ 5 (  !ภภ& !ภภภ ( )  !ภ /@0 ! ภ ภ !ภ  !ภภ () ,

  ภ= 6  ( !  &&) !ภ /@0 )  Operator +

ภ+*  ภ!ŕ¸

!&  a=a+b;

a= +3; (+3 ,*ภ.  3)

c=3+5; a=a-b; c=3-5; a=2*3;

b=a+5; a= -3; b=a-5; c=a*b;

-

ภ 

*

ภ 2/ ภ a=a/2; ภ@< a=3%2;

/ %

b=5/3; (1

90$ 1) b=a%4;

  .ภ*&   9&&   &! + <# int ภ int ,* int   5 $ int ,3 ภ.$ int ภภภ 1

90) !)  1.6666â&#x20AC;Ś ( Compiler *  & +*$ int $,) ภH   Compiler  ,) +@<@&)ภ+*,*  $ 1 ( (* .1)  )4 +* ภ & !(,* &ภ!  Type cast ,*'&ภ) ( * ภ  &$) *!( * ภ   printf(â&#x20AC;&#x153;%.2fâ&#x20AC;?,4/(float)5); 1

90ภ)$ 0.80 ภ & +* 5 $ float ภภ

  ภ# ภภ" -/ &- Operator !&  =

n=8;

+= -= *= /= %= ++

n+=8 n-=8 n*=8 n/=8 n%=8 ++n n++ --n n--

--

!))&   8 ,+ +!( n n = n+8; n = n-8; n = n*8; n = n/8; n = n%8; n=n+1; n=n-1;

12*& KAPONG CPE#11 Com-Club Homepage [http://student.sut.ac.th/comclub]


- 18 of 40 -

10/16/2008 11:01:00 PM

ภภ ภ (Part I) ภ

 Computer Programming ))  )& ++ ภ --   *  ; &&!   2   ( ( *!*+ )ภ&  

num1=8; num2=++num1 -3;

num1=8; num2=num1++ - 3;

,*

num1=9 , num2=6

num1=9,num2=5;

!))& ++ , -- 4*+* & )&4 !  4* ++num )&4 )  num ภ1 ( *! &  num )+* ( 4* num++ )&4   num )+*ภ( *! &)  num ภ  2+' **   (-- )ภ ,) &ภ&#)  &5 ++ ภ-  &-ภ- ++ "/  &-"/ ( ภ! ))

  ภ  ภ ภ.ภ!  ภ   * +* ภ@0 +ภ !/( 55 ภ* ภ&  ! + <# ) !  0  -0 (  +5ภ.) .-#- 0  -  (( + !)&)+* 1 ( ( 1 ,*ภ  )&ภ. ,* 1 )  ภ) ' )2ภ  )&ภ & Operator == > < >= <= != && || !

!& 

!))&  ภ a==b )ภภ!  a>b *&ภ!  a<b )ภภ!  ภ a>=b *&ภ!  ภ a<=b ,)  ภ a!=b (a>b)&&(a<c) (  (a>b)||(a<c)  9 (not) !(a<b)

 -- 4*ภ+* a=5,b=7,c=10 

,*

a>b a<(c-b) (a+b)>c (a*+7)%2==c%2 c!=4 (a>b)&&(c<b)

0 0 1 1 1 0

.!  ,*ภ  )&,*  ภ)$ 0  1   12*& KAPONG CPE#11 Com-Club Homepage [http://student.sut.ac.th/comclub]


- 19 of 40 -

10/16/2008 11:01:00 PM

ภภ ภ (Part I) ภ

 Computer Programming   ภ %!ภ  ภ!ภ( ,,ภ +* ภ,* )2   ;  )& &ภ!    /  )!&" * +* (#&)   )&

!) ; ภ2  #*&,! ()  .7% ! ++ -- (type cast) | #*&,! * / % | #*&,! + | #*&,! < <= > >= | #*&,! == !== | #*&,! && | #*&,! || V   .7% = += -= *= /= %=  & ,) )49&ภ    )&,* )  2!  , ( !()1    ,)  $ , *   ภ( 1 )ภ * . ! ,!* 42ภ*   (...+* (4*9& & ภ! ) ( *! &  (2ภ!  ( *!$ < ! & ภ! ) $ - x=3,y=4,z=7,a=1,b=2 '&0 x=++y -3%2 a+=b%2-5*3

 x=

y=

'&0 x=y++ - 3%2 a+=b%(2-5)*3

x+= 3%2 + y++

x+= 3%2 + ++y

x= y++ + ++z

x= ++y + z++

x=x++

x=++x

x=(a=b++)+2*b

x=(a=++b)+2*b

 x=

y=

1)!   . 1),) )49&,*    !!*+* +* ,* ภ ( !&ภ)ภ) $- &.%

12*& KAPONG CPE#11 Com-Club Homepage [http://student.sut.ac.th/comclub]


- 20 of 40 -

10/16/2008 11:01:00 PM

ภภ ภ (Part I) ภ

 Computer Programming  23ภ" 1.  &'(ภ) !.) 3 !ภ12+* * ( *!ภ 6 & 2.  &'(ภ)( ภ !&#) $ >( ! ภ+* (2.54 cm = 1â&#x20AC;? ,12â&#x20AC;? = 1 >) 3.  &'(ภ)( ภ @>,80 $  @# #& 4.  &'(ภ) ! )ภ12*+* 2 !  ( time1,time2 ) '&ภ.+!.) '& time1 ! 

  1330 )&4 13:30  ! time2 !! ') ภ   340 )&4 3 !') ภ 40    ภภ!ภ time2 *ภ time1 ( *!( 1

90 !&  1 ภ'(ภ) Please enter time1 : 1330 Please enter time2 : 340 Time 13.30 + 3.40 = 17.10 5.  &'(ภ) !!ภ12*+* ( *!ภ( +*$!')  ( ! ) 

  7450 ! ภ.$ 2 !') 4  ภภ10 ! 6.  !/ ! ภ)+; + &)11* ) *ภ!* ( *&!)ภ12*+* 7.  !/*&) ภ! ภ)ภภ & )11* ) *ภ!* ( *&!ภ12*+* 8.    &)+; &2 +     ! ภ)(1/4  ! ภ)( ภ+* ) 9.    &)11*+ ; &2 +  ! ภ) 10.   *) 10 ( *!ภ!   2 ( !    11.  ! num )ภ12+* * ( num  ( 0 4 9999) ( *!ภ  1 !ภ! (   ภ num   2345 ภ.$ 2+3+4+5 = 14 $* 12.  ! num )ภ12+* * ( num  ( 0 4 9999) ( *!ภ  1 !ภ!  ภ num 6  2   2345 ภ.$ 2+4 = 6 $* $" % (+* ... K&!,) 4ภ2 + -_-!!! )

12*& KAPONG CPE#11 Com-Club Homepage [http://student.sut.ac.th/comclub]


- 21 of 40 -

10/16/2008 11:01:00 PM

ภภ ภ (Part I) ภ

 Computer Programming

" %& 4 !+ ( ภ! 2

ภ &  !*))ภ&'(ภ))ภ& ภ(ภ) ( * focus ภภ! )() !*4! $ !*$!+ ภ&'(ภ) &&! ( !), ,) ,*)ภ) -  Syntax - % %  -. % " ภ !& Algorithm ภภ- ,) &! )ภ &ภ! 

12*& KAPONG CPE#11 Com-Club Homepage [http://student.sut.ac.th/comclub]


- 22 of 40 -

10/16/2008 11:01:00 PM

ภภ ภ (Part I) ภ

 Computer Programming Ifâ&#x20AC;Śelseâ&#x20AC;Ś Ifâ&#x20AC;Śelseâ&#x20AC;Ś $  + *+ภ+ )* + (855) &ภ!&    8%(if) )! ( ,) * ภ

*!)ภ(  ) ) "ภ.-(else) ภ.ภ)ภ(  ).) (((#! ) .,*!  ))* !)5  # ) 'ภ$  $. ,*)'(ภ) ( ,) ( *!) ภ+* ภ)ภภ!  1  )!&  +* if else )ภ 2(  )*!&ภ 3 (  if ( ,){

if ( ,)

 s;

 ;

}

else

if ( ,) {

 ; if ( ,) {  ;

 ; â&#x20AC;Ś } else {

 ; â&#x20AC;Ś

â&#x20AC;Ś } else if ( , 2) { }

} â&#x20AC;Ś  ,4, 4*) } else {

 ; â&#x20AC;Ś }

( 1 ())#*&)  )  ภ2  4* ภ.  ( 4*.ภ.,)  , ( 2 ())  #*&)  )  ภ2  4*    4*.   ( 3 ())  !)  4*))ภภ!  2  , ภ. else if ,&5    ! else *& ,) *ภ/+5  & !& .+  !+; $ 0 ภ,*!&  )&(ภ ( *!+*1

90 $  .   ภภ$  &! ,) $* + { } (&ภ+*.+ ())!) ( 4* )   )ภภ!  2  * + { }  %   ภ&'(ภ) !)ภ!**,+*ภ! + &2 +! ,  ภภ!&  !   )ภ!* *,* +ŕ¸

12*& KAPONG CPE#11 Com-Club Homepage [http://student.sut.ac.th/comclub]


- 23 of 40 -

10/16/2008 11:01:00 PM

ภภ ภ (Part I) ภ

 Computer Programming 9

 -ภ ภภ   #include <stdio.h> int main(){ int x; printf(â&#x20AC;&#x153;Please enter even number :â&#x20AC;?); scanf(â&#x20AC;&#x153;%dâ&#x20AC;?,&x); if (x%2 == 0) printf(â&#x20AC;&#x153;It\â&#x20AC;&#x2122;s even number\nâ&#x20AC;?); else { printf(â&#x20AC;&#x153;%d is â&#x20AC;?,x); printf(â&#x20AC;&#x153;Odd Number\nâ&#x20AC;?); } }

1 ภ Please enter even number : 20 Itâ&#x20AC;&#x2122;s even number

1 ภ Please enter even number : 11 11 is Odd Number

 23ภ" if-else 1.  &'(ภ) !.) n ภ12*+* ภ12+* *ภภ! )ภภ!  30 ( $  2 +*)0! â&#x20AC;&#x153;Youâ&#x20AC;&#x2122;re rightâ&#x20AC;? ภ,) + +*)0 !  â&#x20AC;&#x153;Youâ&#x20AC;&#x2122;re wrongâ&#x20AC;? 2.  &'(ภ) !.) score ภ12*+* ( *!ภภ '&)ภ/M0   80 , A 70-79 60-69 50-59

B C D F

ภ 3.  &'(ภ)4)(  @ภ>,80$ # #& ภ# #&$>,80 '&ภ ภ< 1 !)ภ12+* * ภ$ C +*   @# #& ( $ >,80 ภ$ F +*   @ >,80 ( $ # #& ภ,) +* +*)0 !  â&#x20AC;&#x153;Youâ&#x20AC;&#x2122;re wrongâ&#x20AC;? 4.  &'(ภ)!.)) 10 ! ( *!ภ 2  *) 6 & $ &&.%ภIf-else

12*& KAPONG CPE#11 Com-Club Homepage [http://student.sut.ac.th/comclub]


- 24 of 40 -

10/16/2008 11:01:00 PM

ภภ ภ (Part I) ภ

 Computer Programming for loop

!  loop  )ภ!#   for loop  $ภ!#( for ภ!#( !+; +*ภภ!#)  ภ!(  (,*) ( ภ!#() ภ)! index +*+ภภ! ,4   +( *! index &2 + !)&))ภ+* i , j , k , l (  !)&))  )22( for loop ภ &ภ!  for (  ) *; ,;  ) for (  ) *; ,;  ){

 ;

 s; }

&ภภ!  { } +* ภภ&!ภ if-else  4*)  &+* for &  &! ,) $* + { } ( 4*))ภภ!  1  * + *!&  ภภ  for ) &$ Algorithm ภ.,*( 1. )  )* 2. ! , 4*! $.ภ.ภภfor ภ$ +*

ภ  3.    &+ for 4.     5. ภ,+* 2 9

 -ภ /ภ- #include <stdio.h> int main(){ int num,i; printf(â&#x20AC;&#x153;Please enter a number :â&#x20AC;?); scanf(â&#x20AC;&#x153;%dâ&#x20AC;?,&num); for (i=0;i<num;i++) printf(â&#x20AC;&#x153;%d â&#x20AC;?,i); } #include <stdio.h> int main(){ int num,i; printf(â&#x20AC;&#x153;Please enter a number :â&#x20AC;?); scanf(â&#x20AC;&#x153;%dâ&#x20AC;?,&num); for (i=1;i<=num;i++) printf(â&#x20AC;&#x153;%d â&#x20AC;?,i); }

1 ภ Please enter a number : 10 0123456789

1 ภ Please enter a number : 10 1 2 3 4 5 6 7 8 9 10

12*& KAPONG CPE#11 Com-Club Homepage [http://student.sut.ac.th/comclub]


- 25 of 40 -

10/16/2008 11:01:00 PM

ภภ ภ (Part I) ภ

 Computer Programming  23ภ" 1.  &'(ภ) ! !.)!ภภ12*+* ( *!ภ)0 ภ!  4 1 ( ) 2.  &'(ภ) ! !.)!ภภ12*+* ภ12*+* +    +*)0  ( 1 4 ! '&)0 3. 4. 5. 6. 7. 8. 9.

6   ภ  2 +*)0 ( 0 4 !  '&)06  2  &'(ภ) *&ภ* 2 ( +* (  &'(ภ) ! !.)!ภ( *!ภ)0() 2 2/  !  &'(ภ)1 !)   ( 1 4 N ) N $!*)  &'(ภ)!.)) 10 ! ( *!ภ 2  *) 6 &  &'(ภ))0 ภA 4 Z  &'(ภ))0 ภZ 4 A  &'(ภ) !ภ< 1 !ภ12+* *( *!ภ)0ภA 4 !ภ< ( *!)0ภ,& A ภ Please enter a character : L ABCDEFGHIJKLKJIHGFEDCBA

10.  &'(ภ)  ! !.) ( *!ภ)0  &)) !)ภ!*  ภ!.) Please enter a number : 4 **** **** **** **** 11.  &'(ภ) *&5ภ * 10 ( $2 &)ภ! Please enter a number : 4 **** * * * * **** 12.  &'(ภ) *&ภ* 11 ( $ &)ภ!  !ภ< Please enter a number : 4 ABCD E F G H I J K L

12*& KAPONG CPE#11 Com-Club Homepage [http://student.sut.ac.th/comclub]


- 26 of 40 -

10/16/2008 11:01:00 PM

ภภ ภ (Part I) ภ

 Computer Programming 13.  +* !)2*,*ภ* 10,11,12 ( *! * 2 Character Diamon Please enter a number : 4 A B C D E F G H I J K L 14.  +* !)2*ภ* 13 ( *!* 2) &)ภ! Please enter a number : 4 A BC D E FGHI 15.  &'(ภ)* ) &)ภ!

$ &&.%ภfor loop

12*& KAPONG CPE#11 Com-Club Homepage [http://student.sut.ac.th/comclub]


- 27 of 40 -

10/16/2008 11:01:00 PM

ภภ ภ (Part I) ภ

 Computer Programming while loop & do-while loop while loop &)+*ภ)ภภ loop '(ภ))0 (  &) !  2 ) !,) (  ( 

&5 )/) !2() ภ</  &ภ!  for loop  .ภ*& ( !  ) while loop ) 2 2&2 !   dowhile loop # ) ภ</ )ภ while loop (   ภ &! &K !  2 2(( ภ ภ do{ while ( ,){  s;  s; }while ( ,); } ภ  while ภ  do-while 1. ! ,! $ ,) ภ!  1.    &+ do-while loop . ภ. loop while ภ +*ภ 2. ! ,! $ ,) ภ , ! . ภ. loop ภ +*ภ, 2.    &+ while loop * 1 3. ภ,* 1 * ภ while ภ do-while   ภ ! ภ ,$. ( (ภwhile ,)    &+ 2 & ( do-while ,*ภ  1  ( ภ !  do-while ,*ภ  &+ loop &  *& 1  ) ( while   ภ. )  ,& $ &2 9

 -ภ / #include <stdio.h> int main(){ int num,i; printf(â&#x20AC;&#x153;Please enter a number :â&#x20AC;?); scanf(â&#x20AC;&#x153;%dâ&#x20AC;?,&num); while(num>0){ printf(â&#x20AC;&#x153;%d â&#x20AC;?,num); num--; } } #include <stdio.h> int main(){ int num,i; printf(â&#x20AC;&#x153;Please enter a number :â&#x20AC;?); scanf(â&#x20AC;&#x153;%dâ&#x20AC;?,&num); do { printf(â&#x20AC;&#x153;%d â&#x20AC;?,num); num--; } while(num>0); }

1 ภ Please enter a number : 10 10 9 8 7 6 5 4 3 2 1

Please enter a number : 0

1 ภ Please enter a number : 10 10 9 8 7 6 5 4 3 2 1

Please enter a number : 0 0

12*& KAPONG CPE#11 Com-Club Homepage [http://student.sut.ac.th/comclub]


- 28 of 40 -

10/16/2008 11:01:00 PM

ภภ ภ (Part I) ภ

 Computer Programming  23ภ" 1.   ภ  '(ภ)ภ(BEภ for loop ( +* &,+* while loop ( 2.  &'(ภ)   ( 5 â&#x20AC;&#x201C; 20 ภ12*+* ภ12*+*ภภ1+* !  â&#x20AC;&#x153;Wrong numberâ&#x20AC;? ( *!ภ  +) ภ! 12*+* 42ภ* 3.  &'(ภ)  10 ภ12*+* '& ภ.* &2 !  0 â&#x20AC;&#x201C; 10 ( *!ภ 2  4. &'(ภ)*)2  5 ภ*!& @ (â&#x20AC;&#x2DC;Fâ&#x20AC;&#x2122;,â&#x20AC;&#x2122;Mâ&#x20AC;&#x2122;) & (int 20-50) ( ภ(weight) 4* $12*&& 30-50 ภ50 â&#x20AC;&#x201C; 60 12*; & 20-30 ภ45 â&#x20AC;&#x201C; 55 ( *!)0  !  â&#x20AC;&#x153;GREATâ&#x20AC;? 4* ,) +*)0  !  â&#x20AC;&#x153;HOHOHOâ&#x20AC;? 5.  !  2 !ภ12*+* a ,b '& 0<a<10 (  20<b<40 4*,) &2 + !  ภ!+*)0!  â&#x20AC;&#x153;ERRORâ&#x20AC;? ( !+) ),* a,b ( *!+*ภ)0   2   ( a 4 b 8...Break, Continue  break; (   continue; $   +*+ภ ! ) loop '& break; ,!*ภภภ2  switch (...ภ  &) '&ภ+ ( continue; $   +*,*6 2 )  +  2 '(ภ),)   +5  ( ภ,! ,+) ( &5!  *&ภ break; ( ,)+) ( break; ภ, & ()& 4*$ for loop ภ,   ภ( *! ,!  ,) $-  while, do-while, break /continue;

12*& KAPONG CPE#11 Com-Club Homepage [http://student.sut.ac.th/comclub]


- 29 of 40 -

10/16/2008 11:01:00 PM

ภภ ภ (Part I) ภ

 Computer Programming Switchâ&#x20AC;Ścase switchâ&#x20AC;Ścase $  ) ภ </ *&ภ ifâ&#x20AC;Śelse  & , )ภภ ifâ&#x20AC;Śelse )ภ &

 ,6     .ภ5  )22( switch(!($ ,){ !&    case  1 :  s; switch(x){ case 1 : printf(â&#x20AC;&#x153;X is Oneâ&#x20AC;?); break; case 2 : printf(â&#x20AC;&#x153;X is Twoâ&#x20AC;?); break; case 3 : printf(â&#x20AC;&#x153;X is Threeâ&#x20AC;?); break; default : printf(â&#x20AC;&#x153;X is otherâ&#x20AC;?);

break; case 2 :   s; break; â&#x20AC;Ś. case  n :  s; break; default :  s;

}

}

ภ  !&  + *ภ. !  4* x =1 ภ.)0 !  â&#x20AC;&#x153;X is Oneâ&#x20AC;? 4* x=2 ภ.)0 !  â&#x20AC;&#x153;X is Twoâ&#x20AC;? 4* x=3 ภ.)0  !  â&#x20AC;&#x153;X is Threeâ&#x20AC;? ( 4*,) +*ภ.)0 !  â&#x20AC;&#x153;X is otherâ&#x20AC;? default ภ/,) +  ,!*+ case ภ* ( *!   &+ default )ภ ifâ&#x20AC;Śelse  &5  ภ  default ภ.)ภ else !4* 4*,) *ภ/, &ภ.   ,)/ $& % $@< (,) ,*ภ,) ภ.   ภ! )ภ+ break; ภ5 case )0 & +*   2ภ4*,) + break; ภ,.... 1

90  ภ(ภ, )4,) ) ภ(  )ภ.&,) ,* ภ. )ภ   !&    switch(x){ case 1 : printf(â&#x20AC;&#x153;X is Oneâ&#x20AC;?); case 2 : printf(â&#x20AC;&#x153;X is Twoâ&#x20AC;?); case 3 : printf(â&#x20AC;&#x153;X is Threeâ&#x20AC;?); default : printf(â&#x20AC;&#x153;X is otherâ&#x20AC;?); } 4* x=1 ภ.)0 â&#x20AC;&#x153;X is OneX is TwoX is ThreeX is otherâ&#x20AC;? 4* x=2 ภ.)0 â&#x20AC;&#x153;X is TwoX is ThreeX is otherâ&#x20AC;? 855 .4 ภ break (ภ& )  )&5 switch  &   $ภcase  % break; < & "/$$ภ

12*& KAPONG CPE#11 Com-Club Homepage [http://student.sut.ac.th/comclub]


- 30 of 40 -

10/16/2008 11:01:00 PM

ภภ ภ (Part I) ภ

 Computer Programming

" %& 5 >: ภ0 Function

ภ &  !*ภ*++*)4&'(ภ),*  !* & ภ! &ภ( 1)ภ) ! ) & '&*  + ภ!ภ &+ ,parameter (  return value  >: ภ0 ( ภ .,*( *!

12*& KAPONG CPE#11 Com-Club Homepage [http://student.sut.ac.th/comclub]


- 31 of 40 -

10/16/2008 11:01:00 PM

ภภ ภ (Part I) ภ

 Computer Programming >(ภ 4)*  >: ภ0 , ... ) ภภ!ภ  !)&(&ภภภ'(ภ) $ภ ภภ!ภ& & (Module) ... (& &2 + J) '&ภ!ภ& &) ภ,4 !*(ภ    ภ*) ))  ภ!ภ&2  main ภ.+( !  *)+* ,* )* ,ภ( ภ+ 4!* & ( function ) 4 !  ภ!ภ*)+*& , (  ( *! & function ,* )+* main +* ภ.$   *) &+ main main $>: ภ0 + <# # 4! $>: ภ0(ภ! Compiler  *!& & ( *!( ! main  ,&ภ>: ภ0,)  ภ.( *!( (* 2*)   ! ),ภ) !( *!)2!9ภ) >: ภ0ภ &ภ!  Parameter

Function

!( +*>: ภ0

Return Value

 ภ)& &ภ>: ภ0

>: ภ0+! Parameter  *,+*ภ Function & ภ!ภ &+ Function (    ภ)+*ภ  &ภ) (Return Value)  )22(ภภ@( ภ&ภ+*ภภภ!  ภภ@    ภ >: ภ0(!(  +*>: ภ0){  s; return  ภ; //+ภ/)ภ ภ }

ภภ@>: ภ0*( (Prototype)    ภ >: ภ0(!(  +*>: ภ0); ภ&ภ+*ภ/)ภ  ภ !(+* = >: ภ0(  +*>: ภ0),*ภ@); ภ&ภ+*ภ/,) )ภ  ภ >: ภ0(  +*>: ภ0),*ภ@);   -&-ภ/ )ภ !( & ( ))    void  ภภ! ,)  ภ (4*$ void ,) * )    return)  & -#"% ภ@)ภภ!( & ( ! (  !(*!&  )& , (comma) '&) ภ@.,) ) ; D*& !&>(ภ +* ภภ&!ภ!( & ( ! +ภ+*>: ภ0# ภ>: ภ0)&2 ( *!  &+ )&2 + main .! )ภภ@>: ภ0*( +*+ภ/&ภ& & >: ภ0,!*  main ( ภ &   main ,) 2*ภ( *!+* error ,*  * )ภภ@>: ภ0*(,!* !* main  ,!* &+ main ภ.,* (+ภ/,!* &+ main, main  )4+*> : ภ0,*) ภภ@*(&ภ!  Prototype # )ภ(ภ ภภ@>: ภ0(  &ภ{ $ ; D*& & 12*& KAPONG CPE#11 Com-Club Homepage [http://student.sut.ac.th/comclub]


- 32 of 40 -

10/16/2008 11:01:00 PM

ภภ ภ (Part I) ภ

 Computer Programming >: ภ0)ภ &ภ+*> : ภ0 * &,!*  >: ภ0! &ภ+*) ,)  error (2ภ/ main $ !&  )   )2! &  ภ&'(ภ)ภ &ภ!  ,) +*>: ภ0 (Before) +*>: ภ0 (After) #include<stdio.h>

#include<stdio.h>

int main(){ int a,b; do{

int getnumber(char strwel[],char strerr[]){ int a; do{ printf(strwel); scanf(â&#x20AC;&#x153;%dâ&#x20AC;?,&a); if(a>=0&&a<=50) break; else printf(strerr); }while(1); return a; }

printf(â&#x20AC;&#x153;Please enter a :â&#x20AC;?); scanf(â&#x20AC;&#x153;%dâ&#x20AC;?,&a); if(a>=0&&a<=50) break; else printf(â&#x20AC;&#x153;Error Naâ&#x20AC;?); }while(1); do{ printf(â&#x20AC;&#x153;Please enter b :â&#x20AC;?); scanf(â&#x20AC;&#x153;%dâ&#x20AC;?,&b); if(b>=0&&b<=50) int main(){ break; int a,b; else a=getnumber(â&#x20AC;&#x153;Please enter a:â&#x20AC;?, â&#x20AC;&#x153;Error Naâ&#x20AC;?); printf(â&#x20AC;&#x153;Error Najaâ&#x20AC;?); b=getnumber(â&#x20AC;&#x153;Please enter b:â&#x20AC;?, â&#x20AC;&#x153;Error Na Jaâ&#x20AC;?); }while(1); printf(â&#x20AC;&#x153;%d+%d=%dâ&#x20AC;?,a,b,a+b); printf(â&#x20AC;&#x153;%d+%d=%dâ&#x20AC;?,a,b,a+b); } }

ภ!&  * *.!   ภภ+*>: ภ0 +* main  &)ภ( ,) * & do { â&#x20AC;Ś } while  &5*!& & &&!ภ.)2/0,* ภ&  +* main   &*!&! $'(ภ), >(ภ 09  !)&! String strcat() ภ String  ) ภภString  strcmp() ภ&& String 2 ! strcpy() ภ   String   2! ) , ()4,*+  â&#x20AC;&#x153;C Programming for Beginnerâ&#x20AC;?) strlen()

isalpha() islower() isupper() isdigit() isspace() tolower()

toupper() ceil() floor() pow() sqrt()

12*& KAPONG CPE#11 Com-Club Homepage [http://student.sut.ac.th/comclub]


- 33 of 40 -

10/16/2008 11:01:00 PM

ภภ ภ (Part I) ภ

 Computer Programming  23ภ" 1.  &'(ภ)*)2 +*ภ a,b,c # $ !)&!* ) ) &)( *! +*ภ>: ภ0 area()

2. 3. 4. 5.

# $>: ภ0 ภ !/ ) &) ( *! ภ+* main (  (+ภ/ *)2  a,b,c $ +*! +) ) ภ&'(ภ)*)2 (  name  ,& >: ภ0 backward() >: ภ0ภภ String ( *!  +*ภ main ,)0   &'(ภ)*)2 ! !.) 3 *!&>: ภ0 findMax() ( *! )ภภ)+* main ( 1

 &>: ภ0 !/  factorial  &>: ภ0 isTriangle() '&*)2 !  3 ! ( *!  ภ)$ 0  1 ภ!   ) )4)* 3  &),*+* 1 ภ) ( 4*,) +* 0 ภ)

$ &&.% & ภfunction

12*& KAPONG CPE#11 Com-Club Homepage [http://student.sut.ac.th/comclub]


- 34 of 40 -

10/16/2008 11:01:00 PM

ภภ ภ (Part I) ภ

 Computer Programming

" %& 6 )?!( (Array)

ภ &  !*  ,**ภ4ภ/0* ) (855) (  ภ.$ ภ</ Array +!* )ภ ภ.,* ))$$

12*& KAPONG CPE#11 Com-Club Homepage [http://student.sut.ac.th/comclub]


- 35 of 40 -

10/16/2008 11:01:00 PM

ภภ ภ (Part I) ภ

 Computer Programming Array

ภKeyword &0#ภภ &  â&#x20AC;&#x153;Array  *)2 &!ภ )ภ  ( (ภ( !(( !) index () ) 6( !â&#x20AC;? ... )ภ(* ภ+& &ภ#ภ $%9/  ภ  !(,) ,*)& !&!( ) &5! ภ!))ภ ( )&!ภ (    ภ   Index  Subscript   ภ  * Index ภ. ))&&)9&) )ภ.)  (

 * (ภ  ภ ( ภภ.+** &!ภ  * .9/20   2 ,)/ &ภ*  (*)2

) ภ.* &ภ!  .9/20 ( 4*&ภ  ภ.* &ภ!  * .9/20   2   *+ภ Concept ภ) !( *!  , * )2! Array *$&  , ...(  ภ! ภ! )4 !* ภ ,*+* Array ,( *!&  ,) 2*! ( ) #  char[] ,*  &ภ!  String  String 4! $ Array of Char  &0 char  ( *!!, ภ! $&0 ภ. [] , 4*. [] * ภ+* 4 Array ,!*ภ )2!9* &0ภภ!  !( !([&0] (={  &0}); (={  &0})  )*ภ+*ภ &0  ( (ภ & )4ภ ภ &0 4*,)   +*ภ error !9(ภ*) 2   !  &0 ,) ภ.,) * ภ +* ภภ (  *  *!&  )& , (comma)  ภ! ) ภ</ *&ภภภ@!(9) ( ) [] ))  )2!&  ภภ@!( &0 ( 42ภ( 1ภ &ภ! 

'&8ภ9 %

'&: int x[]; //,) +  int x[10]; int x[5]={1,2,3,4,5,6}; //ภ ,)  ภ int x[5]={1,2,3,4,5}; int x[] = {1,2,3,4,5}; char name[] = {â&#x20AC;&#x2DC;Hâ&#x20AC;&#x2122;,â&#x20AC;&#x2DC;eâ&#x20AC;&#x2122;,â&#x20AC;&#x2DC;lâ&#x20AC;&#x2122;,â&#x20AC;&#x2DC;lâ&#x20AC;&#x2122;,â&#x20AC;&#x2DC;oâ&#x20AC;&#x2122;}; char name[]= â&#x20AC;&#x153;Helloâ&#x20AC;?; //@< char[]  

 name[] +  +!&  ),)  ภ  2+ sizeof ! ภภ@&    ,* ) ภ1 ภ!!ภ< )ภ* â&#x20AC;&#x2DC;\0â&#x20AC;&#x2122; ภ! $ภภ! $ String !  ( ภภ@((ภ,) * â&#x20AC;&#x2DC;\0â&#x20AC;&#x2122;  ภภ6 ภ.-8 printf %  %s .% sizeof $  +ภ !(  )4 !9&ภ&0( !ภ* ...  &5  !9  !([index] +*) ! $)!(! , &   )2! &  ภ

12*& KAPONG CPE#11 Com-Club Homepage [http://student.sut.ac.th/comclub]


- 36 of 40 -

10/16/2008 11:01:00 PM

ภภ ภ (Part I) ภ

 Computer Programming

 - #include<stdio.h> #include<conio.h> int main(){ int x[3]; x[0]=4; x[1]=2; x[2]=1; printf(â&#x20AC;&#x153;%d+%d+%d=%dâ&#x20AC;?,x[0],x[1],x[2],x[0]+x[1]+x[2]); getch(); return 0; }

1 ภ 4+2+1=7

* ภ!  &0) index  0 ,4 &0 -1 ภ&ภภภ,???   ( ,ภ.,) 2*( 855 ,) error  *! index )4+*!(ภ. ( *!&ภArray '&+*! (( index ,* ( !+; &)+*! ( i,j,k) )2!&  ภ #include<stdio.h> #include<conio.h> int main(){ int x[7],i,sum=0; for(i=0;i<7;i++) x[i]=i+1; for(i=0;i<7;i++) printf(â&#x20AC;&#x153;%d â&#x20AC;?,x[i]); getch(); return 0; }

1 ภ 1234567

 ภภ &0& ) ภ ภ/$ &) ... ) $ Matrix ภ.,* ) ($  & , ภ.,*  ,) ! ภ ... ( &ภ+*) (  ) ! $ Array  Array 2*   int x[2][3] ภ.  )&0 ( 3 !&2 2 ! ( ภ! ))  x[0] $&0 3 ! x[1] $&0 3 ! ภ $&0#* &2 +&0  2!&  ( *!  &&ภ!&  ภ* ( *!*+ŕ¸

12*& KAPONG CPE#11 Com-Club Homepage [http://student.sut.ac.th/comclub]


- 37 of 40 -

10/16/2008 11:01:00 PM

ภภ ภ (Part I) ภ

 Computer Programming

 - #include<stdio.h> #include<conio.h> int main(){ int x[2][3],i,sum=0; for(i=0;i<3;i++) x[0][i]=i+1; for(i=0;i<3;i++) x[1][i]=2*i+1; for(i=0;i<3;i++) printf(â&#x20AC;&#x153;%d â&#x20AC;?,x[0][i]); printf(â&#x20AC;&#x153;\nâ&#x20AC;?); for(i=0;i<3;i++) printf(â&#x20AC;&#x153;%d â&#x20AC;?,x[1][i]); getch(); return 0; }

1 ภ 123 135

 23ภ" 1.  &'(ภ)! ! 15 !ภ12*+* ( *!ภ(  )) ( *! 2   2. 3. 4. 5. 6. 7.

( 1 !)    )  &'(ภ)  n ภ12*+* '& 5<n<15 ( *!ภ! ! n !ภ12+* * ( *!+*( 1

90 (* 1 '(ภ) *&ภ* 2 ( ! +*! ,*6! ,) ภ 20 ( *!ภ( 1

 &'(ภ)  String 1 !ภ12*+*( *!ภ!!)0+; !)0 .ภ(   !  ( *! )0ภ *  &'(ภ)  String ภ12+* * ! 5 !( *!ภ! !,) !)&!)ภ  &'(ภ) ! !.)ภ12*+* ( *!ภ)0   2 (    '&ภ)0  ภ&'(ภ) *ภ< &+ String '&ภ String ( !ภ<* ภ *ภ12*+* ( *! ภ)0* !)ภ)!  &+ String )!ภ<ภ! ( ( +* String : The river is no return Character : r Found character r = 4 in the position : 5 9 17 21

12*& KAPONG CPE#11 Com-Club Homepage [http://student.sut.ac.th/comclub]


- 38 of 40 -

10/16/2008 11:01:00 PM

ภภ ภ (Part I) ภ

 Computer Programming

-  6,#-.- 30 $ภ6 &*ภ   &ภ*) *   ( *! &26 &   *$ ( 4*  ,ภ.,) ,*,*  +!2ภ ( *!2ภ6 &!  ภ)& ภ,)  ภ. &)4)0))( *!ภ  ) )ภ &ภ!   *** ภ5* ภภ! ภ** 2*5 ภ2,) & ,*  !&    + ' *'&0 ) ; &2 ( *!,) ภ)&2 ( *!ภ.,) * + *,)  ภ,) )ภ .* + *,)*!& ภ&    1

90 * 2! )!*!   ( ภ&)

% ภ/4 1/2550 %& 1 (% ภ/4 1/2550) 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24.

#include<stdio.h> #include<string.h> ------------- &  --------------; void main(){ int i,n[10],x; ------------- &  --------------; for(i=0;i<x;i++){ n[i] = 'A'+i; if(n[i]%2==0) printf("n[%d] = %c\n",i,n[i]); else printf("n[%d] = %d\n",i,n[i]); } } int test(){ int k; char str[11]; do{ printf("Enter a string (5-10 characters) : "); gets(str); k=strlen(str); }while(------------- &  --------------); return k; }

& 3 >: ภ0 prototype  >: ภ0 test()  int test() & 6  +*+ภ&ภ>: ภ0 test()  x=test() & 22  + *+ภ&&'&+* || $!)  k<5 || k>10

& 22  + *+ภ&&'&+* && $!)  k>=5 && k<=10

4*12*+*C â&#x20AC;&#x153;AAAAAAâ&#x20AC;? *)   ( ภ   n[0] = n[1] = n[2] = n[3] = n[4] = n[5] =

65 B 67 D 69 F

12*& KAPONG CPE#11 Com-Club Homepage [http://student.sut.ac.th/comclub]


- 39 of 40 -

10/16/2008 11:01:00 PM

ภภ ภ (Part I) ภ

 Computer Programming %& 2 (ภ/4 1/2550) 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16.

#include<stdio.h> #include<string.h> void main(){ char str1[30]="Trimester 1/2550" ,str2[30]="Com Pro"; int n1,n2,n3; n1=sizeof(str1); n2=strlen(str1); strcpy(str2,"SUT"); strcat(str1,str2); n3=strlen(str2); printf("n1=%d\n",n1); printf("n2=%d\n",n2); printf("n3=%d\n",n3); printf("str1=%s\n",str1); printf("str2=%s\n",str2); }

ภภ'(ภ)  ( ภ    n1 = n2 = n3 = str1 = str2 =

30 16 3 Trimester 1/2550SUT SUT

%& 3 (1/2550) 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20.

#include<stdio.h> void main(){ int test[3][5]={15,14,13,12 ,11,10,9,8,7,6,5,4,3,2,1}; int i,j,a=0,b=0,c=0; for(i=0;i<3;i++){ b++; for(j=0;j<5;j++){ if(j%2==1) continue; c=c+test[i][j]; a++; i++; } } printf("i=%d\n",i); printf("j=%d\n",j); printf("a=%d\n",a); printf("b=%d\n",b); printf("c=%d\n",c); }

ภภ'(ภ) ( 1 ภ *  i= j= a= b= c=

4 5 3 1 24

12*& KAPONG CPE#11 Com-Club Homepage [http://student.sut.ac.th/comclub]


- 40 of 40 -

10/16/2008 11:01:00 PM

ภภ ภ (Part I) ภ

 Computer Programming %& 4 (1/2550) 1. #include<stdio.h> 2. void main(){ 3. char ch='C'; 4. int i=1, total=0; 5. printf("Output: "); 6. while(i){ 7. if(ch>'E') 8. i=0; 9. else if (ch%2==0) 10. ch=ch+2; 11. else if (ch<'E') 12. ch--; 13. printf("%c ",ch); 14. total++; 15. } 16. printf("\nTotal = %d",total); 17. }

ภภ'(ภ) 1

90    Output: B D F F Total = 4

$% ภภภ!&   ( * )&) (, &  ) ภ ( *!)*)+5  !)ภ ( !) .,*!.))  http://student.sut.ac.th/comclub ภ) 4),) *++ !,ภ., '4),*    ' ?# ภ/ภ!&.ภภ#1 ภ- .%  $) -< //ภ,=ภ,ภ ภ-   / ภ5  â&#x20AC;&#x153;â&#x20AC;?    / 5 5 * 5 )) )!0+ *ภ

&/ ภภ0 '    . :9% :  ภ

KAPONG CPE#11

:9%

12*& KAPONG CPE#11 Com-Club Homepage [http://student.sut.ac.th/comclub]


Compro - Comclub  

ติวคอมโปร โดย ชมรมคอมพิวเตอร์

Advertisement
Read more
Read more
Similar to
Popular now
Just for you