C中的挥发性变量是什么?举些例子?它是如何工作的 [英] what is volatile varaible in c? give some examples ?how its work
本文介绍了C中的挥发性变量是什么?举些例子?它是如何工作的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
C中的volatile变量是什么?举一些例子?它的工作方式
what is volatile varaible in c? give some examples ?how its work
推荐答案
当您将volatile
属性添加到变量中时,您所要做的就是告诉编译器
When you add thevolatile
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)
{
}
设置为if
条件而不是while
,因为bytesRecieved
无法在循环中进行修改.
into an if
condition rather than a while
, since bytesRecieved
cannot be modified within the loop.
这篇关于C中的挥发性变量是什么?举些例子?它是如何工作的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文