Interviewer asked me What is Volatile variable in c language
My answer was something like this
Condition : Variable is not volatile
in main function x is set to zero. compiler observe that value of x is always zero The compiler does not go inside the while loop
x variable in main routine and x in ISR store at different locations
Condition : Variable is volatile
Volatile indicate that value of variable may be change at any time from outside of function flow ISR, variable can’t be optimized by the compiler
Volatile variable is a very important topic and in most interviews this question is asked. I think my answer is correct as I know
What do you think, Is my answer suitable, if not, then where I need improvement ? What can be a good answer ?
My answer was something like this
Condition : Variable is not volatile
in main function x is set to zero. compiler observe that value of x is always zero The compiler does not go inside the while loop
x variable in main routine and x in ISR store at different locations
C:
int x;
int main(void) {
x = 0;
while (x == 1) {
printf(" hi ")
printf(" welcome ")
}
}
}
ISR() {
x = 1;
}
Condition : Variable is volatile
Volatile indicate that value of variable may be change at any time from outside of function flow ISR, variable can’t be optimized by the compiler
C:
volatile int x;
int main(void) {
x = 0;
while (x == 1) {
printf(" hi ")
printf(" welcome ")
}
}
}
ISR() {
x = 1;
}
What do you think, Is my answer suitable, if not, then where I need improvement ? What can be a good answer ?