幾天前我接受了一次採訪,但仍然在尋找答案。 我想了解使用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