目录

Arduino - 数据类型

C中的数据类型是指用于声明不同类型的变量或函数的扩展系统。 变量的类型决定了它在存储中占用的空间大小以及如何解释存储的位模式。

下表提供了在Arduino编程期间将使用的所有数据类型。

voidBooleancharUnsigned charbyteintUnsigned intword
longUnsigned longshortfloatdoublearrayString-char arrayString-object

void

void关键字仅在函数声明中使用。 它表示该函数不会向调用它的函数返回任何信息。

例子 (Example)

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

Boolean

布尔值包含两个值之一,true或false。 每个布尔变量占用一个字节的内存。

例子 (Example)

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

占用存储字符值的一个字节内存的数据类型。 字符文字用单引号写成:'A'和多个字符,字符串使用双引号:“ABC”。

但是,字符存储为数字。 您可以在ASCII图表中看到特定的编码。 这意味着可以对字符进行算术运算,其中使用字符的ASCII值。 例如,'A'+ 1的值为66,因为大写字母A的ASCII值为65。

例子 (Example)

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字符表

无符号的字符

Unsigned char是一种占用一个字节内存的无符号数据类型。 unsigned char数据类型对0到255之间的数字进行编码。

例子 (Example)

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

byte

一个字节存储一个8位无符号数,从0到255。

例子 (Example)

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

int

整数是数字存储的主要数据类型。 int存储一个16位(2字节)的值。 这产生-32,768到32,767的范围(最小值-2 ^ 15和最大值(2 ^ 15)-1)。

每个板块的大小因板而异。 例如,在Arduino Due上, int存储一个32位(4字节)的值。 这产生-2,147,483,648到2,147,483,647的范围(最小值-2 ^ 31和最大值(2 ^ 31)-1)。

例子 (Example)

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

无符号整数

无符号整数(无符号整数)与存储2字节值的方式与int相同。 但是,它们不存储负数,而只存储正值,产生0到65,535(2 ^ 16)-1的有用范围。 Due存储一个4字节(32位)值,范围从0到4,294,967,295(2 ^ 32 - 1)。

例子 (Example)

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

Word

在Uno和其他基于ATMEGA的板上,一个字存储一个16位无符号数。 在Due和Zero上,它存储一个32位无符号数。

例子 (Example)

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

Long

长变量是数字存储的扩展大小变量,存储32位(4字节),从-2,147,483,648到2,147,483,647。

例子 (Example)

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

无符号长

无符号长变量是数字存储的扩展大小变量,存储32位(4字节)。 与标准长整数不同,无符号长整数不会存储负数,使其范围从0到4,294,967,295(2 ^ 32 - 1)。

例子 (Example)

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

short

short是16位数据类型。 在所有Arduinos(基于ATMega和ARM)上,short存储一个16位(2字节)值。 这产生-32,768到32,767的范围(最小值-2 ^ 15和最大值(2 ^ 15)-1)。

例子 (Example)

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

float

浮点数的数据类型是带小数点的数字。 浮点数通常用于近似模拟和连续值,因为它们具有比整数更高的分辨率。

浮点数可以大到3.4028235E + 38并且低至-3.4028235E + 38。 它们存储为32位(4字节)信息。

例子 (Example)

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

double

在Uno和其他基于ATMEGA的板上,双精度浮点数占用四个字节。 也就是说,double实现与float完全相同,没有精度增益。 在Arduino Due上,双精度具有8字节(64位)精度。

例子 (Example)

double num = 45.352 ;// declaration of variable with type double and initialize it with 45.352
↑回到顶部↑
WIKI教程 @2018