0
Example for volatile variable in c
give small program on volatile variable
1 Réponse
0
int i = 0;
while (i > 0)
{
// do something
}
...
With the code snippet above, C/C++ compiler will try to optimize your code to make a infinite loop: it sees you assign 4 to i, so while (4 > 0) is true certainly.
But this may not be what you want, another thread may change i, some interrupt can do that also... If so, the program should get out of the while loop.
In order to prevent compiler optimizing for the variable, you can use the volatile keyword.