Arduino Programming Language: Static

Name

static

Description

The static keyword is used to create variables that are visible to only one function. However unlike local variables that get created and destroyed every time a function is called, static variables persist beyond the function call, preserving their data between function calls. 
Variables declared as static will only be created and initialized the first time a function is called.

Syntax

static datatype variable;

Parameters

  • datatype: any data type: int, double, long, char, byte, etc
  • variablename: a valid variable name

Usage

Application

Examples

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

void loop()
{
// x is initialized only once across the calls of loop()
static int x = 0;
Serial.println(x); // outputs the value of x
x = x + 1;
delay(200);
}

Related Functions


Based on the reference originally written by Arduino Community,  wiring.org.co and  arduino.cc