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教程28.Arduino 嵌套循环

    C语言允许你在另一个循环内使用一个循环。下面的例子说明了这个概念。 嵌套循环语句语法 for ( initialize ;control; increment or decrement) { // statement block for ( initialize ;control; increment or decrement) { // statement…

    2017年3月25日
    15500
  • Arduino函数库04.Arduino 数学库

    Arduino数学库(math.h)包含了许多用于操作浮点数的有用的数学函数。 库中的宏 以下是在标题math.h中定义的宏: 宏 值 描述 M_E 2.7182818284590452354 常数e。 M_LOG2E 1.4426950408889634074/* log_2 e */ e以2为底的对数。 M_1_PI 0.3183098861837906…

    2017年4月3日
    21501
  • Arduino教程20.Arduino If语句

    它采用括号中的表达式,后面跟随语句或语句块。如果表达式为真,则执行语句或语句块,否则跳过这些语句。 不同形式的if语句 形式1 if (expression) statement; 如果你有一个语句,你可以使用没有大括号{}的if语句。 形式2 if (expression) { Block of statements; } if语句 – 执行顺…

    2017年3月24日
    16800
  • Arduino教程34.Arduino 将数组传递给函数

    要将数组参数传递给函数,请指定没有任何括号的数组的名称。例如,如果数组 hourlyTemperatures 已声明为函数,则调用时会将数组 hourlyTemperatures 及其大小传递给函数 modifyArray 。 重点 下面是在将数组传递给函数时需要了解的一些重要事项: void modifyArray(…

    2017年3月30日
    21300
  • Arduino教程31.Arduino delayMicroseconds()函数

    delayMicroseconds()函数接受单个整数(或数字)参数。此数字表示时间,以微秒为单位。一毫秒内有一千微秒,一秒内有一百万微秒。 目前,可以产生精确延迟的最大值是16383。这可能会在未来的Arduino版本中改变。对于超过几千微秒的延迟,应该使用delay()函数。 delayMicroseconds()函数语法 delayMicrosecon…

    2017年3月29日
    16700

发表回复

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

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

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

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