Successfully reported this slideshow.
Upcoming SlideShare
×

# โปรแกรมภาษาซีเบื้องต้น

1,186 views

Published on

Published in: Education
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

### โปรแกรมภาษาซีเบื้องต้น

1. 1. • Dev-C++:••••• 2
2. 2. Dev-C++• File > New > Source File•• .c  first.c 3
3. 3. Dev-C++•   F9• :  getch() 4
4. 4. main stdio.h #include <stdio.h> int int main() { int a, b, sum;printf() printf("Enter A: "); int scanf("%d", &a); a, b sum printf("Enter B: "); scanf("%d", &b); scanf() sum = a+b; printf("Sum = %dn", sum); getch(); return 0; } 0 ; 5
5. 5. (Comment)•• /* … ( )… */ // … ( )… /* arithmetic expression */ // create 6
6. 6. a(Beep) n (newline) backslash ’ 7
7. 7. (address) (data) 0000 32 1 =1 8 0001 67 0002 255 0003 0 0004 121 : : 8
8. 8. ( )char 1 -128 +127short 2 -32,768 +32,767int 2 -32,768 - +32,767 4 2,147,483,648 +2,147,483,647long 4 -2,147,483,648 +2,147,483,647float 4 3.4 * 10-38 3.4 * 1038double 8 1.7 * 10-308 1.7 * 10-308 9
9. 9. (Variable)•  ( ) : : short a, b, sum; 1000 10 2 a a = 10; 1001 30 0 b = 5; 1002 211 5 b sum = a+b; 1003 0 5 1004 15 8 sum 1005 23 0 : : b 5 ) &b 1002 10
10. 10. (Variable) underscore ( _ ) underscore ( _ ) 1-32 \$ Case sensitive Reserve words 11
11. 11. Cauto double int structbreak else long switchcase enum register typedefchar extern return unionconst float short unsignedcontinue for signed voiddefault goto sizeof while do 12
12. 12. type variable-list [=value] ;• variable-list (,)  type  value 13
13. 13. int a=1;int lower;float man,ratio;double point;char ch,c=„5‟,name; 14
14. 14. #include <stdio.h>#define PI 3.414 const.cint main(){ 3.414 printf("%.3f",PI); getch(); return 0;} 15
15. 15. •• short arr[3]; : : arr[0] = 50; 0020 50 2 arr arr[2] = 89; 0021 30 0 0022 211 arr 20 0023 5 arr[0] 50 0024 89 8 arr[1] 211 0025 23 0 arr[2] 89 0026 33 0027 12 : : 16
16. 16. • “ ” (array of char) char s[5] = "Hello"; printf("%c %c %cn", s[0], s[1], s[2]); 0 1 2 3 4 S H e l l o : H e l 17
17. 17. /• ( / ) “stdio.h”•  scanf ( %format)• (Output Function)  printf ( %format) 18
18. 18. •• printfint c = 65; : 2printf("c (as a number) = %dn", c); 30printf("c (as a character) = %cn", c); 65 c 5 : 8c (as a number) = 65 23c (as a character) = A : 19
19. 19. %format scanf printf %for mat int %d long %ld long long %lld unsigned int %u unsigned long %lu unsigned long long %llu float %f double %lf char %c char array[] %s 20
20. 20. scanfchar name[20];int age;printf("Enter your name and age: ");scanf("%s %d", name, &age);printf("Hello %s. You are %d years old.n", name, age); :Enter your name and age: Tony 38Hello Tony. You are 38 years old. 21
21. 21. • START Statement1 Statement2 START Statement3 Statement END Statementn END 22
22. 22. Relational Operators• (Operand)Operator < <= > >= == != 23
23. 23. Expression• (Operand) (Operator)A = 10; B = 3; C = 20;Expressio nA+B 13(A + B) – C -7(C*B)+(A*B) 90A%B 1C%B 2 24
24. 24. Relational Operators• Relational Operatorsi = 1; j = 2; k = 3; Expression i<j true 1 (i + j) >= k true 1 (j + k) > (i + 5) false 0 k != 3 false 0 j == 2 true 1 25
25. 25. Logical (Bitwise) Operators• (Operand) Operator Operator) (Logical && AND || OR 26
26. 26. AND Operators• A B A && BTrue True T && T True 1True False T && F False 0False True F && T False 0False False F && F False 0 27
27. 27. AND Operators• AND Operatorsi = 8; j = 4.5; k = „z‟; // ascii z 122 Expression(i >= 6) && (k == „z‟) T && T == T 1(i >= 6) && (k == 122) T && T == T 1(j >= 0) && (j != 4.5) T && F == F 0(k > 0) && (j != 5) T && T == T 1(i < 0) && (j > 0) && (k > 0) F && T && T == 0 F 28
28. 28. OR Operators• A B A || BTrue True T || T True 1True False T || F True 1False True F || T True 1False False F || F False 0 29
29. 29. OR Operators• OR Operatorsi = 8; j = 4.5; k = „z‟; // ascii z 122 Expression(i >= 6) || (k == „z‟) T || T == T 1(i >= 6) || (k != 122) T || F == T 1(j < 0) || (j != 4.5) F || F == F 0(k > 0) || (j != 5) T || T == T 1 30
30. 30. •i = 7; f = 4.5; Expressionf>5 False 0!(f > 5) True 1i <= 3 False 0!(i <=3) True 1i > (f + 1) True 1!(i > (f + 1)) False 0 31
31. 31. (Precedence)• Operatorsunary operators - , ++ , -- , ! , sizeof() R->Lmultiply, divide and *,/,% L->Rremainderadd and subtract +,- L->Rrelational < , <= , > , >= L->Requality == , != L->R 32
32. 32. (Precedence)• Operatorsand && L->Ror || L->Rassignment operators =, +=, -=, *=, /=, %= R->L 33
33. 33. i = 8; j = 4.5; k = „z‟; // ascii z 122 Expressioni + j <= 10 False 0i >= 8 && k == „z‟ True && True == True 1k != „a‟ || i + j <= 10 True || False == True 1k != „a‟ || i < j && k < i True || False && False 0 == True && False == Falsek != „a‟ || i < j && k < i True || False && False 1 == True || False == True 34
34. 34. •  if  if…else  if  switch-case•  while loop  do…while loop  for loop• 35
35. 35. if C Syntax Flowchart if (condition) START { statement1; : true condition statementN; } false Statement• condition Statement int• {} END condition• 36
36. 36. if#include <stdio.h> if1.cint main(){ I>J int i=101,j=100; if(i>j) printf("I > J"); getch(); return 0;} 37
37. 37. if…else Flowchart C Syntax START if (condition) { true false statementt1; condition statementt2;Statementt1 Statementf1 } elseStatementt2 Statementf2 { statementf1; statementf2; END } 38
38. 38. if…else#include <stdio.h> ifelse1.cint main(){ I <= J int i=101,j=102; if(i>j) printf("I > J"); else printf("I <= J"); getch(); return 0;} 39
39. 39. if if (x==1) true Action1; x==1 Action1; false else if (x==2) x==2 true Action2; Action2; false else if (x==3) true x==3 Action3; Action3; false true else if (x==4) x==4 Action4; false Action4;Default_Action; else Default_Action; 40
40. 40. if#include <stdio.h> ifelse2.cint main(){ >6 int i=7; if(i>7) printf("> 7"); else if(i>6) printf("> 6"); else if(i>5) printf("i> 5"); else printf("1 , 2 , 3"); getch(); return 0;} 41
41. 41. switch-case switch (x) x==1 true Action1; { case 1: Action1; false true break; x==2 Action2; case 2: Action2; false true break; x==3 Action3; case 3: Action3; false true break; x==4 Action4; case 4: Action4; false break;Default_Action; default: Default_Action; break; } 42
42. 42. switch-case#include <stdio.h>int main() switch1.c{ int i=2; switch(i) { 2 case 2 : printf("2"); break; case 1 : printf("1"); break; default : printf("NO MATCH"); break; } getch(); return 0;} 43
43. 43. i++ = i= i--1 = i+ i = i-1 i+=5 = i=i+i-=5 = 5 i = i-5 44
44. 44. while while (condition) START { stmt1; stmt2; : false condition stmtN; } true Statement• stmt1 stmtN Statement condition END 45
45. 45. Counting Loop)•• int i, sum = 0; i = 1; while (i <= 10) { sum = sum + i; i = i + 1; } printf("Sum = %dn", sum); 46
46. 46. while#include <stdio.h>int main() while1.c{ Hello 1 Hello 2 int i=1; Hello 3 while(i<=10) Hello 4 { Hello 5 Hello 6 printf("Hello %dn",i); Hello 7 i++; Hello 8 } Hello 9 Hello 10 getch(); return 0;} 47
47. 47. while (INFINITY LOOP)#include <stdio.h>int main() while2.c{ Hello 1 int i=1; Hello 2 while(1) Hello 3 { Hello 4 printf("Hello %dn",i); Hello 5 if(i==10) Hello 6 Hello 7 break; Hello 8 i++; Hello 9 } Hello 10 getch(); return 0;} 48
48. 48. do…while do START { stmt1; stmt2; : Statement1 stmtN; } while (condition); StatementN• stmt1...stmtN true condition condition false• END stmt1...stmtN 49
49. 49. do…while#include <stdio.h> dowhile1.cint main(){ Hello 1 int i=1; Hello 2 do Hello 3 Hello 4 { Hello 5 printf("Hello %dn",i); Hello 6 i++; Hello 7 } Hello 8 while(i<=10); Hello 9 getch(); Hello 10 return 0;} 50
50. 50. for• for (init_stmt; condition; update_stmt) { statement1; statement2; : statementN; }• 1. init_stmt 2. condition statement1...statementN 51
51. 51. for START init_stmtfor (init_stmt; condition; update_stmt){ statement1; false statement2; condition : statementN;} true Statement1 StatementN update_stmt END 52
52. 52. for#include <stdio.h>int main() for1.c{ Hello 1 Hello 2 int i; Hello 3 for(i=1;i<=10;i++) Hello 4 { Hello 5 Hello 6 printf("Hello %d",i); Hello 7 printf("n"); Hello 8 } Hello 9 Hello 10 getch(); return 0;} 53
53. 53. (Subroutine)• (Function)••     54
54. 54. • (Standard Functions)   printf(), scanf(), ...• (User Defined Functions)   main() 55
55. 55. scanf, printf,stdio.h gets, putsmath.h sin, cos, exp, pow isalpha, isdigit,ctype.h islower, isupperstring.h strlen, strcpy, strcmpstdlib.h rand, atoi, atof 56
56. 56. • void say_hi(char *name) {  printf("Hi, %sn", name); void }  return int max(int a, int b) {• if (a > b) return a; else  return b; }  return 57
57. 57. #include <stdio.h>int incr(int i){ int j; function1.c j = i + 1; return j;}int main(){ int k, m = 4; k = incr(m); printf ("k = %d, m = %dn", k, m); getch(); return 0;} 58
58. 58. (Function) #include <stdio.h> void print1() { printf(“Hello”); function2.c printf(“n”); } int main() { print1(); print1(); getch(); return 0; } 59
59. 59. (Function)#include <stdio.h>float cal_tax(float i){ function3.c float ctax; ctax = i*0.07; return ctax;}int main(){ float money=7290,ff; ff = cal_tax(money); printf(“%f”,ff); getch(); return 0;} 60
60. 60. •• 61
61. 61. • Enter N: 3 Enter N: 5 * * ** ** *** *** **** ***** 62
62. 62. #include <stdio.h>int main (){ i scanf int i; scanf("%d", i); if (i = 0) puts("false"); (=) else puts("true"); (==) return 0;} 63