37
A Quick Start of C Introduction

A Quick Start of C Introduction. 2 Terminology Program 程式 Programming language 程式語言 Code 程式碼 Compiler 編譯器 –cc, GNU gcc, g++,… –IDE: MS Visual C++, Dev

Embed Size (px)

Citation preview

Page 1: A Quick Start of C Introduction. 2 Terminology Program 程式 Programming language 程式語言 Code 程式碼 Compiler 編譯器 –cc, GNU gcc, g++,… –IDE: MS Visual C++, Dev

A Quick Start of C

Introduction

Page 2: A Quick Start of C Introduction. 2 Terminology Program 程式 Programming language 程式語言 Code 程式碼 Compiler 編譯器 –cc, GNU gcc, g++,… –IDE: MS Visual C++, Dev

2

Terminology

• Program 程式• Programming language 程式語言• Code 程式碼• Compiler 編譯器

– cc, GNU gcc, g++,…– IDE: MS Visual C++, Dev C++, Turbo C,…

Page 3: A Quick Start of C Introduction. 2 Terminology Program 程式 Programming language 程式語言 Code 程式碼 Compiler 編譯器 –cc, GNU gcc, g++,… –IDE: MS Visual C++, Dev

3

Terminology

• Variable 變數• Statement 敍述

– Assignment 給值– Selection 條件判斷 – Iteration (loop) 迴圈

• Array 陣列• Function 函式• Pointer 指標

Page 4: A Quick Start of C Introduction. 2 Terminology Program 程式 Programming language 程式語言 Code 程式碼 Compiler 編譯器 –cc, GNU gcc, g++,… –IDE: MS Visual C++, Dev

4

Writing a Simple Program

#include <stdio.h>int main(){ printf("Hello World!\n"); return 0; }

directives

statements function call

Page 5: A Quick Start of C Introduction. 2 Terminology Program 程式 Programming language 程式語言 Code 程式碼 Compiler 編譯器 –cc, GNU gcc, g++,… –IDE: MS Visual C++, Dev

5

Printing a Pun#include <stdio.h>

int main(){ printf("To C, or not to C: that is the question.\n"); return 0; }

To C, or not to C: that is the question.

Page 6: A Quick Start of C Introduction. 2 Terminology Program 程式 Programming language 程式語言 Code 程式碼 Compiler 編譯器 –cc, GNU gcc, g++,… –IDE: MS Visual C++, Dev

6

2.4 Variables ( 變數 )• Type

A variable of type int can store an integer, e.g. 1392, 0, -2553

A variable of type float can store a real number (stored in the floating-point fashion), e.g. 34.124, -45.435, 0,NOTE: float is just an approximation of the number0.1 would become 0.09999999999999987

Page 7: A Quick Start of C Introduction. 2 Terminology Program 程式 Programming language 程式語言 Code 程式碼 Compiler 編譯器 –cc, GNU gcc, g++,… –IDE: MS Visual C++, Dev

7

Variable Declaration ( 宣告變數 )• Variables must be declared.

• Declaration: varType varName;Ex: int score;score: name of the variableint declares that score is an integer

Page 8: A Quick Start of C Introduction. 2 Terminology Program 程式 Programming language 程式語言 Code 程式碼 Compiler 編譯器 –cc, GNU gcc, g++,… –IDE: MS Visual C++, Dev

8

Variable Declaration

• A variable must be declared before using it.Ex:int score;score = 95; ()

Ex:score = 95;int score; ()

Page 9: A Quick Start of C Introduction. 2 Terminology Program 程式 Programming language 程式語言 Code 程式碼 Compiler 編譯器 –cc, GNU gcc, g++,… –IDE: MS Visual C++, Dev

9

Variable Declaration (Cont.)

• Declare two or more variablesint price;int tip;int total;

int price, tip, total;• Case sensitive

–a1 and A1 are different.

Page 10: A Quick Start of C Introduction. 2 Terminology Program 程式 Programming language 程式語言 Code 程式碼 Compiler 編譯器 –cc, GNU gcc, g++,… –IDE: MS Visual C++, Dev

10

Assignment

• a = 5;• a = b + c;• d = 3 * 5 + sum / 2;• d = max(a,b);// return value of max function• a = a + 2;

