0

Example for volatile variable in c

give small program on volatile variable

12th Aug 2018, 7:09 AM
Bhavin Kateshiya
Bhavin Kateshiya - avatar
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.
12th Aug 2018, 2:17 PM
Rocky Lee
Rocky Lee - avatar