# Get Start 前情提要

  • Program language

    • C
    • C++
    • Java
    • JavaScript
    • Python
  • Source-code Editor

    • VS code
    • dev c++
    • Code::Block
    • notepad++
  • Compiler

# Introduction 介紹

  • 雖然他是一個很古老的語言,也相較底層,但到如今還是一個相當熱門的語言。
  • 他是很多語言的基礎,學習 C 語言後,在學習其他語言,會更加容易上手。
  • C 語言 與 UNIX 密切相關,因為它是為編寫 UNIX 作業系統而開發的。
  • C 語言 相較於其他語言來說,較為快速。

# Base 基本概念

Rule

  • Left to Right, Up to Down
  • ; at the end of each line of instructions
  • return 0 ; end of program
  • Use TAB
  • {} content in structure

Comment

  • // … single line
  • /* … */ multi-line

# Syntax 語法

get start

#include <stdio.h>
int main(){
    ...
    return 0;
}
  • 引入函式庫

    Standard Library Header File 標準函式庫標頭檔

    #include <stdio.h> : 內容大略為處理輸入和輸出對象 scanf / printf

    給電腦字典 (標頭檔) 查詢程式內容意思

  • 主函式

    程式執行的主要內容,任何程式的切入點

    備註:學到 function 後會更加清楚

# Variable 變數

簡單來說,就是它的數值會改變的數

  • Type 型態
  • Label 標籤
  • Data 資料

# Declare

Data type  變數名稱  =  資料;

# Data Type

  • Primitive Data Types :
    int, float, char, bool, void

  • Derived Data Types :
    function, array, pointer, reference

  • Abstract or User-Defined Data Types :
    Class, Structure, Union, Enumeration, Typedef defined Datatype

# Primitive Data Type

These data types are built-in or predefined data types and can be used directly by the user to declare variables.

Data TypebyteRemark
Int 整數4 byterange from ≈ 10^9
Float 單精度浮點數4 byte
Double 雙經度浮點數8 byterange from -128 to 127 or 0 to 255
Char 字元1 byteTrue(1) or False(0)
Bool 布林1byteused for function which does not return a value
Void 無 / 空

1byte == 8bits

# Identifiers

  • Can contain letters, digits and underscores
  • Must begin with a letter or an underscore (_)
  • case sensitive (myVar and myvar are different variables)
  • Cannot contain whitespaces or special characters ( ! # % ……)
  • Reserved words cannot be used (like C keywords, such as int)
  • 名稱要有描述性且合理
  • 少用縮寫,對於沒有參與這個專案、但在相關領域的人熟悉的縮寫字,可以使用
  • 變數(包括函式的參數)的名稱一律小寫,單字之間用底線連接

命名規則

# Constants

常數

無法被更改的數值

c
const  Data type  變數名稱  =  資料;

# I/O

# Printf

輸出

將資料輸出,顯示在螢幕上

輸出變數

將變數輸出,顯示在螢幕上

輸出變數於特定格式

將變數輸出成特定格式,顯示在螢幕上

# Escape

  • \ 跳脫字元
  • \0 空字元 (NULL)
  • \t TAB
  • \n 換行
  • \a 響鈴 (BEL)
  • \” 雙引號
  • \’ 單引號

# Scanf

輸入

輸入資料,需要利用變數來放入輸入的資料

# Getchar/Putchar

字元輸入 / 輸出

如果輸入了兩個以上的字元,則 getchar 會取得第一個字元,並將第二個字元留在緩衝區中,直到再使用 getchar 或 scanf 取得輸入。

# Operator

  • Arithmetic Operator
  • Assignment Operator
  • Relational Operator / Comparison Operator
  • Logical Operator
  • Unary Operator

# Arithmetic Operators

  • + 加法
  • - 減法
  • * 乘法
  • / 商數除法
  • % 餘數除法

# Assignment Operators

  • = 指派
  • += 複合指派

# Relational Operators

  • > 大於
  • >= 大於等於
  • < 小於
  • <= 小於等於
  • == 等於
  • != 不等於

= VS. ==

  • = 指派: a = b 將 b 的值存入 a 變數中
  • == 等於: a == b a 與 b 是否相等?True/False

# Logical Operators

  • && AND 且

  • || OR 或

  • ! NOT 相反

# Unary Operators

++ / --

  • postfix-expression ++
  • ++ unary-expression

# Challenge

(B)

(A)

(A)

(B)

# Encoding

Encoding is the process of converting information from one format to another.

# ASCII

American Standard Code for Information Interchange

字元編碼(Character encoding)total 128

  • Control character 33
  • Printable characters 95

ASCII 維基百科

大寫轉小寫

  • 英文大寫 A
    十進位表示方式: 65
  • 英文小寫 a
    十進位表示方式: 97

# 9765=3297 – 65 = 32
# 英文大小寫 十進位表示方式差 32

# Control flow

  • Selection Statements 選擇語句
  • Iteration Statements 反覆語句
  • Jump Statements 跳轉語句

# Selection Statements

經由判斷條件是 TRUE 或是 FALSE,來選擇要執行哪段程式

  • If … else
  • Switch case

# Iteration Statements

重複執行單個或多個運算式,在每次執行前會先判斷條件式,若為 FALSE 或是 break,才會中斷。

  • While
  • Do while
  • For loop

# Jump Statements

  • Break 中斷
    中斷並結束迴圈。
  • Continue 繼續
    中斷當次迴圈循環,到開頭在執行一次。

# Selection Statements

# If … else

If _ Flow chart

If _ Syntax

If … else _ Flow chart

If … else _ Syntax

Shorthand if … else _ Syntax

If … else if … else _ Flow chart

If … else if … else _ Syntax

Nested if _ Flow chart

Nested if _ Syntax

# Switch

Switch _ Flow chart

Switch _ Syntax

# Thinking

請問右邊程式執行後會輸出什麼呢?

中止條件:

  • 一直執行到沒有 case 為止
  • 當遇到 break 時,強制跳離

# Iteration Statements

# While

While _ Flow chart

While _ Syntax

# Do while

Do while _ Flow chart

Do while _ Syntax

# For loop

For loop _ Flow chart

For loop_ Syntax

  • statement 1 : initialization
  • statement 2 : conditional
  • statement 3 : update

# Nested loop

巢狀是指東西如同鳥巢一樣,一層一層的包下去,而程式中的巢狀迴圈是, while/for 迴圈內包著 while/for 迴圈。

# Midway Challenge