int

描述

整数型别是储存一个数字的主要资料型别。

在 86Duino 上一个 int 能储存 32-bit (4-byte) 的数值,范围在 -2,147,483,648 到 2,147,483,647 之间 (最小值是 -(2^31) 而最大值是 (2^31)-1)。

int 储存负整数是以二的补数计算方法来运算,最高位元称为 ”sign”,用来表示是负数与否,1 即是负整数,0 则是正整数。

在 86Duino 上,你可以放心的在运算式中使用负数;但请注意:在处理 位元右移运算 时,有可能会出现不可预期的副作用。

范例


int ledPin = 13;

语法


int var = val;

var 整数变数名称
val 指派数值给变数

提醒

当变数超过他们可表示的最大范围时,会返回最小后再加上去,反之亦是如此。例如一个 32-bit 的整数:

   int x;
   x = -2147483648;
   x = x - 1;       // x 现在是 2,147,483,647 (因为范围过头而返回到正数)

   x = 2147483647;
   x = x + 1;       // x 现在是 -2,147,483,648 (因为范围过头而返回到负数)

See also

byte
unsigned int
long
unsigned long
Integer Constants
Variable Declaration


语法参考主页面

本页由热血青年 LBU 译自英文版。

The text of the 86Duino reference is a modification of the Arduino reference, and is licensed under a Creative Commons Attribution-ShareAlike 3.0 License. Code samples in the reference are released into the public domain.