📜  Arduino-数据类型

📅  最后修改于: 2020-11-05 03:25:56             🧑  作者: Mango


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

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

void Boolean char Unsigned char byte int Unsigned int word
long Unsigned long short float double array String-char array String-object

虚空

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

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

布尔型

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

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数据类型编码从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)。

整数大小因板而异。例如,在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位无符号数字。在到期和零处,它存储一个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字节)。与标准long不同,无符号long不会存储负数,其范围为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)上,short存储一个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的板上,双精度浮点数占用四个字节。也就是说,double实现与float完全相同,而没有精度提高。在Arduino Due上,双精度精度为8字节(64位)。

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