Interview question : What is Volatile variable

Thread Starter

Fanfire174

Joined Mar 13, 2018
240
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

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;
}
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 ?
 

dl324

Joined Mar 30, 2015
16,845
What do you think, Is my answer suitable, if not, then where I need improvement ? What can be a good answer ?
Your answer seems sufficient for as far as you went. You're assuming that the ISR disables interrupts so that the updating of a 2 or 4 byte memory location won't be interrupted.
 

Thread Starter

Fanfire174

Joined Mar 13, 2018
240
Your answer seems sufficient for as far as you went. You're assuming that the ISR disables interrupts so that the updating of a 2 or 4 byte memory location won't be interrupted.
Thank you, your reply confirmed that this is a valid answer, and my example is correct
 

402DF855

Joined Feb 9, 2013
271
Thank you, your reply confirmed that this is a valid answer, and my example is correct
Your example appears to presume that ISR would be invoked after main has initialized x=0, but this may not be true. Plenty of startup code executes prior to main() including setting the stack register and perhaps enabling interrupts. ISR could be invoked prior to main depending on your particular setup.

Assuming ISR is called after "x=0" there is only a slight window of time immediately after that assignment where the ISR call will have any impact. After x=0, the code will read x and if x==1 enter an infinite loop of printfs; if x==0 the while condition will fail and despite no return statement it is likely main will exit and the routine that called main will enter an infinite loop. Or perhaps reset.

So, your example is somewhat illustrative, but I don't think that specific code would be helpful in an interview.
 

ericgibbs

Joined Jan 29, 2010
18,766
hi 174,
This is what Google shows.
https://barrgroup.com/embedded-systems/how-to/c-volatile-keyword

C's volatile keyword is a qualifier that is applied to a variable when it is declared. It tells the compiler that the value of the variable may change at any time--without any action being taken by the code the compiler finds nearby. The implications of this are quite serious. However, before we examine them, let's take a look at the syntax.

E
 

ApacheKid

Joined Jan 12, 2015
1,533
The term "volatile" tells the compiler to avoid optimizing expressions that refer to such variables because the variable could be modified asynchronously.

Yes an ISR is a very good example of this.

You might have a loop for example where some variable is incremented over and over, often the compiler's optimizer can decide to store this in a register rather than doing a read/modify/write every time it updates it.

This improves performance but is a huge problem when you have unpredictable asynchronous access to the variable, in these cases the code will not see the register (which has been chosen by the optimizer) but will access the memory, however the memory is not being updated and so any algorithms that assume it is will break often in very bewildering ways.
 
Top