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

What does dm mean sexually?

806


Multiply an Integer Number by 2 Without Using Multiplication Operator

316


What is difference between array and pointer in c?

534


Is c is a procedural language?

593


What are the two types of functions in c?

562






Why is c used in embedded systems?

607


What is the difference between #include

and #include “header file”?

544


What is calloc malloc realloc in c?

589


An organised method of depicting the use of an area of computer memory used to signify the uses for different parts of the memory a) swap b) extended memory c) memory map d) all of the above

703


You are to write your own versions of strcpy() and strlen (). Call them mystrcpy() and mystrlen(). Write them first as code within main(), not as functions, then, convert them to functions. You will pass two arrays to the function in the case of mystrcpy(), the source and target array.

1778


What is a const pointer in c?

668


Can a pointer be null?

558


Explain what is the difference between a string copy (strcpy) and a memory copy (memcpy)? When should each be used?

599


What is file in c preprocessor?

648


How will you declare an array of three function pointers where each function receives two ints and returns a float?

773