Arduino - 資料型別



C 語言中的資料型別指的是一個廣泛的系統,用於宣告不同型別的變數或函式。變數的型別決定了它在儲存器中佔據的空間大小,以及如何解釋儲存的位模式。

下表提供了您在 Arduino 程式設計過程中將使用到的所有資料型別。

void 布林型 字元型 無符號字元型 位元組型 整型 無符號整型 字型
長整型 無符號長整型 短整型 浮點型 雙精度浮點型 陣列 字串-字元陣列 字串-物件

void

void 關鍵字僅用於函式宣告。它表示該函式不期望從其被呼叫的函式返回任何資訊。

示例

Void Loop ( ) {
   // rest of the code
}

布林型

布林型變數儲存兩個值之一,真或假。每個布林型變數佔用一個位元組的記憶體。

示例

boolean val = false ; // declaration of variable with type boolean and initialize it with false
boolean state = true ; // declaration of variable with type boolean and initialize it with true

字元型

一種佔用一個位元組記憶體的資料型別,用於儲存字元值。字元字面量用單引號括起來,例如:'A',而對於多個字元,字串使用雙引號: "ABC"。

但是,字元儲存為數字。您可以在ASCII 表中檢視具體的編碼。這意味著可以在字元上執行算術運算,其中使用字元的 ASCII 值。例如,'A' + 1 的值為 66,因為大寫字母 A 的 ASCII 值為 65。

示例

Char chr_a = ‘a’ ;//declaration of variable with type char and initialize it with character a
Char chr_c = 97 ;//declaration of variable with type char and initialize it with character 97

ASCII Char Table

無符號字元型

無符號字元型是一種無符號資料型別,佔用一個位元組的記憶體。無符號字元型資料型別編碼範圍從 0 到 255 的數字。

示例

Unsigned Char chr_y = 121 ; // declaration of variable with type Unsigned char and initialize it with character y

位元組型

位元組型儲存一個 8 位無符號數,範圍從 0 到 255。

示例

byte m = 25 ;//declaration of variable with type byte and initialize it with 25

整型

整數是數字儲存的主要資料型別。int 儲存一個 16 位(2 位元組)的值。這產生了 -32,768 到 32,767 的範圍(最小值為 -2^15,最大值為 (2^15) - 1)。

int 的大小因開發板而異。例如,在 Arduino Due 上,int 儲存一個 32 位(4 位元組)的值。這產生了 -2,147,483,648 到 2,147,483,647 的範圍(最小值為 -2^31,最大值為 (2^31) - 1)。

示例

int counter = 32 ;// declaration of variable with type int and initialize it with 32

無符號整型

無符號整型(無符號整數)在儲存 2 位元組值的方式上與整型相同。但是,它們不儲存負數,而是僅儲存正值,產生 0 到 65,535(2^16) - 1 的有用範圍。Due 儲存一個 4 位元組(32 位)的值,範圍從 0 到 4,294,967,295(2^32 - 1)。

示例

Unsigned int counter = 60 ; // declaration of variable with 
   type unsigned int and initialize it with 60

字型

在 Uno 和其他基於 ATMEGA 的開發板上,字型儲存一個 16 位無符號數。在 Due 和 Zero 上,它儲存一個 32 位無符號數。

示例

word w = 1000 ;//declaration of variable with type word and initialize it with 1000

長整型

長整型變數是用於數字儲存的擴充套件大小變數,儲存 32 位(4 位元組),範圍從 -2,147,483,648 到 2,147,483,647。

示例

Long velocity = 102346 ;//declaration of variable with type Long and initialize it with 102346

無符號長整型

無符號長整型變數是用於數字儲存的擴充套件大小變數,儲存 32 位(4 位元組)。與標準長整型不同,無符號長整型不儲存負數,使其範圍從 0 到 4,294,967,295(2^32 - 1)。

示例

Unsigned Long velocity = 101006 ;// declaration of variable with 
   type Unsigned Long and initialize it with 101006

短整型

短整型是一種 16 位資料型別。在所有 Arduino(基於 ATMega 和 ARM 的)上,短整型儲存一個 16 位(2 位元組)的值。這產生了 -32,768 到 32,767 的範圍(最小值為 -2^15,最大值為 (2^15) - 1)。

示例

short val = 13 ;//declaration of variable with type short and initialize it with 13

浮點型

浮點型資料型別是一個帶有小數點的數字。浮點數通常用於近似模擬和連續值,因為它們比整數具有更高的解析度。

浮點數可以高達 3.4028235E+38,也可以低至 -3.4028235E+38。它們儲存為 32 位(4 位元組)的資訊。

示例

float num = 1.352;//declaration of variable with type float and initialize it with 1.352

雙精度浮點型

在 Uno 和其他基於 ATMEGA 的開發板上,雙精度浮點數佔用四個位元組。也就是說,雙精度浮點數的實現與單精度浮點數完全相同,精度沒有提高。在 Arduino Due 上,雙精度浮點數具有 8 位元組(64 位)的精度。

示例

double num = 45.352 ;// declaration of variable with type double and initialize it with 45.352
廣告