Pochi giorni fa ho avuto un'intervista ma, ancora sto cercando la risposta. Mi piacerebbe capire l'importanza dell'uso di parole chiave volatili.Qual è la differenza tra volatile ed esterno?
Trova il codice qui sotto: Due diversi scenari.
//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 */
Perché dovremmo usare parola chiave volatile, invece?
possibile duplicato di [Qual è la differenza tra una variabile statica globale e variabile volatile?] (Http: // stackoverflow.it/questions/346306/what-is-the-difference-between-a-static-global-and-static-volatile-variable) – NPE
volatile ed extern sono completamente indipendenti. –
@MarkByers: hai ragione, ma secondo la domanda dell'intervistatore entrambi i precedenti servono lo stesso. Allora perché volatile? –