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教程33.Arduino micros()函数

    micros()函数返回Arduino板开始运行当前程序时的微秒数。该数字在大约70分钟后溢出,即回到零。在16 MHz Arduino板(例如Duemilanove和Nano)上,此函数的分辨率为4微秒(即返回值总是4的倍数)。在8 MHz Arduino板(例如LilyPad)上,此函数的分辨率为8微秒。 micros()函数语法 micros () ;…

    2017年3月29日
    30500
  • Arduino函数库03.Arduino 字符函数

    新手入门是每个应用程序最重要的元素之一。很多时候,它的难易程度决定了用户是否会继续使用应用程序。在用户界面上,新手入门不仅仅是简单的5个介绍软件的页面或说明性的工具提示内容。下面我会使用最有趣的例子来描述新手入门过程的设计原则。

    2017年4月2日
    45701
  • Arduino传感器02.Arduino 温度传感器

    温度传感器LM35系列是精密集成电路温度器件,输出电压与摄氏温度成线性比例。 LM35器件优于以开尔文校准的线性温度传感器,因为用户不需要从输出中减去大的恒定电压以获得便利的摄氏缩放。LM35器件不需要任何外部校准或调整,即可在室温下提供±1/4°C的典型精度,在-55°C至150°C的温度范围内提供±3°C的典型精度。 技术规格 必需的组件 你将需要以下组…

    2017年4月21日
    43500
  • Arduino电机控制02.Arduino 伺服电机

    伺服电机是一种有输出轴的小型设备。通过向伺服发送编码信号,可以将该轴定位到特定的角度位置。只要编码信号存在于输入线上,伺服将保持轴的角位置。如果编码信号改变,则轴的角位置改变。实际上,伺服用于无线电控制的飞机中来定位控制面,如升降舵和方向舵。它们还用于无线电控制的汽车,木偶,当然还有机器人。 伺服在机器人中非常有用。电机体积小,内置控制电路,相对于它们尺寸来…

    2017年4月27日
    74000
  • Arduino教程24.Arduino 条件运算符? :

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

    2017年3月24日
    32000

发表回复

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

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

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

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