Arduino 數(shù)據(jù)類型

2022-03-01 09:28 更新

C中的數(shù)據(jù)類型是指用于聲明不同類型的變量或函數(shù)的擴(kuò)展系統(tǒng)。變量的類型確定它在存儲(chǔ)器中占用多少空間以及如何解釋存儲(chǔ)的位模式。

下表提供了你將在Arduino編程期間使用的所有數(shù)據(jù)類型。

voidBooleancharUnsigned char
byteintUnsigned int
word
longUnsigned long
shortfloatdoublearrayString-char array
String-object


void

void關(guān)鍵字僅用于函數(shù)聲明。它表示該函數(shù)預(yù)計(jì)不會(huì)向調(diào)用它的函數(shù)返回任何信息。

例子

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

Boolean

布爾值保存兩個(gè)值之一,true或false。每個(gè)布爾變量占用一個(gè)字節(jié)的內(nèi)存。

例子

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

Char

一種數(shù)據(jù)類型,占用一個(gè)字節(jié)的內(nèi)存,存儲(chǔ)一個(gè)字符值。字符文字用單引號寫成:'A',對于多個(gè)字符,字符串使用雙引號:"ABC"。

但是,字符是存儲(chǔ)為數(shù)字。你可以在ASCII圖表中查看特定編碼。這意味著可以對使用ASCII值的字符進(jìn)行算術(shù)運(yùn)算。例如,'A'+1的值為66,因?yàn)榇髮懽帜窤的ASCII值為65。

例子

Char chr_a = ‘a(chǎn)’ ;//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

unsigned char

unsigned char是一種無符號數(shù)據(jù)類型,占用一個(gè)字節(jié)的內(nèi)存。unsigned char數(shù)據(jù)類型編碼數(shù)字為0到255。

例子

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

byte

一個(gè)字節(jié)存儲(chǔ)一個(gè)8位無符號數(shù),從0到255。

例子

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

int

整數(shù)(int)是數(shù)字存儲(chǔ)的主要數(shù)據(jù)類型。int存儲(chǔ)16位(2字節(jié))值。這產(chǎn)生-32768至32767的范圍(最小值為-2^15,最大值為(2^15)-1)。

int的大小因板而異。例如,在Arduino Due中,int存儲(chǔ)32位(4字節(jié))值。這產(chǎn)生-2147483648至2147483647的范圍(最小值-2^31和最大值(2^31)-1)。

例子

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

Unsigned int

unsigned int(無符號整數(shù))與int相同,存儲(chǔ)2字節(jié)。然而,它們只存儲(chǔ)正值,產(chǎn)生0到65535(2^16)-1的有效范圍。Due存儲(chǔ)4字節(jié)(32位)值,范圍從0到4294967295(2^32-1)。

例子

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

Word

在Uno和其他基于ATMEGA的板上,一個(gè)word存儲(chǔ)一個(gè)16位無符號數(shù)。在Due和Zero上,它存儲(chǔ)一個(gè)32位無符號數(shù)。

例子

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

Long

Long變量是用于數(shù)字存儲(chǔ)的擴(kuò)展大小變量,存儲(chǔ)32位(4字節(jié)),從-2147483648到2147483647。

例子

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

unsigned long

unsigned long變量是用于數(shù)字存儲(chǔ)的擴(kuò)展大小變量,并存儲(chǔ)32位(4字節(jié))。與標(biāo)準(zhǔn)的long不同,unsigned long不會(huì)存儲(chǔ)負(fù)數(shù),它們的范圍為0到4294967295(2^32-1)。

例子

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

short

short是16位數(shù)據(jù)類型。在所有Arduinos(基于ATMega和ARM)上,一個(gè)short存儲(chǔ)一個(gè)16位(2字節(jié))值。這產(chǎn)生-32768至32767的范圍(最小值為-2^15,最大值為(2^15)-1)。

例子

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

float

浮點(diǎn)數(shù)的數(shù)據(jù)類型是具有小數(shù)點(diǎn)的數(shù)字。浮點(diǎn)數(shù)通常用于近似模擬值和連續(xù)值,因?yàn)樗鼈兊姆直媛矢哂谡麛?shù)。

浮點(diǎn)數(shù)可以大到3.4028235E+38,也可以低到-3.4028235E+38。它們被存儲(chǔ)為32位(4字節(jié))信息。

例子

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

double

在Uno和其他基于ATMEGA的板上,雙精度浮點(diǎn)數(shù)占用四個(gè)字節(jié)。也就是說,double實(shí)現(xiàn)與float完全相同,精度沒有增益。在Arduino Due上,double具有8字節(jié)(64位)精度。

例子

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


以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號