几天前我接受了一次采访,但仍然在寻找答案。 我想了解使用volatile关键字的意义。volatile&extern和有什么不一样?
找到下面的代码:两种不同的情况。
//project1
//File1.c
int abc;//Global variable
/*And this variable is getting used in some other files too.*/
if(abc == 3) //Say
{
printf("abc == 3");
}
else
{
printf("abc != 3");
}
/*So if or else part will not be optimized
because "abc" can not be predicted,
the value can chage at any point of time */
//Project2
//file1.c
volatile int abc;//Global variable with volatile keyword
/*And this variable is getting used in some other files too.*/
if(abc == 3) //Say
{
printf("abc == 3");
}
else
{
printf("abc != 3");
}
/*So if or else part will not be optimized
because "abc" can not be predicted as it is declared as volatile,
the value can chage at any point of time */
为什么我们应该使用volatile关键字呢?
[静态全局变量和静态变量之间的区别是什么?](http:// stackoverflow。com/questions/346306/what-is-a-static-global-and-static-volatile-variable) – NPE 2012-02-07 09:05:57
volatile和extern完全不相关。 – 2012-02-07 09:07:15
@MarkByers:你说得对,但根据面试官的问题,上述两个都是一样的。那么为什么波动? – 2012-02-07 09:12:47