Sunday, August 2, 2009

Can u explain the importance of volatile function in c program?

Nope.

Can u explain the importance of volatile function in c program?
The volatile keyword for a Storage Class modifier informs the compiler that the variable can be modified in ways unknown the the compiler. Although this is a new feature, it is not available with old compilers, the concept is not. This usually applies to the variables mapped to a particular memory address viz.,device registers.





In their cases it is important for the series of statements to be executed exactly as they are written and not reordered for optimization purposes. The best example would be the keyboard where you read the character from the keyboard and store it to a corresponding variable.


Ex:


void get_keyboard()


{


extern char KEYBOARD;


char c0,c1;





c0=KEYBOARD;


c1=KEYBOARD;


}





Here you store 2 values to c0 and c1 using the same variable KEYBOARD,which means the variable KEYBOARD holds different values to be stored at c0 and c1 seperately. However the compiler unaware of this fact will store the KEYBOARD value in a register and then store it to c0 and c1 resulting the same value to be assigned to both c0 and c1. So in order to get it goin to ensure the KEYBOARD is read twice you declare it volatile.





extern volatile char KEYBOARD;





This should answer your question i guess!


No comments:

Post a Comment