Hace pocos días tuve una entrevista, pero todavía estoy buscando la respuesta. Me gustaría entender la importancia de usar palabras clave volátiles.¿Cuál es la diferencia entre volátil y externo?
Encuentra el código a continuación: Dos escenarios diferentes.
//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 */
¿Por qué deberíamos usar la palabra clave volátil en su lugar?
posible duplicado de [¿Cuál es la diferencia entre una variable estática global estática y estática?] (Http: // stackoverflow.com/questions/346306/what-is-the-difference-between-a-static-global-and-static-volatile-variable) – NPE
volátil y extern no tienen ninguna relación. –
@MarkByers: Tiene razón, pero de acuerdo con la pregunta del entrevistador, ambos elementos sirven lo mismo. Entonces, ¿por qué volátil? –