aboutsummaryrefslogtreecommitdiffstats
path: root/docs/source/arduino/define.rst
blob: 6190cb91463692e55b8e6f0bef9d681b1a0c94a5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
.. _arduino-define:

Define
======

``#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 *`const <http://arduino.cc/en/Reference/Const>`_*
keyword is preferred for defining constants and should be used
instead of #define.



Arduino defines have the same syntax as C defines:



Syntax
------

``#define constantName value``



Note that the # is necessary.



Example
-------

::

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



Tip
---

There is no semicolon after the #define statement. If you include
one, the compiler will throw cryptic errors further down the page.



::

    #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.



::

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



See
---


-  `const <http://arduino.cc/en/Reference/Const>`_
-  `Constants <http://arduino.cc/en/Reference/IntegerConstants>`_