Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction To C Programming ปรีดา เลิศพงศ์วิภูษณะ ภาควิชาวิศวกรรมคอมพิวเตอร์ คณะวิศวกรรมศาสตร์ มหาวิทยาลัยเกษตรศาสตร์

Similar presentations


Presentation on theme: "Introduction To C Programming ปรีดา เลิศพงศ์วิภูษณะ ภาควิชาวิศวกรรมคอมพิวเตอร์ คณะวิศวกรรมศาสตร์ มหาวิทยาลัยเกษตรศาสตร์"— Presentation transcript:

1 Introduction To C Programming ปรีดา เลิศพงศ์วิภูษณะ plw@ku.ac.th ภาควิชาวิศวกรรมคอมพิวเตอร์ คณะวิศวกรรมศาสตร์ มหาวิทยาลัยเกษตรศาสตร์

2 C Programming Language 204212 Abstract Data Type and Problem Solver 2 Why use C General-purpose programming language Closely associated with the UNIX and Linux system –C was developed on UNIX –UNIX, Linux and its software are written in C. 13,000 lines of code 800 lines at the very lowest level in assembly System programming language Low-Level language with High-Level structure High-Level language with Low-Level efficiency

3 C Programming Language 204212 Abstract Data Type and Problem Solver 3 The C Programming Language A block structured language The language is weakly typed (explicit type conversion not required). For example, –arithmetic expressions may involve operand of type character and integer; –no type checking between actual and format parameters in a procedure call

4 C Programming Language 204212 Abstract Data Type and Problem Solver 4 Operation systems System programming Compilers Commercial applications Database management systems C has many application areas

5 C Programming Language 204212 Abstract Data Type and Problem Solver 5 Characteristics of C Structured Modular (compiled separately) Free format (read ability) Single thread (one input, one output) Relatively low level

6 C Programming Language 204212 Abstract Data Type and Problem Solver 6 Flow control for structured program –decision making (if) –looping with the termination test at the top (while, for) test at the bottom (do) selecting one of a set of possible case (switch) Characteristics of C

7 C Programming Language 204212 Abstract Data Type and Problem Solver 7 Characteristics of C Call by value (array names are passed as a location of the array of the array origin [call by reference]) Any function may be called recursively Function definitions may not be nested Compact object code Portable

8 C Programming Language 204212 Abstract Data Type and Problem Solver 8 Getting Start The only way to learn a new programming Language is by writing program in it. I want to print this Hello, world using any editor (vi) to edit hello.c main() { /* This is my first C program */ printf(“Hello, world\n”); printf(“Hello, world\n”);}

9 C Programming Language 204212 Abstract Data Type and Problem Solver 9 Compiling gcc -c hello.c gcc -c -o hello hello.c gcc -c -o {O-FileName} {C-FileName} Linking gcc hello.o gcc -o hello hello.o gcc -o {E-FileName} {O-FileName} Compiling & Linking gcc Compiler Hello.c Hello.o, a.out gcc Compiler Hello.o Hello.o, a.out

10 C Programming Language 204212 Abstract Data Type and Problem Solver 10 Runninghello{E-FileName} $./hello Hello, world $./a.out Hello, world $ _ Running

11 C Programming Language 204212 Abstract Data Type and Problem Solver 11 Component of C program main() function name { start of function { start of function.. function body. function body.. } end of function } end of function

12 C Programming Language 204212 Abstract Data Type and Problem Solver 12 main main() function name main is special function name. Every program must have a main() function to show the compiler where the program start, this function can be used only once. –C dose make a distinction between upper and lowercase letter. MAIN, Main, main are not the same. –a-z, A-Z, 0-9, _ –first character cannot be a digit, may be more than 8 char long, but on some system not all characters are significant Component of C program

13 C Programming Language 204212 Abstract Data Type and Problem Solver 13 Lexical Elements Whitespace and Line Termination In C source programs the blank (space), end-of-line, vertical-tab, form feed, and horizontal-tab are know collectively as whitespace character. They are used to separate when they appear in character or string constants.

14 C Programming Language 204212 Abstract Data Type and Problem Solver 14 Character Encodings Each character in a computer’s character set will have some conventional encoding. ‘A’ = 65 ‘Z’ = 90 ‘Z’- ‘A’ + 1 = ? (26) Lexical Elements

