馬卡巴卡
      • 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
    # scanf ## 1. scanf是???? `scanf` 是 `C` 語言中(C++也適用)用來從標準輸入(鍵盤之類的)讀取數據的函式。它允許程式接收使用者輸入的數值並存儲到變數中。 ## 2. 基本語法 ```c= #include <stdio.h> int main() { int number; printf("請輸入一個整數: "); scanf("%d", &number); printf("你輸入的數字是: %d\n", number); return 0; } ``` ### 說明: 1. `scanf("%d", &number);` 會從標準輸入讀取一個整數並存入 `number` 變數中。 2. `&number` 是取變數 `number` 的記憶體位址,讓 `scanf` 能夠將讀取的值存入該變數。 * 這部分因為攸關 `pointers` (指標),所以各位可能會不太理解,但是由於這裡是在介紹 `scanf` ,所以不多做贅述,那會在 `pointers` 做細部說明的。 ## 3. 常見格式化符號 格式符號 | 說明 | 例子 | |---------|----------|---------------------| | `%d` | 讀取整數 | `scanf("%d", &x);` | | `%f` | 讀取浮點數 | `scanf("%f", &y);` | | `%lf` | 讀取雙精度浮點數 | `scanf("%lf", &z);` | | `%c` | 讀取單一字元 | `scanf("%c", &ch);` | | `%s` | 讀取字串 | `scanf("%s", str);` | ## 4. 讀取多個輸入 `scanf` 可以一次讀取多個變數,例如: ```c #include <stdio.h> int main() { int a, b; printf("請輸入兩個整數: "); scanf("%d %d", &a, &b); printf("你輸入的數字是: %d 和 %d\n", a, b); return 0; } ``` ## 5. 讀取字串時的注意事項 使用 `%s` 讀取字串時,`scanf` 預設會以空白(空格、換行、Tab)作為分隔符,因此無法直接讀取含空格的字串。 ```c #include <stdio.h> int main() { char name[50]; printf("請輸入你的名字: "); scanf("%s", name); printf("你的名字是: %s\n", name); return 0; } ``` > **注意**:如果輸入 "high school",`scanf("%s", name);` 只會讀取 "high"。 解決方案:使用 `fgets` 或 `scanf(" %[^\n]", str);` ```c #include <stdio.h> int main() { char name[50]; printf("請輸入你的全名: "); fgets(name, 50, stdin); //意思是至多可以讀取50個字元 printf("你的名字是: %s", name); return 0; } ``` ## 6. 避免輸入錯誤 當 `scanf` 讀取的格式與輸入數據不符時,可能會導致錯誤輸入。例如: ```c #include <stdio.h> int main() { int num; printf("請輸入一個整數: "); if (scanf("%d", &num) != 1) { printf("輸入錯誤!請輸入有效的整數。\n"); } else { printf("你輸入的數字是: %d\n", num); } return 0; } ``` ## 7. 進階應用 ### 讀取換行字元 當 `scanf` 讀取數值後,換行 `\n` ('\n'的細則可至講義跳脫字元) 仍然留在輸入緩衝區,可能影響下一次的輸入: ```c #include <stdio.h> int main() { int age; char name[50]; printf("請輸入年齡: "); scanf("%d", &age); getchar(); // 清除輸入緩衝區的換行字元 printf("請輸入名字: "); fgets(name, 50, stdin); printf("年齡: %d, 名字: %s", age, name); return 0; } ``` ## 8. 結論 `scanf` 是 C 語言中重要的輸入函式,但使用時需要注意: - **變數的地址**(使用 `&` reference) - **格式化符號的正確使用** 不過不過 `scanf` 和 `printf`,我並不常用,通常只有在輸出小數點後幾位等等的情況才會使用。 eg. ```cpp= #include <stdio.h> int main(){ float a; scanf("%f", &a); printf("哈哈 %.4f", a); } ``` 輸入: `3.1415926` 輸出:**四捨五入** ```markmap 哈哈 3.1416 ``` ## 為什麼C語言的東東可以在C++使用?!?! 因為C++ 是在 C 語言的基礎上發展而來的,它**保留了 C 語言的大部分語法和特性**,並在此基礎上添加了許多新的特性,例如物件導向程式設計、模板等等的。

    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