Skip to content

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

Unwanted side effects: 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).

The const keyword is preferred for defining constants and should be used instead of #define.

Syntax

#define constantName value

Parameters:

  • constantName: the name of the macro to define.
  • value: the value to assign to the macro.

Example Code

#define ledPin 3

#include (include)

Syntax

Examples

& reference operator

This operator is use with pointers, if x is a variable, &x represent the address of the variable.

Syntax

1
2
3
4
5
6
int *p;       // declare a pointer to an int data type
int i = 5;
int result = 0;
p = &i;       // now 'p' contains the address of 'i'
result = *p;  // 'result' gets the value at the address pointed by 'p'
              // i.e., it gets the value of 'i' which is 5

* Dereference operator

The Arduino Documentation call it "Dereference", the * is use to represent the Dereference. "[...] If p is a pointer, then *p represents the value contained in the address pointed by p."

Syntax

1
2
3
4
5
6
int *p;       // declare a pointer to an int data type
int i = 5;
int result = 0;
p = &i;       // now 'p' contains the address of 'i'
result = *p;  // 'result' gets the value at the address pointed by 'p'
              // i.e., it gets the value of 'i' which is 5