what is difference b/w extern & volatile variable??

Answer Posted / antonio leite

extern states that a variable is referenced in a module but
is declared in another module. This makes the linker not
generate an error/warning when the extern variable is
referenced but wait till the declaration is stated in a
module.
volatile states that the value of a variable can be changed
anywhere in the code. This is used by the optimizer to know
that a piece of code must not be optimized when a volatile
variable is found. For example, when a variable is changed
by a interrupt timer it must be declared volatile. The code
seems to be always true because Timer_xpto seems to be
always > 0. If the code is optimized, the if would simply
disapper from the code, but this is not what the programmer
wants, so declare
extern volatile unsigned long Timer_xpto;
so that the compiler will never optimize the code below.

Timer_xpto = 100;
do something
if (Timer_xpto > 0 )
{
do any other thing
}

Here extern is stating that the variable is declared in
other module and volatile that the code where the
Timer_xpto appears must not be optimized.
See http://www.eetimes.com/discussion/programming-
pointers/4025609/Place-volatile-accurately

Is This Answer Correct ?    13 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Is it better to use a macro or a function?

658


What is keyword with example?

643


how to execute a program using if else condition and the output should enter number and the number is odd only...

1660


How can you be sure that a program follows the ANSI C standard?

1132


WRITE A CODE IN C TO SEARCH A FILE FROM NOTEPAD FILE.

2030






In C language what is a 'dangling pointer'?

640


What are the advantages of c preprocessor?

714


What is a file descriptor in c?

563


Is calloc better than malloc?

579


C program execution always begins with a) #include b) comment (/*-------*/) c) main() d) declaration instructions

613


What is the purpose of main( ) in c language?

624


Tell us bitwise shift operators?

600


Here is a neat trick for checking whether two strings are equal

566


write a program for the normal snake games find in most of the mobiles.

1787


What does do in c?

611