A Quick Start of C Introduction
2 Terminology Program 程式 Programming language 程式語言 Code 程式碼 Compiler 編譯器 –cc, GNU gcc, g++,… –IDE: MS Visual C++, Dev C++, Turbo C,…
3 Terminology Variable 變數 Statement 敍述 –Assignment 給值 –Selection 條件判斷 –Iteration (loop) 迴圈 Array 陣列 Function 函式 Pointer 指標
4 Writing a Simple Program #include int main() { printf("Hello World!\n"); return 0; } directives statementsfunction call
5 Printing a Pun #include 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.
6 2.4 Variables ( 變數 ) Type A variable of type int can store an integer, e.g. 1392, 0, A variable of type float can store a real number (stored in the floating-point fashion), e.g , , 0, NOTE: float is just an approximation of the number 0.1 would become
7 Variable Declaration ( 宣告變數 ) Variables must be declared. Declaration: varType varName; Ex: int score; score: name of the variable int declares that score is an integer
8 Variable Declaration A variable must be declared before using it. Ex: int score; score = 95; ( ) Ex: score = 95; int score; ( )
9 Variable Declaration (Cont.) Declare two or more variables int price; int tip; int total; int price, tip, total; Case sensitive –a1 and A1 are different.
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.6 CError 2.6 放結果的變數是在等號左邊,不能寫成 b + c = a 。
11 Print Out an Integer #include int main() { int score; score = 85; printf(" 成績是: %d\n", score); return 0; } 用 %d 來印出整數資料 逗號 要印出的變數
12 Print Out an Integer #include int main() { int price, tip, total; price = 500; tip = 50; total = price + tip; printf(" 總共要付: %d\n", total); return 0; }
13 Print Out an Integer #include int main() { int price, tip, total; price = 500; tip = 50; printf(" 總共要付: %d\n", price + tip); return 0; }
14 Print Out Many Integers #include 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; }
15 Print Out Real Numbers #include int main() { float radius = 2; float area; area = f * radius * radius; printf(" 半徑 %f,\n", radius); printf(" 面積 %.2f\n", area); return 0; }
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
Reading Input Syntax: scanf("%f", &varName); –Ex: scanf("%f", &radius); & A floating-point variable
Arithmetic Operators Unary operators –For positive/negative numbers Binary operators –For addition, subtraction, multiplication, division, and remainder (modular)
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);
20 Unary Arithmetic Operators + ( 正 ) – ( 負 ) Precedence: + = - > binary operators – 也就是說,正負號優先於加減乘除 – 所以, a = 3*-5; 表示 a = 3*(-5);
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;
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 ;
23 Practices
24 The if -Selection Statement C code if (score >= 60) { printf(" 恭喜及格了 !\n"); } score >=60 print " 恭喜 " yes no
25 if…else… Statement Ex. if (score >= 60) { printf(" 恭喜及格了 !\n"); } else { printf(" 很抱歉,你被當了。 \n"); printf(" 那麼,明年見了! \n"); } >=60 " 及格 " yes " 被當 " no // score < 60 的情形
26 Conditional Operators Equality Operators == 代表 = != 代表 Relational Operators > 代表 > < 代表 < >= 代表 <= 代表
27 Conditions score is 100 score == 100 score is not 100 score != 100 score is larger than 60 score > 60 score is larger than or equal to 60 score >= 60
28 Operatormeaningexample && and( i>0) && (i<10) || or( 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
29 Truth Tables
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 100)
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)
32 練習 看分數給等級 – 優等: 90~100 ,優良學生 (bestStudent) – 甲等: 80~89 – 乙等: 70~79 – 丙等: 60~69 – 戊等: 59~ ,補救學生 (toBeTutored)
33 for ( initial ; condition ; update ){ actions if in condition ; } The for Statement Syntax: Example: Print out 1 to 6 for (i=1; i<=6; i++){ printf("%d ",i); } _ condition statement true false Initial update
34 The for Statement int i; for (i=1; i<=6; i++){ printf("%d ",i); } * $ _1_1 2_1 2 3_ _ _ _
35 _$_$$_$$$_$$$$_$$$$$_$$$$$$_ Examples Print out 6 $'s int i; for (i = 0; i < 6; i++) { printf("$"); } * $
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++) …
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.)