# Variable / Basic IO / 程式架構 <!-- ###### tags: `lion_1061` !--> ###### 講師: 曾韻如 --- ## 目錄 - 程式架構 - 格式化輸出 - 跳脫字元、轉換字元 - 格式化輸入 - 變數 - 算數運算子 - cast - 資料流導向 --- ## Program Structure 程式架構 - 標頭檔 (header file) - 主函式 (main function)(程式進入點--開始執行) - 程式內容 - 回傳值 (return value)(程式結束)  <!--程式由上而下執行,在C裡面,main函式就是他開始執行的地方--> ---- 執行結果  ---- ### 練習一 寫一個程式,印出"Hello World!" ---- <!--把echo刪掉 ### 標頭檔 (Header File) | 標頭檔 | 用途 | | ---- | ----- | | stdio.h | printf()、scanf() | | stdlib.h | rand() | | math.h | sin() | | time.h | time() | | ... | ... | --> ### Comment 註解  --- ## 常用變數種類 |類型名稱|其他名稱 | |:------:| :--------: | | int | (signed)integer 整數 | | char | character 字元 | | float | floating point 單精度浮點數 | | double | double precision floating point 雙精度浮點數| --- ## Formatted Output 格式化輸出 - #include<stdio.h> - 指令 : printf("格式",參數1,參數2...) - 換行要打 `\n`  ---- <!-- ## 跳脫字元 - \a 警告,用聲音或閃光提示使用者 - \b 退格,像是在打字時按左方向鍵 - \n 換行 - \r 返回,回到這一行的開頭 - \t 水平tab,類似鍵盤輸入Tab - \v 垂直tab,在下一行的相同位置輸入 ------> ## Format Specifiers 格式指定碼  [我要看更多~](http://edisonx.pixnet.net/blog/post/35305668-%5Bc%5D-printf-%E5%BC%95%E6%95%B8%E8%AA%AA%E6%98%8E) ---- ## Formatted Input 格式化輸入 - #include<stdio.h> - 指令 : scanf("格式",&參數1,&參數2...) - 記得要加&  --- ### Variable Declaration 變數宣告 - 變數(Variable) - 有名字的記憶體儲存空間 - 變數宣告 - 產生一個某型態的變數 - 語法 : `變數型態 變數名稱;` - ex : - int a; - float b; - char c; ---- ### Identifier 變數名稱 - 不可以數字開頭 ex: 9s,2b... - 不可以跟系統原有關鍵字(保留字 Reserved Word)重疊 - 不可以用中文 - 大小寫視為不同變數 ex: Lion ≠ lion - 不可以有空格(可以用底線取代) - 盡量使用有意義的單詞 <!--為什麼需要identifier!--> ---- ### Reserved Word 常用保留字  ---- ### Initialization 變數初始化 - 宣告變數後,直接給變數一個值 - 語法 : `type 變數名 = 值;` - ex : int lion = 99; - 變數沒有初始化會怎樣? Uninitialized ----  ----  ---- ### Assignment 變數賦值 - 直接給變數一個值 - 語法 : `變數名 = 值;` - ex : `lion = 99;`  ---- ```C /*Lion與lion有一樣的內容*/ char lion = 'A'; char Lion = 65; ``` <!--示範char.c--> ---- ### [ASCII](https://zh.wikipedia.org/wiki/ASCII)  --- ## Arithmetic Operator 簡單的算術運算子 - 加(`+`) - 減(`-`) - 乘(`*`) - 除 * 取商數(`/`) - 用在整數只會有整數部分 * 取餘數(`%`) - 只能用在整數 <!--示範mod.c!--> ---- ### 練習二 <!-- ```C float a, b; a=5.0, b=3.0; int c = a / b; printf("%d\n",c); ``` --> 試試用int、float、char等變數做四則運算~ <!-- 1.加減乘都很順利,可是int在除的時候卻有問題~ 2.不同型態之間不能運算、賦值 3.示範cast.c--> ---- ## Type Casting 強制轉型 - "暫時"將變數轉為另一個型態 - 語法 : `(新的變數型態) 變數名稱;` - ex : ```C int a,b; (float)a / (float)b; ``` --- ## 更多算術運算子 - Increase(`++`) -> a++ 或 ++a 相當於 a = a + 1 - Decrease(`--`) -> a-- 或 --a 相當於 a = a - 1 ---- ### Pre-increment/post-increment `++a`/`a++` 和其他運算子並用時 ```C int a, b; a = 1; b = 3; b = ++a; ``` ->先做 a = a + 1 ->再做 b = a ```C b = a++; ``` ->先把a的值保存起來 ->再做 a = a + 1 ->最後 b = a "原本" 的值 ----  ---- ### Assignment Operators 賦值運算子 `a = b` `a += b (a = a + b)` `a -= b (a = a - b)` `a *= b (a = a * b)` `a /= b (a = a / b)` `a %= b (a = a % b)` ---- ### 練習二 分別宣告一個int、float、char型態的變數並初始化 嘗試 先++ & 後++ 分別會得出什麼結果 範例Code:  --- ## Typesetting 排版  ---- ### 練習三 - 宣告一個int和float變數 - 用scanf將兩個變數賦值 - 分別除87看發生什麼事~ - 1.把結果排版好印出來 - 2.印出小數點後三位  --- ## I/O Redirection 資料流重導向  ---- ## 用途 - 輸入大量測資or需要重複輸入同測資 - 需要將螢幕輸出的資訊存下來時 ---- | |STDIN|STDOUT | |:-:|:---:|:-----:| |符號| < |> or >>| ---- ### STDIN < 檔案 - 用"檔案內容"代替"鍵盤輸入" ---- ### STDOUT > 檔案 - 檔案若不存在,系統會自動建立 - 檔案存在時,檔案會被覆蓋掉 ---- ### STDOUT >> 檔案 - 檔案若不存在,系統會自動建立 - 檔案存在時,會在檔案尾端開始寫入
×
Sign in
Email
Password
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