大同大學科學開源服務社
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Owners
        • Signed-in users
        • Everyone
        Owners Signed-in users Everyone
      • Write
        • Owners
        • Signed-in users
        • Everyone
        Owners Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Versions and GitHub Sync Note Insights Sharing URL Help
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Owners
  • Owners
  • Signed-in users
  • Everyone
Owners Signed-in users Everyone
Write
Owners
  • Owners
  • Signed-in users
  • Everyone
Owners Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       owned this note    owned this note      
    Published Linked with GitHub
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    --- 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; } ```

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully