Email: Password: Remember Me | Create Account (Free)

Back to Subject List

Old thread has been locked -- no new posts accepted in this thread
???
05/19/08 07:26
Read: times


 
#154890 - meaning of "volatile" with example
Responding to: ???'s previous message
Dear Harshada,

volatile is a keyword, it tells compiler that this variable may change its value due to something not known to compiler.

e.g. considor following segment

unsigned char ch;

unsigned char my_func(void)
{

ch = 0xFF;

....... //some code lines
....... //these lines donot change value of ch directly or indirectly
.......
.......

if(ch == 0)
{
return(0);
}
else
{
return(1);
}

}//end of my_func



in the above segment ch is global variable initialised to 0xFF.
hence while compiling if..else compiler knows value of ch is 0xFF, hence it will omit the if..else statement & returns 1 irrespective of value of ch.
it is possible that some ISR is making ch = 0, which compiler may fail to visualise.


On the other hand if we declare it as volatile compiler will compile if..else though it knows that ch is initialised to 0xFF.

Regards,
Mahesh


List of 22 messages in thread
TopicAuthorDate
keyword volatile            01/01/70 00:00      
   Opposite!            01/01/70 00:00      
      volatile modifier            01/01/70 00:00      
         Incorrect            01/01/70 00:00      
         incorrect!            01/01/70 00:00      
            even more            01/01/70 00:00      
               A heap o' trouble            01/01/70 00:00      
                  the curse of the PC programmers            01/01/70 00:00      
      meaning of "volatile" with example            01/01/70 00:00      
   example            01/01/70 00:00      
      gobbelygook            01/01/70 00:00      
      heap?            01/01/70 00:00      
      Confused?            01/01/70 00:00      
   Google told me...            01/01/70 00:00      
   link            01/01/70 00:00      
      go through my explanation            01/01/70 00:00      
         put simply...            01/01/70 00:00      
   thanks            01/01/70 00:00      
   answer accepted by interviewer            01/01/70 00:00      
      Not entirely            01/01/70 00:00      
      not input only            01/01/70 00:00      
         That's the kind of thing I'd be looking for...            01/01/70 00:00      

Back to Subject List