what is difference b/w extern & volatile variable??
Answers were Sorted based on User's Feedback
Answer / 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 |
Answer / sathya
extern keyword is used to strike the global declaration.
but volatile its indirectly change the values in externally
Is This Answer Correct ? | 5 Yes | 1 No |
Answer / mchilakala
Extern keyword specifies that the variable had been
declared globally where as volatile keyword specifies that
there is a chance of changing it's value by external
conditions.
Ex:
if we declare a variable as
"Volatile date=03/08/2011"
then output is 03/08/2011
then after one day the output will change to 04/08/2011.
Is This Answer Correct ? | 3 Yes | 0 No |
Answer / anonimos
volatile variables are used on RT Embedded systems to
interface a physical memory mapped/IO mapped cell on the
computer board (volatile pointer).
Example define IO port:
#define PortA (*(volatile unsigned char *)0x3b)
unsigned char inputValue=PortA;
optimization may attempt to perform paging to hard drive of
to cache or even CPU registers so when reading from the
physical location in Mem/IO space the program will actually
read old value that was paged/cached by optimization
algorithm of the computer/board, even after the Input
changed on this Memory/IO cell.
volatile instruct the compiler to prevent optimization by
caching to registers/cache.
Is This Answer Correct ? | 1 Yes | 0 No |
Answer / deepa
extern is a keyword used to declare variables as global
varible.volatile variables are temporary variable
Is This Answer Correct ? | 1 Yes | 7 No |
How can I get the current date or time of day in a c program?
write a program to generate 1st n fibonacci prime number
write c program without semicolon
11 Answers MindTech, TCS, Wipro,
What is array of structure in c programming?
What is 'makefile' in C langauage? How it be useful? How to write a makefile to a particular program?
Program to find larger of the two numbers without using if-else,while,for,switch
what is the similarities between. system call and library function?
How do I declare an array of N pointers to functions returning pointers to functions returning pointers to characters?
Write a program to swap two numbers without using a temporary variable?
HOW TO SWAP TWO NOS IN ONE STEP?
wat is the output int main() { char s1[]="Hello"; char s2[]="Hello"; if(s1==s2) printf("Same"); else printf("Diff"); }
what is the c source code for the below output? 10 10 10 10 10 10 10 10 10 10 9 9 7 6 6 6 6 6 6 9 7 5 9 7 3 2 2 5 9 7 3 1 5 9 7 3 5 9 7 4 4 4 4 5 9 7 8 8 8 8 8 8 8 8 9