CError 2.6CError 2.6 放結果的變數是在等號左邊,不能寫成 b + c = a 。

Page 11: A Quick Start of C Introduction. 2 Terminology Program 程式 Programming language 程式語言 Code 程式碼 Compiler 編譯器 –cc, GNU gcc, g++,… –IDE: MS Visual C++, Dev

11

Print Out an Integer

#include <stdio.h>

int main(){ int score; score = 85; printf(" 成績是: %d\n", score);

return 0;} 用 %d 來印出整數資料

逗號 要印出的變數

Page 12: A Quick Start of C Introduction. 2 Terminology Program 程式 Programming language 程式語言 Code 程式碼 Compiler 編譯器 –cc, GNU gcc, g++,… –IDE: MS Visual C++, Dev

12

Print Out an Integer

#include <stdio.h>

int main(){ int price, tip, total; price = 500; tip = 50; total = price + tip; printf(" 總共要付: %d\n", total); return 0;}

Page 13: A Quick Start of C Introduction. 2 Terminology Program 程式 Programming language 程式語言 Code 程式碼 Compiler 編譯器 –cc, GNU gcc, g++,… –IDE: MS Visual C++, Dev

13

Print Out an Integer

#include <stdio.h>

int main(){ int price, tip, total; price = 500; tip = 50; printf(" 總共要付: %d\n", price + tip); return 0;}

Page 14: A Quick Start of C Introduction. 2 Terminology Program 程式 Programming language 程式語言 Code 程式碼 Compiler 編譯器 –cc, GNU gcc, g++,… –IDE: MS Visual C++, Dev

14

Print Out Many Integers#include <stdio.h>

int main(){ int price, tip, total; price = 500; tip = 50; total = price + tip; printf(" 原價 %d 元 , 小費 %d 元 \n", price, tip); printf(" 總共要付: %d 元 \n", total); return 0;}

Page 15: A Quick Start of C Introduction. 2 Terminology Program 程式 Programming language 程式語言 Code 程式碼 Compiler 編譯器 –cc, GNU gcc, g++,… –IDE: MS Visual C++, Dev

15

Print Out Real Numbers

#include <stdio.h>

int main(){ float radius = 2; float area; area = 3.1416f * radius * radius; printf(" 半徑 %f,\n", radius); printf(" 面積 %.2f\n", area); return 0;}

Page 16: A Quick Start of C Introduction. 2 Terminology Program 程式 Programming language 程式語言 Code 程式碼 Compiler 編譯器 –cc, GNU gcc, g++,… –IDE: MS Visual C++, Dev

16

2.5 Reading Input

• Syntax:

scanf("%d", &varName);

– Ex: scanf("%d", &score);

• The program will read in an integer from the keyboard, then set it as the value of the variable.

&

An integer variable

Page 17: A Quick Start of C Introduction. 2 Terminology Program 程式 Programming language 程式語言 Code 程式碼 Compiler 編譯器 –cc, GNU gcc, g++,… –IDE: MS Visual C++, Dev

17

2.5 Reading Input

• Syntax:

scanf("%f", &varName);

– Ex: scanf("%f", &radius);&

A floating-point variable

Page 18: A Quick Start of C Introduction. 2 Terminology Program 程式 Programming language 程式語言 Code 程式碼 Compiler 編譯器 –cc, GNU gcc, g++,… –IDE: MS Visual C++, Dev

18

4.1 Arithmetic Operators

• Unary operators– For positive/negative numbers

• Binary operators– For addition, subtraction, multiplication,

division, and remainder (modular)

Page 19: A Quick Start of C Introduction. 2 Terminology Program 程式 Programming language 程式語言 Code 程式碼 Compiler 編譯器 –cc, GNU gcc, g++,… –IDE: MS Visual C++, Dev

19

Binary Arithmetic Operators• + ( 加 ) – ( 減 ) * ( 乘 ) / ( 除 )• % ( 餘數 , mod)

– 14%5 is 14 mod 5, which answer is 4• Precedence: * = / = % > + = -

–也就是說,先乘除後加減–所以,a = 3*5+6%4;表示a = (3*5)+(6%4);

