Arduino教程05.Arduino 数据类型

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

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

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

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

voidBooleancharUnsigned charbyteintUnsigned intword
longUnsigned longshortfloatdoublearrayString-char arrayString-object


void

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

例子

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

Boolean

布尔值保存两个值之一,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

Char

一种数据类型,占用一个字节的内存,存储一个字符值。字符文字用单引号写成:’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

unsigned char

unsigned char是一种无符号数据类型,占用一个字节的内存。unsigned char数据类型编码数字为0到255。

例子

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

byte

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

例子

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

int

整数(int)是数字存储的主要数据类型。int存储16位(2字节)值。这产生-32768至32767的范围(最小值为-2^15,最大值为(2^15)-1)。

int的大小因板而异。例如,在Arduino Due中,int存储32位(4字节)值。这产生-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(无符号整数)与int相同,存储2字节。然而,它们只存储正值,产生0到65535(2^16)-1的有效范围。Due存储4字节(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的板上,一个word存储一个16位无符号数。在Due和Zero上,它存储一个32位无符号数。

例子

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

Long

Long变量是用于数字存储的扩展大小变量,存储32位(4字节),从-2147483648到2147483647。

例子

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

unsigned long

unsigned long变量是用于数字存储的扩展大小变量,并存储32位(4字节)。与标准的long不同,unsigned long不会存储负数,它们的范围为0到4294967295(2^32-1)。

例子

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字节)值。这产生-32768至32767的范围(最小值为-2^15,最大值为(2^15)-1)。

例子

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

float

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

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

例子

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

double

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

例子

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

发布者:suiyublg,转转请注明出处:https://huibian.net/1005-2/

(36)
suiyublg的头像suiyublg
上一篇 2017年3月20日 14:35
下一篇 2017年3月22日 14:46

相关推荐

  • Arduino教程24.Arduino 条件运算符? :

    条件运算符 ? : 是C语言中唯一的三元运算符。 ? :条件运算符语法 expression1 ? expression2 : expression3 首先评估expression1。如果其值为true,那么将评估expression2,并忽略expression3。如果expression1评估为false,则将评估expression…

    2017年3月24日
    23200
  • Arduino教程27.Arduino for循环

    for循环执行语句预定的次数。循环的控制表达式在for循环括号内完全的初始化,测试和操作。它很容易调试循环结构的行为,因为它是独立于循环内的活动。 每个for循环最多有三个表达式决定其操作。以下示例显示了通用的for循环语法。请注意,在for循环参数括号中的三个表达式用分号分隔。 for循环语句语法 for ( initialize; control; in…

    2017年3月25日
    27500
  • Arduino教程32.Arduino millis()函数

    此函数用于返回Arduino板开始运行当前程序时的毫秒数。这个数字在大约50天后溢出,即回到零。 millis()函数语法 millis () ; 此函数从程序开始处返回毫秒。 例子 unsigned long time; void setup() { Serial.begin(9600); } void loop() { Serial.print(“Tim…

    2017年3月29日
    26500
  • Arduino进阶07.Arduino 串行外设接口

    串行外设接口(SPI)总线是用于串行通信的系统,最多可使用四个导体,通常为三个。一个导体用于数据接收,一个导体用于数据发送,一个导体用于同步,另一个导体用于选择与之通信的设备。它是一个全双工连接,这意味着数据是同时发送和接收的。最大波特率高于I2C通信系统中的波特率。

    2017年4月11日
    47466
  • Arduino项目07.Arduino 鼠标按钮控制

    使用鼠标库,你可以使用Arduino Leonardo,Micro或Due来控制计算机的屏幕光标。 这个特殊的例子使用五个按钮来移动屏幕上的光标。四个按钮是方向性的(上,下,左,右),一个是用于鼠标左键单击。来自Arduino的光标移动总是相对的。每次读取输入时,光标的位置都会相对于当前位置进行更新。 只要有一个方向按钮被按下,Arduino就会移动鼠标,在…

    2017年4月18日
    32200

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信
欢迎大家来到大雄学编程!