C Language Program

3,469 views
3,342 views

Published on

C Language Program

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

  • Be the first to like this

No Downloads
Views
Total views
3,469
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
165
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

C Language Program

  1. 1. Chapter 2C Language Program Mr.Warawut Khangkhan Twitter: http://twitter.com/awarawut Facebook: http://www.facebook.com/AjWarawut E-Mail: awarawut@hotmail.com Mobile: 083-0698-410
  2. 2. Contents F C C F C F C C C F (Expression) C Mr.Warawut Khangkhan Chapter 2 C Language Program 2
  3. 3. Contents (Cont.) F C F C Mr.Warawut Khangkhan Chapter 2 C Language Program 3
  4. 4. Mr.Warawut Khangkhan Chapter 2 C Language Program 4
  5. 5. BCPL B CBasic Combined . . 2515Programming PDP-7Language (UNIX) . . 2513 Mr.Warawut Khangkhan Chapter 2 C Language Program 5
  6. 6. F C Mr.Warawut Khangkhan Chapter 2 C Language Program 6
  7. 7. F C (High Level Language) F (OS Independent) F F (Hardware Independent) F (Structure Language) Mr.Warawut Khangkhan Chapter 2 C Language Program 7
  8. 8. CMr.Warawut Khangkhan Chapter 2 C Language Program 8
  9. 9. C ---- ---- ----Source Code F F F source code Mr.Warawut Khangkhan Chapter 2 C Language Program 9
  10. 10. C F (Compiler) F F F F FF F F F F F F Mr.Warawut Khangkhan Chapter 2 C Language Program 10
  11. 11. FMain() F F F{printf(“XX”);printf(“YY”);} F F F F F Mr.Warawut Khangkhan Chapter 2 C Language Program 11
  12. 12. F F C F F F F F F copydisk F Fsource code F F F (Binary) executable file (.exe) Mr.Warawut Khangkhan Chapter 2 C Language Program 12
  13. 13. F C Mr.Warawut Khangkhan Chapter 2 C Language Program 13
  14. 14. F C// Comment#include <stdio.h> /* Hearder file */… Data declarations …;int main( ) { … Executable statement …;} Mr.Warawut Khangkhan Chapter 2 C Language Program 14
  15. 15. F C// Comment#include <stdio.h> /* Hearder file */… Data declarations …;int main( ) { … Executable statement …;} Mr.Warawut Khangkhan Chapter 2 C Language Program 15
  16. 16. F C// Comment#include <stdio.h> /* Hearder file */… Data declarations …; Header File Fint main( ) { F F … Executable statement …;} F F F F F F Preprocessor Directive Mr.Warawut Khangkhan Chapter 2 C Language Program 16
  17. 17. F C// Comment#include <stdio.h> /* Hearder file */… Data declarations …; Fint main( ) { … Executable statement …;} data_type variable ; Mr.Warawut Khangkhan Chapter 2 C Language Program 17
  18. 18. main Function F F F C// Comment { }#include <stdio.h> /* Hearder file */ function function main… Data declarations …; Fint main( ) { … Executable statement …;} Mr.Warawut Khangkhan Chapter 2 C Language Program 18
  19. 19. F C Mr.Warawut Khangkhan Chapter 2 C Language Program 19
  20. 20. F C F F F F { } F (function) F main( ) F ; (semi colon) F F F , (comma) F(Parameters) F /* … */ F F F Fcompiler Mr.Warawut Khangkhan Chapter 2 C Language Program 20
  21. 21. Example ex01.c ex01.c#include <stdio.h> //#include “stdio.h”#include <conio.h>int main( ) { printf(“Hello World!!!”); getch( ); return 0;} Mr.Warawut Khangkhan Chapter 2 C Language Program 21
  22. 22. Example ex02.c ex02.c#include <stdio.h>#include <stdlib.h>int main( ) { printf(“Hello World!!!”); system(“PAUSE”); return 0;} Mr.Warawut Khangkhan Chapter 2 C Language Program 22
  23. 23. Example ex03.c ex03.c#include <stdio.h>#include <stdlib.h>int main( ) { printf(“Hello “); printf(“World!!!”); printf(“n”); system(“PAUSE”); return 0;} Mr.Warawut Khangkhan Chapter 2 C Language Program 23
  24. 24. Example ex03.c ex03.c#include <stdio.h>#include <stdlib.h>int main( ) { printf(“Hellon “); printf(“World!!!”); system(“PAUSE”); return 0;} Mr.Warawut Khangkhan Chapter 2 C Language Program 24
  25. 25. CMr.Warawut Khangkhan Chapter 2 C Language Program 25
  26. 26. C (Variable) FF F F F F F F F F F F F F F Mr.Warawut Khangkhan Chapter 2 C Language Program 26
  27. 27. C (Scalar) F F F (Array) F F F F F (Structure)F F F F F F Mr.Warawut Khangkhan Chapter 2 C Language Program 27
  28. 28. (Scalar)F 2Integer typesReal types Mr.Warawut Khangkhan Chapter 2 C Language Program 28
  29. 29. Mr.Warawut Khangkhan Chapter 2 C Language Program 29
  30. 30. Mr.Warawut Khangkhan Chapter 2 C Language Program 30
  31. 31. Example limit.c#include <stdio.h>#include <limits.h>int main( ) { printf(“Minimum char = %dn“, CHAR_MIN); printf(“Maximum char = %dn”, CHAR_MAX); printf(“Minimum int = %in”, INT_MIN); printf(“Maximum int = %in”, INT_MAX); return 0;} Minimum char = -128, Maximum char = 127 Minimum int = -32768, Maximum int = 32767 Mr.Warawut Khangkhan Chapter 2 C Language Program 31
  32. 32. F ( )Char 1 ASCII 1 0 255Int 2 –32768 32767Short 2 –32768 32767Long 4 ± 2000Unsigned Unsigned short = 2 0 65535 Unsigned long = 4 0 4000Float 4 10x x –37 +38Double 8 Mr.Warawut Khangkhan Chapter 2 C Language Program 32
  33. 33. CF F F a-z A-Z _ F A-Z a-z0-9 _ F F F F F F F F F F F Mr.Warawut Khangkhan Chapter 2 C Language Program 33
  34. 34. C (Cont.) F (Reserved Word)auto break case char constcontinue default do double elseenum extern float for gotoif int long register returnshort signed sizeof static structswitch typedef union unsigned voidvolatile while Mr.Warawut Khangkhan Chapter 2 C Language Program 34
  35. 35. F C : :average pi 3rd_entry all$donenumber_of_students the end intentry_total entryTot alall_total allTotal Mr.Warawut Khangkhan Chapter 2 C Language Program 35
  36. 36. C : int num;type name; float y; : char c; double salary;typename ( C) Mr.Warawut Khangkhan Chapter 2 C Language Program 36
  37. 37. C :type name = value; :typename ( C)value F F int num = 10; float y = 5.2; char c = ‘A’; double salary = 25.00; Mr.Warawut Khangkhan Chapter 2 C Language Program 37
  38. 38. CMr.Warawut Khangkhan Chapter 2 C Language Program 38
  39. 39. C (Operator) F F F F (+), (-), (*)(/) F Mr.Warawut Khangkhan Chapter 2 C Language Program 39
  40. 40. C (Cont.)Assignment StatementArithmetic OperatorsIncrement/Decrement OperatorsOther Assignment OperatorsComparison OperatorsLogical Operators Mr.Warawut Khangkhan Chapter 2 C Language Program 40
  41. 41. Assignment Statement F F F F = : variable = expression; : variable expression F F F Mr.Warawut Khangkhan Chapter 2 C Language Program 41
  42. 42. Assignment Statement (Cont.) Mr.Warawut Khangkhan Chapter 2 C Language Program 42
  43. 43. Example term.c#include <stdio.h>int main( ) { int teerm, term_2, term_3; term = 3 * 5; term = 15 term_2 = 2 * term; term_2 = 30 term_3 = 3 * term; term_3 = 45 printf("term = %dn", term); printf("term_2 = %dn", term_2); printf("term_3 = %dn", term_3); return 0;} Mr.Warawut Khangkhan Chapter 2 C Language Program 43
  44. 44. Arithmetic Operators F F F (+), (-),(*) (/) F F F Mr.Warawut Khangkhan Chapter 2 C Language Program 44
  45. 45. Mr.Warawut Khangkhan Chapter 2 C Language Program 45
  46. 46. Example math1.c math1#include <stdio.h> a+b=7int main( ) { a-b=3 a * b = 10 int a = 5, b = 2, z; z = a + b; printf(“a + b = %dn”, z); z = a - b; printf(“a - b = %dn”, z); z = a * b; printf(“a * b = %dn”, z); return 0;} Mr.Warawut Khangkhan Chapter 2 C Language Program 46
  47. 47. Example math2.c math2#include <stdio.h>int main( ) { div = 2 mod = 1 int a = 5, b = 2, z; z = a / b; printf(“div = %dn”, z); z = a % b; printf(“mod = %dn”, z); // printf(“a %% b = %dn”, z); return 0;} Mr.Warawut Khangkhan Chapter 2 C Language Program 47
  48. 48. Increment/Decrement Operators / F 1 F 1 F ++ F 1 F --int i = 5, j = 4;i++; // i = i + 1; “i” becomes 6--j; // j = j – 1; “j” becomes 3++i; // i = i + 1; “i” becomes 7 Mr.Warawut Khangkhan Chapter 2 C Language Program 48
  49. 49. Mr.Warawut Khangkhan Chapter 2 C Language Program 49
  50. 50. Example inc_dec.c#include <stdio.h> n= 5 n= 6int main( ) { n= 6 int n = 5; n= 7 printf(“n = %dn”, n); printf(“++n = %dn”, ++n); printf(“n++ = %dn”, n++); printf(“n = %dn”, n); return 0;} Mr.Warawut Khangkhan Chapter 2 C Language Program 50
  51. 51. Example effect.c#include <stdio.h>int main( ) { 8 int value = 1, result; result = (value++ * 5) + (value++ * 3); printf("%dn", result); return 0;} Mr.Warawut Khangkhan Chapter 2 C Language Program 51
  52. 52. Other Assignment Operators Mr.Warawut Khangkhan Chapter 2 C Language Program 52
  53. 53. Example other_ass.c#include <stdio.h>void main( ) { Before b = 1 After b = 6 int a = 5, b = 1; printf(“Before b = %dn”, b); b += a; // b = b + a; printf(“After b = %dn”, b); return 0;} Mr.Warawut Khangkhan Chapter 2 C Language Program 53
  54. 54. Comparison Operators Mr.Warawut Khangkhan Chapter 2 C Language Program 54
  55. 55. Logical Operators Mr.Warawut Khangkhan Chapter 2 C Language Program 55
  56. 56. Table Logical Operators A B A && B A || B !ATrue True True True FalseTrue False False True FalseFalse True False True TrueFalse False False False True Mr.Warawut Khangkhan Chapter 2 C Language Program 56
  57. 57. F (EXPRESSION) C Mr.Warawut Khangkhan Chapter 2 C Language Program 57
  58. 58. F (Expression) CF (Expression) F F F F F F F y = 2x * 5, 2xy – 5 F Mr.Warawut Khangkhan Chapter 2 C Language Program 58
  59. 59. F C (Cont.)1. F C F C F F F C2. F F F F F F F C F F F F F* x F/ Mr.Warawut Khangkhan Chapter 2 C Language Program 59
  60. 60. F C (Cont.)3. F F F F F C (&&, ||, !) F F F F F F F F c && (a <= b), (b >= c) || (c <= a) Mr.Warawut Khangkhan Chapter 2 C Language Program 60
  61. 61. F C (Cont.)4. F F F F F F F F F a / b + 15 * c (a – b) * 10 / c F F F F C F Mr.Warawut Khangkhan Chapter 2 C Language Program 61
  62. 62. 1 ()2 !, ++, --, (typecast)3 *, /, %4 +, -5 <, <=, >, >= Mr.Warawut Khangkhan Chapter 2 C Language Program 62
  63. 63. (Cont.)6 = =, !=7 &&8 ||9 *=, /=, %=, +=, -= Mr.Warawut Khangkhan Chapter 2 C Language Program 63
  64. 64. FC Mr.Warawut Khangkhan Chapter 2 C Language Program 64
  65. 65. F C F F F F int + float int – char F F F F C F Casting 2◦ F◦ F F Mr.Warawut Khangkhan Chapter 2 C Language Program 65
  66. 66. FF C F F F F F Mr.Warawut Khangkhan Chapter 2 C Language Program 66
  67. 67. FVar. 1 Var. 2 Var. 3 char int chat -> int int long int -> long int unsigned int int -> unsigned int int float int -> float int double int -> double float double float -> double long double long -> doubledouble long double double -> long double
  68. 68. Example implicit.c#include <stdio.h>int main( ) { int n = 5; float f = 3.5, r; r = 8.500000 r = n + f; printf(“r = %fn”, r); return 0;} Mr.Warawut Khangkhan Chapter 2 C Language Program 68
  69. 69. F F F F F F :(type)expression; :type Fexpression F F Mr.Warawut Khangkhan Chapter 2 C Language Program 69
  70. 70. Example explicit.c#include <stdio.h> n = 5.000000int main( ) { f=3 int n = 5; n+f=8 float f = 3.2; printf(“n = %fn”, (float)n); printf(“f = %dn”, (int)f); printf(“n + f = %dn”, (n + (int)f)); return 0;} Mr.Warawut Khangkhan Chapter 2 C Language Program 70
  71. 71. FC Mr.Warawut Khangkhan Chapter 2 C Language Program 71
  72. 72. C F F F F F F printf( ) F F F F FF F :printf ( format, exp.1, exp.2, …); :format F Fexp.1, exp.2 F F Mr.Warawut Khangkhan Chapter 2 C Language Program 72
  73. 73. C (Cont.)Mr.Warawut Khangkhan Chapter 2 C Language Program 73
  74. 74. F%d %i %u %0 %x %f (6 ) %e E e Mr.Warawut Khangkhan Chapter 2 C Language Program 74
  75. 75. (Cont.) F%c 1 (char)%s F (string)%p F (pointer) Mr.Warawut Khangkhan Chapter 2 C Language Program 75
  76. 76. n Ft F F F 1 tab ( F 6 )r F cursor F Ff F F F 1 Fb F 1 Mr.Warawut Khangkhan Chapter 2 C Language Program 76
  77. 77. Example control.c#include <stdio.h> 5 10int main( ) { 15 int a = 5, b = 10, c; printf(“%dt%dn”, a, b); c = a + b; printf(“%d”, c); return 0;} Mr.Warawut Khangkhan Chapter 2 C Language Program 77
  78. 78. F putchar( putchar( ) :putchar (expression); :expression F F F ‘ ’ F ‘A’ Mr.Warawut Khangkhan Chapter 2 C Language Program 78
  79. 79. Example putchar.c#include <stdio.h> Func. printf( ) : Aint main( ) { Func. putchar( ) : A char ch = ‘A’; printf(“Func. printf( ) : %cn”, ch); printf(“Func. putchar( ) : ”); putchar(ch); return 0;} Mr.Warawut Khangkhan Chapter 2 C Language Program 79
  80. 80. F F puts( ) :puts (expression); :expression F F F “ ” F “ABC” Mr.Warawut Khangkhan Chapter 2 C Language Program 80
  81. 81. Example puts.c#include <stdio.h>int main( ) { char str[ ] = “ABC”; printf(“Func. printf( ) : %sn”, str); printf(“Func. puts( ) : ”); puts(str); Func. printf( ) : ABC return 0; Func. putchar( ) : ABC} Mr.Warawut Khangkhan Chapter 2 C Language Program 81
  82. 82. F C F F C F Fscanf( ) F F F F F FF (Integer), (Floating point), (Character) F (String) Mr.Warawut Khangkhan Chapter 2 C Language Program 82
  83. 83. F C (Cont.) :scanf (format, &var.1, &var.2, …); :format F F Fvar.1, var.2 F F F & F F string F F & F Mr.Warawut Khangkhan Chapter 2 C Language Program 83
  84. 84. F F F F scanf( scanf( )scanf(“%d %f %d”, &var1, &var2, &var3);scanf(“%d/%f/%d”, &var1, &var2, &var3);scanf(“%d,%f,%d”, &var1, &var2, &var3);scanf(“%s %s %s”, var1, var2, var3); Mr.Warawut Khangkhan Chapter 2 C Language Program 84
  85. 85. Example scanf1.c scanf1#include <stdio.h> Input number to 2 value : 3 6int main( ) { a+b=9 int a, b, c; printf("Input number to 2 value : "); scanf("%d %d", &a, &b); c = a + b; printf("a + b = %dn", c); return 0;} Mr.Warawut Khangkhan Chapter 2 C Language Program 85
  86. 86. Example scanf2.c scanf2#include <stdio.h> Input number to a : 3int main( ) { Input number to b : 6 int a, b, c; a+b=9 printf("Input number to a : "); scanf("%d", &a); printf(“Input number to b : “); scanf(“%d”, &b); c = a + b; printf("a + b = %dn", c); return 0;} Mr.Warawut Khangkhan Chapter 2 C Language Program 86
  87. 87. F F getchar( getchar( )getch(getch( ) : variable = getchar( ); variable = getch( ); : variable F F ‘ ’ F ‘A’ getchar – F F 1 F (1 ) F enter getch – F F 1 F (1 ) F F enter F F F F Mr.Warawut Khangkhan Chapter 2 C Language Program 87
  88. 88. Example getchar1.c getchar1#include <stdio.h>int main( ) { char ch1; printf(“Enter character 1 : “); scanf(“%c", &ch1); printf(“Result ch1 : %cn”, ch1); return 0;} Mr.Warawut Khangkhan Chapter 2 C Language Program 88
  89. 89. Example getchar2.c getchar2#include <stdio.h>int main( ) { char ch2, ch3; printf(“Enter character 2 : ”); ch2 = getchar( ); printf(“Result ch2 : %cn”, ch2); printf(“Enter character 3 : “); ch3 = getch( ); return 0;} Mr.Warawut Khangkhan Chapter 2 C Language Program 89
  90. 90. F F F gets( ) :gets (expression); :expression F F (F ) F “ ” F “ABC” Mr.Warawut Khangkhan Chapter 2 C Language Program 90
  91. 91. Example scanf1.c scanf1#include <stdio.h>int main( ) { char str[ ] = “”; printf("Input string : "); scanf("%s", str); printf("String : %s", str); return 0;} Mr.Warawut Khangkhan Chapter 2 C Language Program 91
  92. 92. Example gets.c#include <stdio.h>int main( ) { char str[ ] = “”; printf(“Input string: “); gets(str); printf(“String : %s”, str); return 0;} Mr.Warawut Khangkhan Chapter 2 C Language Program 92

×