Page 20: A Quick Start of C Introduction. 2 Terminology Program 程式 Programming language 程式語言 Code 程式碼 Compiler 編譯器 –cc, GNU gcc, g++,… –IDE: MS Visual C++, Dev

20

Unary Arithmetic Operators• + ( 正 ) – ( 負 )• Precedence: + = - > binary operators

–也就是說,正負號優先於加減乘除–所以,a = 3*-5;表示a = 3*(-5);

Page 21: A Quick Start of C Introduction. 2 Terminology Program 程式 Programming language 程式語言 Code 程式碼 Compiler 編譯器 –cc, GNU gcc, g++,… –IDE: MS Visual C++, Dev

21

Arithmetic ( 四則運算 )• Use ( ) to make it clear.

x=a+b+c+d+e/5;最好表示為x=a+b+c+d+(e/5);

x=(a+b+c+d+e)/5;

5e

dcbax

5edcba

x

Page 22: A Quick Start of C Introduction. 2 Terminology Program 程式 Programming language 程式語言 Code 程式碼 Compiler 編譯器 –cc, GNU gcc, g++,… –IDE: MS Visual C++, Dev

22

Examples

• a = 3 * b + 5 ;

• a = 6 * x * x – 3 ;

• a = k * (-2 * x + 1) ;

• a = 4 + (y - 2) / 5 ;

• a = (p % m) + 2 ;

53 ba