15 C Programming Language 204212 Abstract Data Type and Problem Solver 15 Comments Comments may contain any number of character and are always treated as whitespace Begin with /*, end with */ Legal anywhere a space is legal /* in-line comment */ /* in-line comment */ /* /* * * Header Comment * Header Comment * */ */

16 C Programming Language 204212 Abstract Data Type and Problem Solver 16 Comments แบบครั้งละบรรทัดใช้ //...EOL เช่น printf(“Test Program”) // ปิดหน้าต่าง แบบหลายบรรทัดใช้ /*...*/ เช่น /*The purpose of this script is to calculate the total from three difference variable.*/

17 C Programming Language 204212 Abstract Data Type and Problem Solver 17 void Squares() /* no arguments */ { int i; int i; /* /* loop from 1 to 10 loop from 1 to 10 printing out the squares printing out the squares */ */ for (i = 1; i <= 10; i++) for (i = 1; i <= 10; i++) printf(“%d squared is %d\n”, i, i*i); printf(“%d squared is %d\n”, i, i*i);} Example of Comments

18 C Programming Language 204212 Abstract Data Type and Problem Solver 18 Identifiers identifier must not begin with a digitit must not have the same as a reserved word. An identifier, also called name in C is a sequence of letters, digits, and underscores. An identifiers must not begin with a digit, and it must not have the same as a reserved word. averylongidentifier avery_long_identifier avery_long_identifier AveryLongIdentifier AveryLongIdentifier

19 C Programming Language 204212 Abstract Data Type and Problem Solver 19 Identifier Name ต้องเริ่มต้นด้วยตัวอักษร ยาวสุดไม่เกิน 40 ตัวอักษร จะต้องไม่มีช่องว่าง สามารถใช้ตัวเลขได้ ใช้อักษรพิเศษต่อไปนี้ได้ _ มีความแตกต่างระหว่างอักษรตัวใหญ่และตัวเล็ก

20 C Programming Language 204212 Abstract Data Type and Problem Solver 20 Reserved word The following identifier have been reserved and must not be used as program identifiers asm default float register asm default float register auto do for return auto do for return break double fortran short break double fortran short case else goto signed case else goto signed const enum int static const enum int static continue extern long struct continue extern long struct

21 C Programming Language 204212 Abstract Data Type and Problem Solver 21 Integer Constants decimal-constants 123 123L 456 456L 789 789L

22 C Programming Language 204212 Abstract Data Type and Problem Solver 22 octal-constants 077 077L 076 076L 012 012L hexadecimal-constants 0xFF 0xFFL 0x1A 0x1AL 0x2A 0x2AL Integer Constants

23 C Programming Language 204212 Abstract Data Type and Problem Solver 23 Floating-point Constants Fix-point Constants 0.0 0.0F 0.0L 1.0 1.0F 1.0L 1.5 1.5F 1.5l Floating-point Constants 3E1 3E1L 1.0E-3 1.0E-3L 1.0E67 1.0E67L

24 C Programming Language 204212 Abstract Data Type and Problem Solver 24 Character Constants CHARACTER VALUE CHARACTER VALUE ‘a’ 97 ‘a’ 97 ‘ ’ 32 ‘ ’ 32 ‘/r’ 13 ‘/r’ 13 ‘A’ 65 ‘A’ 65 ‘?’ 63 ‘?’ 63 ‘\O’ O ‘\O’ O ‘\377’ 255 ‘\377’ 255 ‘%’ 37 ‘%’ 37 ‘8’ 56 ‘8’ 56 ‘\23’ 19 ‘\23’ 19 ‘\\’ 92 ‘\\’ 92

25 C Programming Language 204212 Abstract Data Type and Problem Solver 25 String Constants CHARACTER VALUE “” ‘\0’ “” ‘\0’ “\\” ‘\’,‘\0’ “\\” ‘\’,‘\0’ “Total” ‘T’,‘o’,‘t’,‘a’,‘l’,‘\0’ “Total” ‘T’,‘o’,‘t’,‘a’,‘l’,‘\0’ “Copyright 1982” ‘C’,‘o’,‘p’,‘y’,‘r’,‘i’,‘g’,‘h’,‘t’, “Copyright 1982” ‘C’,‘o’,‘p’,‘y’,‘r’,‘i’,‘g’,‘h’,‘t’, ‘ ’,’1’,’9’,’8’,’2’,’\0’ ‘ ’,’1’,’9’,’8’,’2’,’\0’ ‘Total’ = ‘T’ ‘Total’ = ‘T’

