โครงสร้างภาษาซี1

4,928 views

Published on

Published in: Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
4,928
On SlideShare
0
From Embeds
0
Number of Embeds
2,542
Actions
Shares
0
Downloads
107
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

โครงสร้างภาษาซี1

  1. 1. F F
  2. 2. F F F F F F Interactive 2
  3. 3. F Preprocessor Directives Global Declarations void main(void) { Local Declarations Statements } 3
  4. 4. Preprocessor Directives F file F F F (Header File) F Directive # 4
  5. 5. Preprocessor Directives Directive F #include Include text from file #define Define a macro #undef Undefine a macro #if Test if a compile-time condition hold #ifdef Test if a symbol is defined #ifndef Test if a symbol is not defined #else Indicate alternatives if a test fails #elif Combination of #if and #else #endif End a preprocessor conditional #line Give a line number for compiler message #error Terminate processing early #pragma Implementation dependent directive 5
  6. 6. #include F F Compiler F F F Compile F #include FILE NAME F #include stdio.h F F stdio.h #include conio.h F F conio.h 6
  7. 7. #define F F F #define NAME VALUE F #define PI 3.14 F PI F 3.14 #define X 3.14*2 F X F 3.14*2 7
  8. 8. Global Declarations F F F F F F F F F F F 8
  9. 9. Main () function F F F main() F F F ; F F main() F { F } 9
  10. 10. F (Program Comment) F F // comment 1 /* comment 1 */ /* comment F 1 .*/ 10
  11. 11. F F F F F F Interactive 11
  12. 12. #include stdio.h void main() { .; .; } 12
  13. 13. #include stdio.h void main() { printf( Hello World ); // F F Hello World } Hello World 13
  14. 14. #include stdio.h void main() { printf( Hellon ); printf( World ); } Hello World 14
  15. 15. #include stdio.h void main() { printf( Hellon ); printf( Worldn ); } Hello World  15
  16. 16. F F F F F F Interactive 16
  17. 17. #include stdio.h PI 3.14 #define PI 3.14 r area void main() { 20.00 0.0 float r = 20.00, circum = 0.0; 125.6 circum = 2 * PI * r; printf( Circumference is %f Radius is %fn , circum,r); } 17
  18. 18. F F F F F F Interactive 18
  19. 19. F F keyboard scanf(...) monitor printf(...) Memory 19
  20. 20. F F F F F printf() putchar() puts() 20
  21. 21. F F F F F F scanf() getchar() getch() getche() gets() 21
  22. 22. F F F F F printf() putchar() puts() 22
  23. 23. F printf F F F F F #include stdio.h printf(“Control String”,arg1,arg2,arg3,..); printf(“Control String”); 23
  24. 24. printf(“%dn%d”,a,b); %dn%d a,b Memory a 22 b 30 22 22 -> 30 . 30 Output Stream 24
  25. 25. F printf : F printf( Computer Programming 1 ); printf( ComputernProgramming 1 ); printf( Result is %f ,area); printf( Result isn%f ,area); 25
  26. 26. F printf : F F 10 + 3 F START X (1. F 10 + 3 X) 1. X = 10 +3 X = 10 + 3 (2. F X ) 2. PRINT 10 + 3 PRINT X PRINT X END 26
  27. 27. F printf : F n F t F 6 r cursor F F f F 1 F b cursor 1 F F a F F F 27
  28. 28. F printf : F F %d F F %ld F F long int %u F F F %f F F float %lf F F double %Lf F F long double %e F F %c F F (char) %s F F (string) F %% F F % %o F F %x F F %p F F address pointer 28
  29. 29. F printf : F int j=45, k = -123; printf(“Hellon”); float x = 12.34 printf(“Tax is 10%%n”); char c = ‘A’; printf(“%dn”,j); char *m = “Hello”; char mm[6] = “Hello” ; printf(“%dn”,k); printf(“%fn”,x); printf(“%6.2fn”,x); printf(“%cn”,c); printf(“%dn”,c); 29
  30. 30. F printf : F int j=45, k = -123; printf(“%dn”,c+1); float x = 12.34 printf(“%cn”,c+1); char c = ‘A’; printf(“%sn”,m); char *m = “Hello”; printf(“%sn”,mm); char mm[6] = “Hello” ; printf(“%en”,x); printf(“%En”,x); 30
  31. 31. F printf : F 31
  32. 32. F printf : F F %d %4d 12 12 12 123 123 123 1234 1234 1234 12345 12345 12345 32
  33. 33. #include stdio.h #include stdio.h void main() { void main() { printf( Yesn ); printf( Yesn ); printf( Non ); printf( n ); } printf( Non ); } 33
  34. 34. #include stdio.h #include stdio.h void main() { void main() { printf( Yes ); printf( YesnNo ); printf( No ); } } 34
  35. 35. #include < stdio.h > void main ( ) { // number F int number; // F 8 F number number = 8; printf ( The value of number is %d . , number); } The value of number is 8 . 35
  36. 36. #include stdio.h Circumference is 125.6  #define PI 3.14 void main() { float r = 20.00, circum = 0.0; circum = 2 * PI * r; printf( Circumference is %.1fn , circum); } 36
  37. 37. #include stdio.h #define PI 3.14 void main ( ) { float r, area; printf ( This program computes the area of a circlen ); r = 12.5; /* F F 12.5 */ area = PI * r * r; /* = ¶ r2 */ printf ( Area = Pi x radius x radiusn ); printf ( = %.2f x %.2f x %.2fn , PI, r, r); printf ( = %fn , area); This program computes the area of a circle Area = Pi x radius x radius = 3.14 x 12.50 x 12.50 = 490.873444  37
  38. 38. #include < stdio.h > #define SIXTY 60 /* SIXTY F F F */ void main ( ) { float hour; // hour F F int minute, second; // 2 F hour = 1.5; // F 1.5 F hour // F hour F 60 F F F minute minute = hour * SIXTY; // F minute F 60 F F F second second = minute * SIXTY; printf ( In one period : n %.2f hoursn , hour); printf ( %d minutesn%d seconds , minute, second); } In one period : 1.50 hours 90 minutes 5400 seconds 38
  39. 39. F F F F F printf() putchar() puts() 39
  40. 40. F putchar F 1 F #include stdio.h putchar(variable); 40
  41. 41. F putchar : F #include “stdio.h” void main ( ) { char s; s = ‘A’; putchar(s); } A 41
  42. 42. F F F F F printf() putchar() puts() 42
  43. 43. F puts F F F F F 1 variable F F F F F1 puts(variable); 43
  44. 44. F puts : F #include “stdio.h” #include<string.h> void main ( ) { char str[30]; str = “C Programming”; strcpy(str,”C programming”); puts(str); printf(“nOutput = %sn”,str); } C Programming Output = C Programming  44
  45. 45. F F F F F F scanf() getchar() getch(), getche() gets() 45
  46. 46. F scanf F F F F F Enter F F F F F F include stdio.h scanf(“Control String”,&arg1,&arg2,&arg3,..); 46
  47. 47. scanf(“%d%f”,&a,&b); %d%f a,b 22 30 . a 22 b 30 Input Stream int a; float b; scanf( %d%f ,&a,&b); 47
  48. 48. F Scanf : F int num; scanf( %d ,&num); printf( the value in num variable is %d ,num); char str[80]; printf( Enter a string : ); scanf( %s ,str); printf( Here s your string : %s , str); 48
  49. 49. #include < stdio.h > void main ( ) { int years; printf ( How long have you been here? ); scanf ( %d , &years); printf ( You ve been here for %d years. , years); printf ( tReally? ); } How long have you been here? 20 You’ve been here for 20 years. Really? 49
  50. 50. F F F F F F scanf() getchar() getch() ,getche() gets() 50
  51. 51. F getchar F F F F 1 F F 1 Enter F F F F ch = getchar(); 51
  52. 52. F getchar : F #include “stdio.h” void main ( ) { char ch; ch = getchar(); printf(“The Character you typed is %cn”, ch); } d The Character you typed is d 52
  53. 53. F F F F F F scanf() getchar() getch(),getche() gets() 53
  54. 54. F getch(),getche() F F 1 F F F F F Enter ch = getch(); F F 1 F F F F Enter ch = getche(); F #include<conio.h> 54
  55. 55. F getch : F #include “stdio.h” #include “conio.h” void main ( ) { char ch; ch = getch(); printf(“The Character you typed is %cn”, ch); } The Character you typed is d 55
  56. 56. F F F F F F scanf() getchar() getch() gets() 56
  57. 57. F gets F F F F F F F Enter F F F F F F F F gets(variable); 57
  58. 58. F gets : F #include “stdio.h” void main ( ) { char str[51]; gets(str); printf(“The Message you typed is %sn”, str); } test The Message you typed is test 58
  59. 59. F F F F F F Interactive 59
  60. 60. Interactive F F F F F F F F F F F F F F F F F F F F 60

×