Define

從 YFRobotwiki
跳到: 導覽搜尋

#define是個有用的C組件,可以在程序編譯前,給常量一個名詞。被定義的常量不會佔用Arduino芯片的內存。在編譯時編譯器會用事先定義的值來取代這些常量。


同樣也會帶來一些不利因素,舉例來說,一旦常量被#define,將自動包含其他一些常量或者變量名。那樣的話,這些代碼將被替換成#define的數字。


通常,關鍵字const常被用來取代#define來定義常量。


Arduino宏定義與C宏定義有同樣的語法:

#define constantName value

注意:"#"符號不可缺。


示例

#define ledPin 3
// The compiler will replace any mention of ledPin with the value 3 at compile time.


提示

在#define 聲明後不能有分號。如果存在分號,編譯器會拋出語義不明的錯誤。

#define ledPin 3;    // this is an error 

類似的,在#define聲明中包含"="也會產生語義不明的編譯錯誤。

#define ledPin  = 3  // this is also an error 




返回Arduino語法參考列表

更多建議和問題歡迎反饋至 YFRobot論壇