26 C Programming Language 204212 Abstract Data Type and Problem Solver 26 Escape Characters ‘\Escape-Code’ f Form Feed n r t v \ ‘ “ ? b a Escape code Translation Alert,Bell Backspace Question mask New Line Carriage Return Horizontal tab Vertical tab Backslash Single Quote Double Quote

27 C Programming Language 204212 Abstract Data Type and Problem Solver 27 The C preprocessor The C preprocessor is a simple macro processor conceptually processes the source text of a C program before the compiler reads the source program.

28 C Programming Language 204212 Abstract Data Type and Problem Solver 28 Definition and Replacement #define {First-String} {Second-String} Example #define sum(x,y) x+y result = sum(5,a*b); the preprocessor replaces the source program line with result = 5+a*b;

29 C Programming Language 204212 Abstract Data Type and Problem Solver 29 Simple macro Definitions #define name Sequence-of-Token When the name is encountered in the source program text or other appropriate context, the name is replaced by the body #define BLOCK_SIZE 0*100 #define EOF ‘\004’ #define ERRMSG “***Error***”

30 C Programming Language 204212 Abstract Data Type and Problem Solver 30 #define NUMBER_OF_TAPE_DRIVERS 5 count = NUMBER_OF_TAPE_DRIVES count = 5; Simple macro Definitions

31 C Programming Language 204212 Abstract Data Type and Problem Solver 31 Define Macros with Parameters #define name(n1, n2,..., nn) Sequence-of-Token #define product(x,y) ((x)+(y)) produtct(a+3, b); ((a+3)+(b)); ((a+3)+(b));

32 C Programming Language 204212 Abstract Data Type and Problem Solver 32 Undefining and Redefining Macro #undef name #define NULL 0 #define FUNC(x) x+4 #define NULL 0 #define FUNC(x) x+4 #undef FUNC(x) #define FUNC(y) y+4

33 C Programming Language 204212 Abstract Data Type and Problem Solver 33 #define ON 1 #undef ON #define ON 1 if (ON == 1) printf (); printf (); #undef ON Undefining and Redefining Macro

34 C Programming Language 204212 Abstract Data Type and Problem Solver 34 File Inclusion #include #include Search for the file in certain standard places #include “name” Search for the file in current directory #include “third.h” #include #include

35 C Programming Language 204212 Abstract Data Type and Problem Solver 35 Condition Compilation #if#else#endif#elif #if Constant-Expression-1 Group-of-Lines-1 Group-of-Lines-1 #elif Constant-Expression-2 Group-of-Lines-2 Group-of-Lines-2#else Group-of-Lines-3 Group-of-Lines-3#endif #ifdef name #if defined name

36 C Programming Language 204212 Abstract Data Type and Problem Solver 36 Declaration C Type C Type Void Scalar Function Union Aggregate Type Type Type Type Type Type Type Type pointer Arithmetic array structure type type type type type type type type integral floating-point integral floating-point type type type type enumerated character type type type type

37 C Programming Language 204212 Abstract Data Type and Problem Solver 37 Signed Integer Types short 16 bit int 16-32 bit long 32 bit MicroSoft C short 16 bit -32,768 --> 32,767 int 16 bit -32,768 --> 32,767 long 32 bit -2,147,483,648 --> 2,147,483,647 Data Type

38 C Programming Language 204212 Abstract Data Type and Problem Solver 38 Unsigned Integer Type unsigned short 16 bit unsigned int 16-32 bit unsigned long 32 bit MicroSoft C unsigned short 16 bit 0 --> 65,535 unsigned int 16 bit 0 --> 65,535 unsigned long 32 bit 0 --> 4,294,967,296 Data Type

39 C Programming Language 204212 Abstract Data Type and Problem Solver 39 Character Type unsigned char 8 bit signed char 8 bit MicroSoft C unsigned char 8 bit 0 --> 255 signed char 8 bit -128 --> 127 Data Type

40 C Programming Language 204212 Abstract Data Type and Problem Solver 40 Pointer Types type_name *variable; Generic Pointers void *G_Ptr; void *G_Ptr; int *I_Ptr; int *I_Ptr; char *C_Ptr; char *C_Ptr; G_Ptr = I_Ptr; G_Ptr = I_Ptr; C_Ptr = G_Ptr; C_Ptr = G_Ptr; C_Ptr = I_Ptr; C_Ptr = I_Ptr; Null Pointer (void *) 0 (void *) 0 Address & int Data; int Data; int *DataPtr = &Data; int *DataPtr = &Data;

