--- title: 社課共筆 - Re:從 C 開始的大學生活(上) description: 從介紹Visual Studio Code、Dev-C++、CodeBlocks 等軟體,建立自己寫程式的開發環境,再進入到基礎的C語言,例如:輸出/入、變數宣告、資料型態、數學、邏輯運算、條件判斷,最後利用問題練習來複習本次課堂的內容。 tags: - 社課 - 共筆 - 113 學年 --- :::success # Re:從 C 開始的大學生活(上) **時間:** 2024/10/07(星期一)18:00~20:00 **地點:** 挺生大樓 A3-200 教室 **簡報:** [連結](https://www.canva.com/design/DAGR16_YIK4/WqCVe5nntFJTll0XBX8O0Q/view?utm_content=DAGR16_YIK4&utm_campaign=designshare&utm_medium=link&utm_source=editor) **Slido:** [連結](https://app.sli.do/event/4TW296oA3oAEFayFQBJsK1/live/questions) ::: 參考檔案: [C reference](https://en.cppreference.com/w/c) ## 環境建置 參考檔案: [mingw下載](https://sourceforge.net/projects/mingw-w64/files/mingw-w64/mingw-w64-release/) [GCC使用教學](https://reurl.cc/93DG5V) #### 比較常見的開發環境 - Visual Studio Code + gcc - Visual C++ - Dev-C++ - CodeBlocks - ... 當然, 線上編譯器在作為練習環境的時候也是一個好選擇。 [線上編譯器](https://www.onlinegdb.com/) ## 輸入\出 ```c= #include <stdio.h> // include headers int main() { // 開始區塊 // statement結尾要有; // 字串用" " printf("hello, ttussc!"); //輸出format string。 return 0; } // 結束區塊 ``` ## 變數宣告 ```c= #include <stdio.h> int main() { int id; //宣告變數, type為int, 名子為id (未賦值) scanf("%d", &id); // %d printf("hello, %d!", id); int i = 1; //宣告變數, type為int, 名子為i (賦值 1) return 0; } ``` #### 宣告變數時的規則 - 資料型態(必要) - 名稱(必要) - 賦值(optional) #### 變數命名規則 - 字首不能為數字0-9 - 只能用[A-Za-z0-9_]字元。 - 名子不能使用保留字 ## 資料型態 ### 常見資料型態 - 整數 - byte - short - int - long long - 浮點數 - float (單精度) - double (雙精度) - 字元 - char - 特殊 - void (空) - 有號(包含負數) - signed(default) - 無號(非負數) - unsigned ### 取得資料型態佔用內存的大小 呼叫 ```sizeof(type)```, return ```size_t``` ex: ```c int intSize = sizeof(int); ``` ### 常見資料型態列表(大小/範圍) | 資料型態 | 大小(bytes) | 範圍 | |:------------------:|:----------:|:--------------:| | short | 2 | -2^15 ~ 2^15-1 | <!-- -32,768 ~ 32,767 --> | unsigned short | 2 | 0 ~ 2^16-1 | | int | 4 | -2^31 ~ 2^31-1 | | long | 4 | -2^31 ~ 2^31-1 | | unsigned long | 4 | 0 ~ 2^32-1 | | long long | 8 | -2^63 ~ 2^63-1 | | unsigned long long | 8 | 0 ~ 2^64-1 | | unsigned char | 1 | 0 ~ 255 | | char | 1 | -128 ~ 127 | | float | 4 | X | | double | 8 | X | | long double | 16 | X | > 請大家利用sizeof填完這個表格的中間欄(or後兩欄) ### 不同資料型態/表示法在printf/scanf的寫法: - %d : decimal - %o : octal - %x : hexadecimal - %u : unsigned decimal - %c : char - %f : float/double :chestnut: ```c= #include <stdio.h> int main() { for (int i = 1; i < 10; i++) { for (int j = 1; j < 10; j++) { printf("%3d *%3d = %3d", i, j, i * j); } printf("\n"); } } ``` 小:chestnut: ```c= #include <stdio.h> int main() { float f = 3.1415926; printf("%.4f", f); } ``` > 輸出為: > 3.1416 ### &符號 取變數的地址 ```c= int i; scanf("%d", &i); //把i的地址作為引數傳給scanf function, //scanf會把該地址對應的值設為stdin用 "%d" format 讀到的value printf("%d", i); //輸出i的value ``` ## 數學運算 ```+ - * / %``` 加/減/乘/除/取餘數 :chestnut: ```c= #include <stdio.h> int main() { int a, b; printf("Pleases input the num1 num2:"); scanf("%d %d", &a, &b); printf("\n%d + %d = %d\n", a, b, a + b); printf("%d - %d = %d\n", a, b, a - b); printf("%d * %d = %d\n", a, b, a * b); printf("%d / %d = %d\n", a, b, a / b); } ``` ### C內建的math lib ```c #include <math.h> ``` 裡面有比較多工具能使用, 如```pow()```, ```sqrt()``` , ```abs()``` , ```exp()```, ```log10()```, ```round()```, ```ceil()```, ```floor()```... ### **NEW CHALLENGE!** :::info $\frac{-b \pm \sqrt{b^{2}-4ac}}{2a}$ LaTex:\frac{-b \pm \sqrt{b^{2}-4ac}}{2a} ::: [LaTeX符號指令大全](https://hackmd.io/@CynthiaChuang/Basic-LaTeX-Commands#%E7%9B%AE%E9%8C%84) ```c= /* 公式解.c **輸入三個數字解出他的兩根** 測資 1 3 -10 答案 2 -5 ※pow(底數,指數) */ //put your code below here ``` ### Bug 有兩種 - 語法錯誤->無法編譯 - 語意錯誤->可編譯/執行但不是你想的那樣 [語法錯誤、語意錯誤都幾?](https://www.instagram.com/reel/DARMANsvJ2T/?utm_source=ig_web_button_native_share) ## 邏輯運算 - `>` 大於、 `>=` 大於等於 - `<` 小於、`<=` 小於等於 - `==` 等於(很重要!) // 只有一個等號是賦值 - `||` 或 (其中一個成立, 結果就是 `true`) - `&&` 且 (兩個都要成立, 結果才會是 `true`) ## 條件判斷 ### if ```c if (condition) { // 如果condition = true, 會執行這裡。 } ``` ### if-else ```c if (condition) { // 如果condition = true, 會執行這裡。 } else { // 如果condition = false, 會執行這裡。 } ``` ### if-else-if > **else if後面不一定要有else** ```c if (condition) { // 如果condition = true, 會執行這裡。 } else if (condition2) { // 如果condition2 = true, 會執行這裡。 } ``` 前面有說過, statement只有一句的時候, 可以不用 ```{}``` 但是為了程式碼可讀性, 還是加括號比較好。 ### switch-case > **switch-case不一定比if-else好用, 有時候反而會降低程式可讀性** switch() 傳入一個參數, **類別必須是整數/列舉。** 裡面的程式預設不會執行, 當有遇到任一個case符合參數的值後(或是遇到default), 接下來遇到的程式都會執行, 直到break/結束為止。 所以, 如果要把switch-case當成if-else用, 每一個case, 進下一個case(或是結束) 前要break。 #### default (optional) 如果有default, 一定是放在所有case後面 :chestnut: ```c= #include<stdio.h> int main() { char operator; int a, b; printf("Enter +,-,*,/:"); scanf("%c", &operator); printf("input two numbers:"); scanf("%d %d", &a, &b); switch (operator) { case '+': printf("answer is equals to %d\n", a + b); break; case '-': printf("answer is equal to %d\n", a - b); break; case '*': printf("answer is equal to %d\n", a * b); break; case '/': printf("answer is equal to %d\n", a / b); break; default: printf("error!\n"); } } ``` ### 三元運算子 寫法: **兩種結果的Type要相同。** ```c conditon ? trueResult : falseResult ``` - 簡化if-else的賦值 ```c= int i; if (flag) { i = 1; } else { i = 2; } ``` 可以寫為: ```c= int i = flag ? 1 : 2; ``` - 簡化method call也可以, 畢竟method call也是有return value的。 ```c= int i; if (flag) { a(); } else { b(); } ``` 可以寫為: ```c= int i = flag ? a() : b(); // a 跟 b return Type皆為int ``` 或是 ```c= flag ? a() : b(); // 沒有把a(), b()的結果賦值給其他變數。 ``` 假設a() 跟 b() return Type是**void**, 這時候就不能賦值了, 對吧? ------- > **看起來很好用? 確實 可以常用, > 但是建議不要當作if-else if...else堆一起用, 像是:** ```c happy >= 1 ? printf("clap hands") : (happy < 1 && happy > 0) ? printf("I don't give a st") : printf("I don't give a fk"); ``` > **沒有人會想要這樣寫, 很難看懂。** > **如果硬要的話, 不要全部擠在同一行, 小括號在每個三元運算子都建議括一下。** ```c happy >= 1 ? printf("clap hands") //這裡是單一statement, 不用加; : ((happy < 1 && happy > 0) ? printf("I don't give a st") //這裡是單一statement, 不用加; : printf("I don't give a fk")); ``` ## BMI計算機 ```C= #include <stdio.h> #include <math.h> int main(){ float h, w; printf("請輸入身高(cm), 體重(kg)"); scanf("%f %f", &h, &w); float BMI = w/(pow((h/100),2)); printf("your BMI is: %.2f\n", BMI); if (BMI>24) printf("你屬於:過重"); else if (BMI < 24 && BMI > 18.5) printf("你屬於:正常"); else printf("你屬於:過輕"); return 0; } ```