When you add the
volatile
attribute to a variable all you are doing is telling the compiler
"The value in this variable is subject to change at any time. Do not try to keep it in a register, or use a working copy of the value. Do not optimise it at all. Just leave it alone."
It is intended for use with interrupts and other such events which do not happen as part of normal execution of your program but which use particular memory to transfer information. If the compiler did not know about it, it could happily optimise
while (bytesRecieved == 0)
{
}
into an
if
condition rather than a
while
, since
bytesRecieved
cannot be modified within the loop.