41 C Programming Language 204212 Abstract Data Type and Problem Solver 41 Array Types type_name variable[N] Array and Pointer int a[10], *ip; ip = a; ip = &a[0]; a[i] a[i]*((a)+(i))

42 C Programming Language 204212 Abstract Data Type and Problem Solver 42 Multidimensional Arrays type_name variable[n1], [n2]...[nn] int matrix [10][10]; int t[2][3]; t[1][2] t[1][2] *(*(t+1)+2) *(*(t+1)+2)

43 C Programming Language 204212 Abstract Data Type and Problem Solver 43

44 C Programming Language 204212 Abstract Data Type and Problem Solver 44 Structure types struct struct_name {Declaration_1; Declaration_2; Declaration_2;.;.; Declaration_N;}; Declaration_N;};Example struct complex { double real; double real; }; double real; };

45 C Programming Language 204212 Abstract Data Type and Problem Solver 45 Example struct A {int X, Y, Z;}; struct A Data, *DataPtr; Data.x = 0; Data.y = 1; Data.z = 2; DataPtr -> x = 0; DataPtr -> y = 1; DataPtr -> z = 2; Structure types

46 C Programming Language 204212 Abstract Data Type and Problem Solver 46 Structure types 2 struct u { double d; char c[2]; char c[2]; int i; int i;}; d {double} (8 bytes) c {char} (2 bytes) i {int} (4 bytes)

47 C Programming Language 204212 Abstract Data Type and Problem Solver 47 Union types union union_name {Declaration_1; Declaration_2; Declaration_2;.;.; Declaration_N;}; Declaration_N;};Example union single_data { char cData; int iData; int iData; long lData; long lData; float fData; float fData; double dData; }; double dData; };

48 C Programming Language 204212 Abstract Data Type and Problem Solver 48 Union types 2 union u { double d; char c[2]; char c[2]; int i; int i;}; d {double} (8 bytes) c {char} (2 bytes) i {int} (4 bytes)

49 C Programming Language 204212 Abstract Data Type and Problem Solver 49 Type defined typedef oldtype newtype Example typedef long bigint; typedef long bigint; typedef struct s { int a; typedef struct s { int a; int b; int b; } Stype; } Stype;

50 C Programming Language 204212 Abstract Data Type and Problem Solver 50 Enumerted Types enum enum_identifier {n1,n2,..., nn} enum colour {red, blue, green}; enum colour c; The first enumeration constant receives the value 0 if no explicit value is specifiedExample enum boy {Bill = 10, enum boy {Bill = 10, John = Bill + 2, John = Bill + 2, Fred = John + 2}; Fred = John + 2};

51 C Programming Language 204212 Abstract Data Type and Problem Solver 51 Function Types Return_Type function_name( Parameter_1, Parameter_2, Parameter_2,.,., Parameter_n) { Parameter_n) {...}

52 C Programming Language 204212 Abstract Data Type and Problem Solver 52 Example int square(int x) { return x*x; return x*x;} Function Types

53 C Programming Language 204212 Abstract Data Type and Problem Solver 53 Pointer to Function void (*fptr)(); int *ptr[10]; int (*fptr[])(); double (*fptr[])(double) = {sin, cos, tan}; (*fptr)(-1.0);(*(double(*)())fptr)(-1.0);(*(double(*)(double))fptr)(-1.0);

54 C Programming Language 204212 Abstract Data Type and Problem Solver 54 qsort Function Void qsort(void *base, int num, int width, int (*compare)(void *elem1, void *elem2)); <0 elem1 < elem2 =0 elem1 = elem2 >0 elem1 > elem2

55 C Programming Language 204212 Abstract Data Type and Problem Solver 55 Memory Function #include #include Buffer Compare int memcmp(void *buf1, void *buf2, int size); int memcmp(void *buf1, void *buf2, int size); void *memchr(void *buf, int c, int size); void *memchr(void *buf, int c, int size); Buffer copy void *memcpy(void *dest, void *src, int size); void *memcpy(void *dest, void *src, int size); void *memmove(void *dest, void *src, int size); void *memmove(void *dest, void *src, int size); Buffer Initialization void *memset(void *dest, int c, int size); void *memset(void *dest, int c, int size);

