What does volatile mean in C language?

Status
Not open for further replies.

dayal

Member level 3
Joined
Aug 26, 2007
Messages
54
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Activity points
1,629
hai all,

what is mean by volatile and what is the use, can anyone tell me briefly.

Thanks
dayal
 

c language volatile

use the search function:
In short: it's used if a variable or register can change it's value outside a specific program location, e.g. an IO port or a variable which is changed in an ISR.
The keyword "volatile" tells the compiler not to optimize this code, so the variable's or register's value is really determined again. Example:
Code:
volatile int counter;

void TimerInterrupt(void)  // interrupt service routine triggered e.g. by timer interrupt
{
  counter++;
}

void main(void)
{
  counter = 0;
  while(1)
  {
    if (counter == 5) DoSomething(); // without volatile declaration this line wouldn't be processed as counter isn't changed here
  }
}
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…