Framework (A-Z)

Reference for Wiring version 1.0 Build 0100+ If you have a previous version, use the reference included with your software. If see any errors or have any comments, let us know.

Name

#define

Examples
// replaces COUNT with number 1000 
// doesn't allocate ram for a variable
#define COUNT 1000

int i = 0;

void setup()
{
  Serial.begin(9600);
}

void loop()
{
  if (i < COUNT) {
    Serial.print("i = ");
    Serial.println(i);
  }
  i = i+1;
  // i will overflow to -32768 eventually
  // printing again  
}

// replaces MYLED with number 8 
// doesn't allocate ram for a variable
#define MYLED 8

void setup()
{
  pinMode(MYLED, OUTPUT);
}

void loop()
{
  digitalWrite(MYLED, HIGH);
  delay(100);
  digitalWrite(MYLED, LOW);
  delay(100);
}
Description the #define directive directs the preprocessor to replace all subsequent occurrences of a macro with specified replacement tokens. There are Object-like macros Function-like macros. An object-like macro definition replaces a single identifier with the specified replacement tokens. The following object-like definition causes the preprocessor to replace all subsequent instances of the identifier COUNT with the token 1000 as shown in the example: #define COUNT 1000. IMPORTANT Note: The #define does direct identifier replacement in the code, the identifier it is not a variable. More complex than object-like macros, a function-like macro definition declares the names of formal parameters within parentheses, separated by commas and are beyond this tutorial. For further information on this subject refer to "function-like macro definitions in C/C++".
Syntax
#define identifier replacement
Parameters
identifier the token to be replaced
replacement the replacement token
Usage Application
Updated on July 07, 2011 11:08:05pm PDT

Creative Commons License