[常數]
說明
整數常量是直接在草圖中使用的數字,例如 123。默認情況下,這些數字被視為 int 但您可以使用 U 和 L 修飾符來更改它(見下文)。
通常,整數常量被視為基數為 10(十進製)的整數,但可以使用特殊符號(格式化程序)來輸入其他基數的數字。
根據 | 示例 | 格式化程序 | 評論 |
---|---|---|---|
10(十進製) |
123 |
none |
|
2(二進製) |
0b1111011 |
領先"0b" |
字符 0&1 有效 |
8(八進製) |
0173 |
領先"0" |
字符 0-7 有效 |
16(十六進製) |
0x7B |
領先"0x" |
字符 0-9、A-F、a-f 有效 |
十進製(以 10 為底)
這是您熟悉的common-sense 數學。假定沒有其他前綴的常量是十進製格式。
示例代碼:
n = 101; // same as 101 decimal ((1 * 10^2) + (0 * 10^1) + 1)
二進製(基數 2)
隻有字符 0 和 1 有效。
示例代碼:
n = 0b101; // same as 5 decimal ((1 * 2^2) + (0 * 2^1) + 1)
八進製(以 8 為基數)
隻有字符 0 到 7 是有效的。八進製值由前綴"0"(零)表示。
示例代碼:
n = 0101; // same as 65 decimal ((1 * 8^2) + (0 * 8^1) + 1)
通過(無意地)在常量前包含前導零並讓編譯器無意中將您的常量解釋為八進製,可能會生成難以發現的錯誤。
十六進製(以 16 為基數)
有效字符為 0 到 9 和字母 A 到 F; A 的值為 10,B 為 11,直到 F,即 15。十六進製值由前綴 "0x" 指示。請注意,A-F 可以是大寫 (A-F) 或小寫 (a-f)。
示例代碼:
n = 0x101; // same as 257 decimal ((1 * 16^2) + (0 * 16^1) + 1)
注意事項和警告
U & L 格式化程序:
默認情況下,整數常量被視為 int 並伴隨值限製。要指定具有其他數據類型的整數常量,請在其後跟上:
-
a 'u' 或 'U' 將常量強製為無符號數據格式。示例:33u
-
a 'l' 或 'L' 將常量強製為長數據格式。示例:100000L
-
a 'ul' 或 'UL' 將常量強製為無符號長常量。示例:32767ul
相關用法
- Arduino long用法及代碼示例
- Arduino Arduino_EMBRYO_2 - setLengthXY()用法及代碼示例
- Arduino ~用法及代碼示例
- Arduino ArduinoBLE - bleDevice.advertisedServiceUuidCount()用法及代碼示例
- Arduino const用法及代碼示例
- Arduino Ethernet - server.begin()用法及代碼示例
- Arduino ArduinoBLE - BLEService()用法及代碼示例
- Arduino digitalWrite()用法及代碼示例
- Arduino ArduinoBLE - bleCharacteristic.subscribe()用法及代碼示例
- Arduino Servo - attach()用法及代碼示例
- Arduino write()用法及代碼示例
- Arduino Arduino_LSM9DS1 - readGyroscope()用法及代碼示例
- Arduino ArduinoSound - FFTAnalyzer.input()用法及代碼示例
- Arduino MKRGSM - gprs.attachGPRS()用法及代碼示例
- Arduino WiFiNINA - WiFi.config()用法及代碼示例
- Arduino MKRGSM - sms.read()用法及代碼示例
- Arduino MKRNB - getCurrentCarrier()用法及代碼示例
- Arduino Scheduler - Scheduler.startLoop()用法及代碼示例
- Arduino Arduino_LSM9DS1 - magneticFieldAvailable()用法及代碼示例
- Arduino MKRWAN - available()用法及代碼示例
- Arduino ArduinoBLE - BLE.poll()用法及代碼示例
- Arduino ArduinoBLE - bleCharacteristic.hasDescriptor()用法及代碼示例
- Arduino Ethernet - EthernetUDP.parsePacket()用法及代碼示例
- Arduino WiFi101 - WiFi.subnetMask()用法及代碼示例
- Arduino TFT - PImage.width()用法及代碼示例
注:本文由純淨天空篩選整理自arduino.cc大神的英文原創作品 Integer Constants。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。