共筆
      • 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 New
    • Engagement control
    • Make a copy
    • 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 Note Insights Versions and GitHub Sync Sharing URL Help
Menu
Options
Engagement control Make a copy 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
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    # Chapter2 ## 2.2 A Simple C Program:Printing a Line of Text ### comment(註解) 註解並不會進行編譯,它幫助我們了解程式內容(也能幫助我們debug) #### 1. 單行註解://... ```c++= //Fig. 2.1 : fig02_01.c ``` #### 2. multi-line comments(多行註解): /* ... */ ```c++= /* Fig. 2.1 : fig02_01.c A first program in c */ ``` ### Include Preprocessor Directive ```c++= #include<stdio.h> ``` - 使用#include 呼叫 <stdio.h> - 其中<stdio.h>為"stadard input/output library functions" - 而我們常用的printf也是一種"standard input/output library functions" ### The main function ```c++= int main(void) ``` - main 也是一種 function - 每個program開始於 main function,而每個program只會有一個main function `小蜜蜂2.1: 每個function 應該要在前面做註解來描述function的目的` ### An Output Statement ```c++= printf("Welcome to C!\n"); ``` 記得以semicolon(分號)來結束statement ### Escape Sequences |Escape Sequences | 意義 | | :-- | --: | |\n|換行| |\t|與使用Tab鍵功能相同| |\a|(alert)發出鳴叫| |\\\ |輸出反斜槓(\)| |\\"|輸出雙引號(")| 因為反斜槓( \\ )對於string有特殊意義,所以我們用" \\\ " replace" \\" `小蜜蜂2.2:在每個function以 " } "結束後,加上註解` ### linker library functions 並不存在於 C programming language 中。因此我們需要linker幫助連結library functions與object function,出現無法連結的話就是語法錯誤 ## 2.3 Another Simple C Program:Adding Two Integers ### Variable and Variable Definitions ```c++= int integer1; int integer2; ``` - 使用int來definite(宣告)變數 - 在第一次使用變數之前須definte #### variable: 存在於memory中用來儲存數值 `小蜜蜂2.2:不要同時使用大寫字串與小寫字串(ex. Main and main)` `小蜜蜂2.1:避免使用"_"來當identifies 的開頭` `小蜜蜂2.5:使用有意義的變數名稱,來減少需要的註解` `小蜜蜂2.6:使用小寫字母來當簡單變數的開頭` `小蜜蜂2.7:使用"_"分離變數名稱(ex. total_commissions)或以首字母大寫來分離(ex. totalCommissions)` ### The Scanf Function and Formatted Inputs ```c++= scanf("%d",&integer1); ``` #### %d : d for decimal integer(十進位整數) #### &integer1 - &是取址符號 , integer1是變數名稱 - &integer1是要取變數integer1的所在位址(指標的時候應該會講到) `小蜜蜂2.8:在 "," 後加個空白鍵,讓整個程式更容易閱讀` p.s. scanf()的回傳值會是正確讀入變數的個數,EOF=-1 ### Assignment Statement ```c++= sum = integer1 + integer2; ``` `小蜜蜂2.9:在 binary operator(二元運算子)兩側加上空白鍵,讓整個程式更容易閱讀` `小蜜蜂2.3:在等號的右邊做運算` ### Printing with a Format Control String ```c++= printf( "Sum is %d\n", sum ); ``` 將sum中的數值印出在%d處 `小蜜蜂2.4:在scanf functions中,如果漏打"&"將發生錯誤` `小蜜蜂2.5:在printf functions中,變數前不應該加"&"` ## 2.4 Memory Concepts - 兩個變數的記憶位置不一定相鄰 - 陣列的相鄰兩格元素記憶體位置會相鄰 ## 2.5 Arithmetic in C ### Arithmetic operator | Arithmetic operator |Algebraic expression | C expression | | :--: | :--: |:--:| | + | f+7 | f+7 | | - | p-c | p-c| | * | bm | b*m | | / | X/Y| X/Y| | % | r mod s | r%s | `小蜜蜂2.6:分母為0的除法運算通常會出現錯誤` ### Rules of Operator Precedence(運算子優先順序) |Order of evaluation |Operator(s)| |:--:|:--:| |first|()| |second|* / % | |third|+ - | |last | = | 其計算規則與四則運算規則相同 ## 2.6 Decision Making:Equality and Relation Operators ### Equality and Relational Operators(關係運算子) 這邊有if的簡介,但後面好像有,這邊我就不整理了 `小蜜蜂2.7:==、!=、<=、>= 等中間皆不可加空白鍵` `小蜜蜂2.8:"=="與"="不同。"=="為equality operator read “double equals。"=”為"assignment operator(賦值運算子)"read “gets” or “is assigned the value of.” ` | operators | Cequality or relational operator| |:--:|:--:| | > | > | | < | < | | ≥ | >= | | ≤ | <= | | = | == | | ≠ | != | `小蜜蜂2.10:Although it is allowed , there should be no more than one statement per line in a program`(p.87) `小蜜蜂2.9:在scanf中不需要以逗號來隔開兩個%d` `小蜜蜂2.10:在if中不需要在條件句後加分號(;)` `小蜜蜂2.11:statement 可能橫跨多行,我們需要適當了分段` `小蜜蜂2.12:如果你不確定運算子的優先順序,使用括號或將其分為多個簡單運算。另外需要注意這些運算子的associativity(ex. "=" associate from right to left)` ### Associativity |Operators|Associativity| |:--:|:--:| | () | 由左至右 | | * / % | 由左至右 | | + - | 由左至右 | |< 、<=、 >、 >= | 由左至右 | |==、 != |由左至右 | | = | ==由右至左== | - 不要使用特殊keyword (ex. int , if , void)當作變數名稱 (可以看編譯器有沒有變色判斷) ## 2.7 Secure C Programming ```c= printf("Welcome to C!\n"); puts("Welcome to C!"); ``` 在這兩行我們會得出相同的輸出結果 (puts會自動換行) ```c= printf("Welcome") printf("%s","Welcome") ``` - 在這兩行我們會得到相同的輸出結果 - 以下列出一些%後可加的字以及其意義 - %c:字元 - %d:十進位整數 - %f:浮點數(計算到小數第六位) - %s:字串 - %p:列印變數位址 - %o:八進位整數 - %x:十六進位整數 - %u:無符號十進位整數 - %hd:short int - %lf:double - %lld:long long int # Chapter 2 結束 ###### tags: `程設好難` `學校門口大樹石頭下` `他的課就很好睡阿` <style> .navbar-brand::after { content: " × 老葉的程式設計"; } </style>

    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