A palavra-chave static é usada para criar variáveis que são visíveis para apenas uma função. No entanto, diferentemente de variáveis locais, que são criadas e destruidas toda vez que uma função é chamada, variáveis static persistem entre chamadas da função, preservando seu valor.

Variáveis declaradas como static são criadas e inicializadas apenas a primeria vez que uma função é chamada.

Código de Exemplo

/* RandomWalk Paul Badger 2007 RandomWalk wanders up and down randomly between two endpoints. The maximum move in one loop is governed by the parameter "stepsize". A static variable is moved up and down a random amount. This technique is also known as "pink noise" and "drunken walk". */ #define randomWalkLowRange -20 #define randomWalkHighRange 20 int stepsize; int thisTime; int total; void setup() { Serial.begin(9600); } void loop() { // test randomWalk function stepsize = 5; thisTime = randomWalk(stepsize); Serial.println(thisTime); delay(10); } int randomWalk(int moveSize) { static int place; // variable to store value in random walk - declared static so that it stores // values in between function calls, but no other functions can change its value place = place + (random(-moveSize, moveSize + 1)); if (place < randomWalkLowRange) { // check lower and upper limits place = randomWalkLowRange + (randomWalkLowRange - place); // reflect number back in positive direction } else if (place > randomWalkHighRange) { place = randomWalkHighRange - (place - randomWalkHighRange); // reflect number back in negative direction } return place; }

Ver também

Ver Também


Arduino UNO R3
Arduino Starter Kit
Please note: These are affiliate links. If you buy the components through these links, We may get a commission at no extra cost to you. We appreciate it.


  • We are AVAILABLE for HIRE. See how to hire us to build your project
  • Any suggestion, correction, and translation? please email us at, We appreciate it
  • We mainly keep improving the references in English. See English version of this page for the latest update.