56 C Programming Language 204212 Abstract Data Type and Problem Solver 56 Memory Function Allocate memory initialized void *malloc(int size); void *malloc(int size); Allocate memory uninitialized void *calloc(int num, int size); void *calloc(int num, int size); Free memory void free(void *); void free(void *); Reallocate memory void *realloc(void *buf,int newsize); void *realloc(void *buf,int newsize); Calculate size sizeof(); sizeof();

57 C Programming Language 204212 Abstract Data Type and Problem Solver 57 Pointer int Data[3]; int *DataPtr; int Data[3][4]; int *DataPtr[4]; int (*DataPtr)[4]; int Data[3][4][5]; int *DataPtr[4][5]; int (*DataPtr)[4][5];

58 C Programming Language 204212 Abstract Data Type and Problem Solver 58 Arithmetic Operators Simple assignment = assignment = assignment Binary Arithmetic + addition + addition - subtraction - subtraction * multiplication * multiplication / division / division % modulus % modulus

59 C Programming Language 204212 Abstract Data Type and Problem Solver 59 Binary Comparative == equal == equal != not equal != not equal > greater than > greater than >= greater than or equal to >= greater than or equal to < less than < less than <= less than or equal to <= less than or equal to Comparative Operators

60 C Programming Language 204212 Abstract Data Type and Problem Solver 60 Logical Operators Binary Logical && Boolean AND && Boolean AND || Boolean OR || Boolean OR Unary Logical ! NOT ! NOT

61 C Programming Language 204212 Abstract Data Type and Problem Solver 61 Example x = a + 1; if ((i >= MAX) || (C[i] = ‘ ’)) {...} Logical Operators

62 C Programming Language 204212 Abstract Data Type and Problem Solver 62 Operator Perference ( ) [ ] ->. ! ~ ++ -- * & sizeof(type) * / % + - > > => => == != &^|&&||?

63 C Programming Language 204212 Abstract Data Type and Problem Solver 63 Assignment Operators and Expressions i = i + 2; i += 2; Most binary operators have a corresponding assignment operator (op=)

64 C Programming Language 204212 Abstract Data Type and Problem Solver 64 Assignment Operators and Expressions x = y++; y = y+1; x = y; x += y+5; x = x+y+5; Operator Example Equivalent ++ i++ i=i+1 -- i-- i=i-1 += i+=5 i=i+5 -= i-=5 i=i-5 *= i*=2 i=i*2 /= i/=2 i=i/2 ^= i^=2 i=i^2

65 C Programming Language 204212 Abstract Data Type and Problem Solver 65 x += 2; --> x = x + 2; x += 2; --> x = x + 2; x -= 2; --> x = x - 2; x -= 2; --> x = x - 2; x *= 2; --> x = x * 2; x *= 2; --> x = x * 2; x /= 2; --> x = x / 2; x /= 2; --> x = x / 2; x %= 2; --> x = x % 2; x %= 2; --> x = x % 2; x x = x x = x << 2; x >>= 2; --> x = x >> 2; x >>= 2; --> x = x >> 2; x &= 2; --> x = x $ 2; x &= 2; --> x = x $ 2; x |= 2; --> x = x | 2; x |= 2; --> x = x | 2; Assignment Operators and Expressions

66 C Programming Language 204212 Abstract Data Type and Problem Solver 66 Statements x = 0i++ An expression such as x = 0 or i++ becomes a statement when it is followed by a semicolon, as in x = 0; x = 0; i++; i++; printf(...); printf(...);

67 C Programming Language 204212 Abstract Data Type and Problem Solver 67 { } The braces { and } are used to group declarations and statement together into a compound statement or block. there is never a semicolon after the right brace that ends a block. { statement statement... } Blocks Statements

68 Flow Control Statement ปรีดา เลิศพงศ์วิภูษณะ plw@nontri.ku.ac.th ภาควิชาวิศวกรรมคอมพิวเตอร์ คณะวิศวกรรมศาสตร์ มหาวิทยาลัยเกษตรศาสตร์

69 C Programming Language 204212 Abstract Data Type and Problem Solver 69 Flow Control Statement if then Statement if then else Statement else if Statement switch Statement for Statement do while Statement do util Statement

70 C Programming Language 204212 Abstract Data Type and Problem Solver 70 IF THEN Statement Single Line Format Multi Line Format IF งานที่ต้องการทำ จริง เท็จ