36 2 xa

)12( xka

5)2(

4 y

a

2 mod mpa

Page 23: A Quick Start of C Introduction. 2 Terminology Program 程式 Programming language 程式語言 Code 程式碼 Compiler 編譯器 –cc, GNU gcc, g++,… –IDE: MS Visual C++, Dev

23

Practices

tky

xa 2

)5(

523 2 xxa

byaxz 3

2)( baxk

Page 24: A Quick Start of C Introduction. 2 Terminology Program 程式 Programming language 程式語言 Code 程式碼 Compiler 編譯器 –cc, GNU gcc, g++,… –IDE: MS Visual C++, Dev

24

The if-Selection Statement

• C codeif (score >= 60){printf(" 恭喜及格了 !\n");

}

score >=60

print " 恭喜 "

yes

no

Page 25: A Quick Start of C Introduction. 2 Terminology Program 程式 Programming language 程式語言 Code 程式碼 Compiler 編譯器 –cc, GNU gcc, g++,… –IDE: MS Visual C++, Dev

25

if…else… Statement

• Ex.if (score >= 60){ printf(" 恭喜及格了 !\n");

}else{ printf(" 很抱歉,你被當了。 \n"); printf(" 那麼,明年見了! \n");

}

>=60

" 及格 "

yes

" 被當 "

no

// score < 60 的情形

Page 26: A Quick Start of C Introduction. 2 Terminology Program 程式 Programming language 程式語言 Code 程式碼 Compiler 編譯器 –cc, GNU gcc, g++,… –IDE: MS Visual C++, Dev

26

Conditional Operators

• Equality Operators == 代表 = != 代表

• Relational Operators > 代表 > < 代表 < >= 代表 <= 代表

Page 27: A Quick Start of C Introduction. 2 Terminology Program 程式 Programming language 程式語言 Code 程式碼 Compiler 編譯器 –cc, GNU gcc, g++,… –IDE: MS Visual C++, Dev

27

Conditions

• score is 100score == 100

• score is not 100score != 100

• score is larger than 60score > 60

• score is larger than or equal to 60score >= 60

Page 28: A Quick Start of C Introduction. 2 Terminology Program 程式 Programming language 程式語言 Code 程式碼 Compiler 編譯器 –cc, GNU gcc, g++,… –IDE: MS Visual C++, Dev

28

Operator meaning example && and (i>0) && (i<10)|| or (i<0) || (i>10)! not !(i>0)

• &&: true iff both conditions are true• ||: true as long as one condition is true• !: true if the inner condition is false

Logical Operators

Page 29: A Quick Start of C Introduction. 2 Terminology Program 程式 Programming language 程式語言 Code 程式碼 Compiler 編譯器 –cc, GNU gcc, g++,… –IDE: MS Visual C++, Dev

29

Truth Tables&& expression1 expression2 expression1 && expression2

t r ue t r ue t r uet r ue f al se f al sef al se t r ue f al sef al se f al se f al se

| | expression1 expression2 expression1 | | expression2

t r ue t r ue t r uet r ue f al se t r uef al se t r ue t r uef al se f al se f al se

! ! expression1

f al set r ue

expression1

t r uef al se

Page 30: A Quick Start of C Introduction. 2 Terminology Program 程式 Programming language 程式語言 Code 程式碼 Compiler 編譯器 –cc, GNU gcc, g++,… –IDE: MS Visual C++, Dev

30

Condition Examples

• i is not equal to 0 and j mod i > 4(i != 0) && (j % i > 4)

• a > 6 or b > 4(a > 6) || (b > 4)

• 0 a 100(a >= 0) && (a <= 100)

• a is not between 0 and 100!( (a >= 0) && (a <= 100) )Or (a < 0) || (a > 100)

Page 31: A Quick Start of C Introduction. 2 Terminology Program 程式 Programming language 程式語言 Code 程式碼 Compiler 編譯器 –cc, GNU gcc, g++,… –IDE: MS Visual C++, Dev

31

Condition Examples

• a is not a multiple of 3!(a%3==0)Or (a%3)!=0

• a is 1 or 2(a==1) || (a==2)

• a is not 1 or 2!( (a==1) || (a==2) )Or (a!=1) && (a!=2)

(a is neither 1 nor 2)

Page 32: A Quick Start of C Introduction. 2 Terminology Program 程式 Programming language 程式語言 Code 程式碼 Compiler 編譯器 –cc, GNU gcc, g++,… –IDE: MS Visual C++, Dev

32

練習• 看分數給等級

– 優等: 90~100 ,優良學生 (bestStudent)– 甲等: 80~89– 乙等: 70~79– 丙等: 60~69– 戊等: 59~ ,補救學生 (toBeTutored)

Page 33: A Quick Start of C Introduction. 2 Terminology Program 程式 Programming language 程式語言 Code 程式碼 Compiler 編譯器 –cc, GNU gcc, g++,… –IDE: MS Visual C++, Dev

33

for (initial; condition; update){ actions if in condition;}

The for Statement

• Syntax:

• Example: Print out 1 to 6for (i=1; i<=6; i++){ printf("%d ",i);}

1 2 3 4 5 6 _

condition statementtrue

false

Initial

update

Page 34: A Quick Start of C Introduction. 2 Terminology Program 程式 Programming language 程式語言 Code 程式碼 Compiler 編譯器 –cc, GNU gcc, g++,… –IDE: MS Visual C++, Dev

34

The for Statement

int i;for (i=1; i<=6; i++){ printf("%d ",i);}

i@#*$1234567

_1 _1 2 _1 2 3 _1 2 3 4 _1 2 3 4 5 _1 2 3 4 5 6 _

Page 35: A Quick Start of C Introduction. 2 Terminology Program 程式 Programming language 程式語言 Code 程式碼 Compiler 編譯器 –cc, GNU gcc, g++,… –IDE: MS Visual C++, Dev

35

_$_$$_$$$_$$$$_$$$$$_$$$$$$_

Examples

• Print out 6 $'sint i;for (i = 0; i < 6; i++) { printf("$");}

i@#*$0123456

Page 36: A Quick Start of C Introduction. 2 Terminology Program 程式 Programming language 程式語言 Code 程式碼 Compiler 編譯器 –cc, GNU gcc, g++,… –IDE: MS Visual C++, Dev

36

for Statement Idioms

• The for statement is usually the best choice for loops counting up or down:

Counting up from 0 to n–1:for (i = 0; i < n; i++) …

Counting up from 1 to n:for (i = 1; i <= n; i++) …

Counting down from a to b:for (i = a; i >= b; i--) …

Repeat n times:for (i = 0; i < n; i++) …

Page 37: A Quick Start of C Introduction. 2 Terminology Program 程式 Programming language 程式語言 Code 程式碼 Compiler 編譯器 –cc, GNU gcc, g++,… –IDE: MS Visual C++, Dev

37

Examples

• Print out odd numbers between 1 and 10.

• Print out 1 to 20 with an interval 3.

• Print out 10 to 1.

• Print out 6 $'s in a line. (See next slide.)