Define

From Microduino Wiki
Revision as of 09:14, 4 August 2016 by Fengfeng (talk) (Created page with "*'''Macro definition |#define ''' Macro definition is a useful C component, which allows the programmer to give a name to a constant before compiling. Constants defined in ard...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
  • Macro definition |#define

Macro definition is a useful C component, which allows the programmer to give a name to a constant before compiling. Constants defined in arduino won’t take any space in the chip. When compiling, the compiler will alter the constant reference to defined value. This may have some harmful side effects, for example, a constant name which has been defined is included in some other constant or variable names. In that case, this text will be replace by the defined numbers (or text).

Usually, using keyword const to define constants is more popular and it is useful to replace #define.

Arduino macro definition has the same syntax with C macro definition.

  • Syntax

#define constantName value 

Pay attention to that‘#’is essential.

  • Example:

#define ledPin 3
// compiler will alter all ledPin to the number 3 when compiling
  • Note

The semicolon next to the #define statement. If you add one, the compiler will trigger strange errors in the next page.


#define ledPin 3;    // this is an error 

Similarly, so does an equal sign included.


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


[Return to Arduino Syntax Manual]