TNPLR
    • Create new note
    • Create a note from template
      • 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
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me 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
    • Save as template
    • 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 Create Help
Create Create new note Create a note from template
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
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me 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
    # C語言教學03 - 一點點C常識及資料型態 ## 撰寫於2018年9月 [現代C語言小心得](https://hackmd.io/s/HJPpJjvw7) ## 進位法 雖然說進位法不很難,但是沒有碰過電腦的人,應該不常使用。 ### 二進位 電腦最廣為人知的就是二進位,就只有1和0兩種數字就可以代表各種數。 ### 八進位 因為二進位太麻煩,所以利用三個二進位數可以直接換成一個八進位數的原理製成。這種進位法只會出現0到7的數字。 ### 十進位 這不用講了吧。十進位就是小學學、中學學、大學也在學的最常用進位法。 ### 十六進位 有人搞出十六進位。其實就是四個二進位數可以轉換成一個十六進位數,十六進位會出現所有0到9的數字,但還不夠用,所以又加上了A到F。 ### 簡易的範例 | 10 進位 | 2 進位 | 8 進位 | 16 進位 | | -------- | -------- | -------- | ---- | | 20 | 10100 | 24 | 14 | | 91 | 1011011 | 133 | 5B | ## 跳脫字元 C語言有許多跳脫字元,這些字元都是以反斜線 \ 為開頭。以下列出一些常用的: | 字元 | 代表意義 | 字元 | 代表意義 | | -------- | -------- | ------- | ------- | | \n | 換行 newline | \\' | 單引號 | | \r | 復位 carriage-return| \\\\ | 反斜線| | \b | 退格 backspace| \a | 響鈴 alert | | \t | 水平定位符號 horizontal-tab| \123 | 八進位的東西| | \\" | 雙引號 | \xFF | 十六進位的東西 | \\f |換頁 form-feed|\v|垂直定位 vertical-tab 我們可以試用一下: ```C= #include <stdio.h> int main() { printf("Hello\nworld!\"\'\\"); return 0; } ``` 執行結果 ``` Hello world!"'\ ``` ## 基礎型別 basic types ### 整數 integer C語言有五種「標準有號整數型別(standard signed integer type)」,分別是**signed char**、**short int**、**int**、**long int**、**long long int**。這些型別的差異是其佔用的記憶體大小,也代表著可以儲存數字的範圍。要如何宣告呢?利用以下方法: ```C= int a; int b = 10; int c, d; ``` 宣告 a 代表一個 int 型別,然後宣告 b 是一個 int 型別,並且初始值是10,再來同時宣告 c 和 d 皆是 int。記住陳述最後要有分號。 :::warning 沒有定義初始值,沒有人保證內容是什麼 ::: 宣告無號整數,可利用**unsigned**修飾字。 ```C= unsigned int p; unsigned long int q; unsigned short int r = 5; unsigned char s = 20; ``` ### 浮點數(實數) floating C語言有三種浮點實數型別(real floating type),分別是**float**、**double**、**long double**,這三種浮點數型別差異也是其佔用記憶體大小,並影響精確度以及儲存數字的範圍。 :::warning 浮點數不一定準確 ::: 要宣告浮點數,其實和整數差不多: ```C= double w; double x = 3.14; double y, z; ``` 宣告 w 代表一個 double 型別,然後宣告 x 是一個 double 型別,並且初始值是10,再來同時宣告 y 和 z 皆是 double。 :::info 所有浮點數型別皆是有號的 ::: ### 字元 character C語言有幾個存放字元的型別,分別是**char**、**signed char**、**unsigned char**。其中**char**有可能是**signed char**或是**unsigned char**,這要看編譯器還有電腦架構。要宣告字元,利用以下方式(其實和其他類型差不多): ```C= char h, i; char j = 10; char k = 'u'; char m = '\n'; ``` ### 複數 Complex 浮點數加上了\_**Complex**關鍵字就成為了複數型別。這個型別是較為不常用的。 ## 變數命名 ### 限制 可以使用非數字字元(nondigit),也就是底線以及拉丁文字大小寫: **\_abcdefghijklm nopqrstuvwxyz ABCDEFGHIJKLM NOPQRSTUVWXYZ** 可以使用數字,但是不能在開頭: **0123456789** 還有個限制,就是變數名稱不能是C語言關鍵字,44個關鍵字如下表: |關鍵字 |關鍵字 |關鍵字| |:------:|:------:|:-----:| |auto |if |unsigned |break |inline |void |case |int |volatile |chat |long |while |const |register|\_Alignof |continue|restrict|\_Alignas |default |return |\_Atomic |do |short |\_Bool |double |signed |\_Complex |else |sizeof |\_Generic |enum |static |\_Imaginary |extern |struct |\_Noreturn |float |switch |\_Static_assert |for |typedef |\_Thread_local |goto | union | 關鍵字的用途會在以後闡述。 ### 範例 以下是合法的命名 ```C= int abc; double _kkk; long int aaa_bbb; unsigned short int _123_345; ``` 以下是不合法的命名 ```C= int 2abc; // 數字不能在開頭 long int for; // 不能用關鍵字 ``` ## 字面常量 像是1、5.3之類的稱作字面常量,字面常量也有型別。例如1是一個int型別。下表提供一個簡單的參考 | 進位前綴 | 進位 | | :--------: | :--------: | | 0 | 八進位 | | 0x | 十六進位 | | 型別後綴 | 十進位時型別 | 八進位或十六進位時型別 | | :--------: | :--------: | :--------: | | U或u | unsigned int、unsigned long int、unsigned long long int|unsigned int、unsigned long int、unsigned long long int| |L或l |long int、long long int|long int、unsigned long int、long long int、unsigned long long int |以上兩種同時出現|unsigned long int、unsigned long long int|unsigned long int、unsigned long long int| |LL或ll|long long int|long long int、unsigned long long int| |LL或ll以及U或u同時出現|unsigned long long int|unsigned long long int|

    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