#define
Description
#define
is a useful C++ component that allows the programmer to give a name to a constant value before the program is compiled. Defined constants in arduino don’t take up any program memory space on the chip. The compiler will replace references to these constants with the defined value at compile time.This can have some unwanted side effects though, if for example, a constant name that had been defined is included in some other constant or variable name. In that case the text would be replaced by the defined number (or text).
In general, the
keyword is preferred for defining constants and should be used instead of const
#define
.Syntax
#define constantName value
Parameters
: the name of the macro to defineconstantName
: the value to assign to the macrovalue
Example Code
1#define ledPin 32 // The compiler will replace any mention of ledPin with the value 3 at compile time.
Notes and Warnings
There is no semicolon after the #define statement. If you include one, the compiler will throw cryptic errors further down the page.
1#define ledPin 3; // this is an error
Similarly, including an equal sign after the #define statement will also generate a cryptic compiler error further down the page.
1#define ledPin = 3 // this is also an error
See also
Suggest changes
The content on docs.arduino.cc is facilitated through a public GitHub repository. If you see anything wrong, you can edit this page here.
License
The Arduino documentation is licensed under the Creative Commons Attribution-Share Alike 4.0 license.