71 C Programming Language 204212 Abstract Data Type and Problem Solver 71 Single Line Format if (LogicalExpression) Statement; Example if (Score > 95) Grade = ‘A’;

72 C Programming Language 204212 Abstract Data Type and Problem Solver 72 Multiline Format if (LogicalExpression) { StatementBlock } Example if (G == h) { a = a + 1; b = b + 1; }

73 C Programming Language 204212 Abstract Data Type and Problem Solver 73 IF THEN ELSE Statement Single Line Format Multi Line Format IF จริงเท็จ งานที่ 1 งานที่ 2

74 C Programming Language 204212 Abstract Data Type and Problem Solver 74 Single Line Format if (LogicalExpression) Statement1; else Statement2; Example if (Score > 94) Grade = ‘A’; else Grade = ‘F’;

75 C Programming Language 204212 Abstract Data Type and Problem Solver 75 Multiline Format if (LogicalExpression) { StatementBlock } else { StatementBlock } Example if (Score > 94) { Grade = ‘ A ’ ; a = a + 1; } else { Grade = ‘ F ’ ; a = a - 1; }

76 C Programming Language 204212 Abstract Data Type and Problem Solver 76 ELSE IF Statement IF จริงเท็จ งานที่ 1 งานที่ 4 Multi Line Format ELSE IF เท็จ งานที่ 2 ELSE IF เท็จ งานที่ 3 จริง

77 C Programming Language 204212 Abstract Data Type and Problem Solver 77 Multiline Format if (LogicalExpression) { StatementBlock } else if (LogicalExpression) { StatementBlock } else { StatementBlock } Example if (Mark > 90) { Grade = ‘ A ’ ; } else if (Mark > 80) { Grade = ‘ B ’ ; } else { Grade = ‘ C ’ ; }

78 C Programming Language 204212 Abstract Data Type and Problem Solver 78 SWITCH CASE Statement switch (Expression) { case constant_1: StatementBlock_1; break; case constant_2: StatementBlock_2; break; default: StatementBlock_n; break; } CASE งานที่ 1 งานที่ 4 งานที่ 2 งานที่ 3

79 C Programming Language 204212 Abstract Data Type and Problem Solver 79 BREAK Statement break; ใช้คำสั่งนี้เมื่อต้องการหยุดการทำงาน และออก จากคำสั่งนั้นไปทำคำสั่งถัดไป

80 C Programming Language 204212 Abstract Data Type and Problem Solver 80 Looping with the termination test at the top Single Line Format Multi Line Format WHILE DO Statement WHILE เท็จ งานที่ต้องการทำ จริง

81 C Programming Language 204212 Abstract Data Type and Problem Solver 81 Single Line Format while (LogicalExpression) Statement; Example X = 0; while (X < 5) X = X + 1; while ((C = gechar()) ! = EOF) putchar(C);

82 C Programming Language 204212 Abstract Data Type and Problem Solver 82 MultiLine Format while (LogicalExpression) { StatementBlock;. } Example X = 0; while (X < 5) { X = X + 1; } C = gechar(); while (C != EOF) { putchar(C); C = getchar(); } while ((C = gechar()) != EOF) putchar(C); putchar(C);

83 C Programming Language 204212 Abstract Data Type and Problem Solver 83 Looping with the termination test at the bottom. do StatementBlock;. while (LogicalExpression); The statement is executed, then expression is evaluated. If true, statement is evaluated again, and so on. If theexpression becomes false, the loop terminates. The statement is always executed at least once. DO WHILE Statement WHILE เท็จ งานที่ต้องการทำ จริง

84 C Programming Language 204212 Abstract Data Type and Problem Solver 84 DO WHILE Format do StatementBlock; while (LogicalExpression); Example X = 0; do X = X + 1 while (X < 5); X = 5; do X = X - 1 while (x > 0);

85 C Programming Language 204212 Abstract Data Type and Problem Solver 85 Create Loop c = 1; while (c <= 10) { // // คำสั่งอื่นๆ ที่ต้องการให้ ทำงาน // c = c + 1; } C <= 10 C = 1 งานที่ต้องการทำ C = C + 1 Statement ที่ 3 Statement ที่ 2 Statement ที่ 1 ตรวจสอบเงื่อนไข กำหนดค่าเริ่มต้นให้กับตัวนับ เพิ่มค่าให้กับตัวนับ

86 C Programming Language 204212 Abstract Data Type and Problem Solver 86 FOR Statement for (C = N1;C <= N2;C = C + N3) { // // คำสั่งอื่นๆ ที่ต้องการให้ทำงาน // } C <= N2 C = N1 เท็จ งานที่ต้องการทำ C = C + N3 จริง

87 C Programming Language 204212 Abstract Data Type and Problem Solver 87 FOR NEXT Statement for (FirstState; SecondState; ThirdState) { StatementBlock } Example for (i = 1;i <= 1;i++) { k = k + i } for (i = 2;i <= 20;i++) { k = k + i } for (i = -2;i >= -20; i--) { k = k + i }

88 C Programming Language 204212 Abstract Data Type and Problem Solver 88 Break Exiting a loop Control loop exists other than by testing at the top or bottom. The break statement provides an early exit from for, while, and do, just as from switch. A break statement cause the innermost enclosing loop (or switch) to be exited immediately.

89 C Programming Language 204212 Abstract Data Type and Problem Solver 89 Example while ((ch = getchar()) != EOF) { if (ch == ‘\n’) if (ch == ‘\n’) break ; break ; putchar(ch); putchar(ch);} Break Exiting a loop

90 C Programming Language 204212 Abstract Data Type and Problem Solver 90 Continue a loop Continue ignoring Code within a loop causes the next iteration of the enclosing loop (for, while, do) to begin:Example while ((ch = getchar()) != EOF) { if (ch == ‘\n’) if (ch == ‘\n’) continue; continue; putchar(); putchar();};

91 C Programming Language 204212 Abstract Data Type and Problem Solver 91 CONTINUE and BREAK Statement 1 continue กลับไปตรวจสอบเงื่อนไขแล้วทำ LOOP ถัดไป break ออกจาก LOOP หนึ่งลำดับชั้น

92 C Programming Language 204212 Abstract Data Type and Problem Solver 92 Example for (i = 1;i <= 10;i++) if (i > 3) continue; do X = X + 1; if (X = 3) break; while (x <= 10); for (i = 1;i <= 10;i++) { do while (j < 5) { k = i + j; if (K > 25) break; } CONTINUE and BREAK Statement 2

93 C Programming Language 204212 Abstract Data Type and Problem Solver 93 GOTO and Label Statement การประกาศ Label Identifier: Identifier: การใช้คำสั่ง goto goto LabelName; Example if (Flag = 1) goto JumpStep; Total = Total + 1; JumpStep : printf(“ ทดสอบคำนวณถูกต้อง ”);

94 C Programming Language 204212 Abstract Data Type and Problem Solver 94 RETURN Statement เมื่อต้องการหยุดการทำคำสั่งภายใน Function และส่งค่าของข้อมูลกลับใช้คำสั่ง return; return; return VariableName; return VariableName;

95 C Programming Language 204212 Abstract Data Type and Problem Solver 95 Declaration {AccessControl} DataType Identifier{=Value, Identifier{=Value,...}}... Single Variable Example long ll_Temp; char lc_Temp; Multiple Variable Example long ll_t1, ll_t2, ll_t3;

96 C Programming Language 204212 Abstract Data Type and Problem Solver 96 Scope Local Variable Global Variable

97 C Programming Language 204212 Abstract Data Type and Problem Solver 97 Local Variable Local Variable คือการประกาศตัวแปรต่างๆที่ ต้องการใช้งาน ในช่วงของการเขียนโปรแกรม

98 C Programming Language 204212 Abstract Data Type and Problem Solver 98 Global Variable Global Variable คือการประกาศตัวแปรต่างๆที่ ต้องการใช้งาน ติดต่อภายในแอพพลิเคชั่นที่ทำ การประกาศ

99 C Programming Language 204212 Abstract Data Type and Problem Solver 99 Assignment Operator VariableName = Values; VariableName = VariableName; VariableName = FunctionName(); Example ll_t1 = 1; ls_t2 = “test”, s_test.il_t3 = 5; s_test.CompCode[1] = 100;

100 C Programming Language 204212 Abstract Data Type and Problem Solver 100 Question & Answer


Download ppt "Introduction To C Programming ปรีดา เลิศพงศ์วิภูษณะ ภาควิชาวิศวกรรมคอมพิวเตอร์ คณะวิศวกรรมศาสตร์ มหาวิทยาลัยเกษตรศาสตร์"

Similar presentations


Ads by Google