Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

global extern vs and l extern variable

Status
Not open for further replies.

John99407

Junior Member level 3
Junior Member level 3
Joined
Jul 14, 2019
Messages
26
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
240
Variable may be two type local or global. I am not clear about the life time and scope of global extern variable and local extern variable

does difference only with the life time not in scope ?
 

variables declared within a block is local. It is 'auto' storage class by default
and discarded on exit from block. Their scope is within the block.

variables with 'static' storage class retain their values across exit.
it can be local , or external to all blocks.Their scope is everywhere.


They can be made global by using 'extern' keyword storage class.
 

I think 'local extern' is a misnomer, local means just that, extern means it is defined in a different code module so it can't be local at the same time.

Brian.
 

Hi,

I´m no C expert, so please experts correct me if I´m wrong:

Just some notes:
"local" does not need to be written in the code (never), because it is the default - if a variable is declared in a block or function.

"global" does not need to be written either. A variable is called global if it is declared outside of all blocks/functions. (usually at the top of a file)

"external" needs to be written to a variable declaration inside a block/function ... Wrong: to notify that the variable/value should be available even outside the block/function.
[Edited (greyed out) after post#5 to avoid spreading wrong informations. Please refer to post#5]

Klaus
 

Almost Klaus - we will make a 'C' programmer of you yet :-D

You are right about the local and global definitions but 'extern' is used when more than one source file refers to the same variable. One source file defines it, the others refer to it as 'extern' so the linker knows they are the same thing and not a new variable with the same name.

example: file_1.c contains
int MyVariable;

file_2.c contains:
extern int MyVariable;

then 'MyVariable' is the same storage location for both files. The 'extern' reference can also be in a header (.h or .inc) file with the same effect. Without it, the second or subsequent files will generate their own storage with the name being local to that source file only.

There are external and public references used in C++ to specify which variables or functions are shared but that's a different matter.

Brian.
 
Hi,

Thanks Brian for clarifying this (I thought it was the other way round).
I´m still learning. Currently I can read C code quite good, but am unable to write "clean" C code.

we will make a 'C' programmer of you yet
I hope so :)

Klaus
 

Almost Klaus - we will make a 'C' programmer of you yet :-D

You are right about the local and global definitions but 'extern' is used when more than one source file refers to the same variable. One source file defines it, the others refer to it as 'extern' so the linker knows they are the same thing and not a new variable with the same name.

example: file_1.c contains
int MyVariable;

file_2.c contains:
extern int MyVariable;

then 'MyVariable' is the same storage location for both files. The 'extern' reference can also be in a header (.h or .inc) file with the same effect. Without it, the second or subsequent files will generate their own storage with the name being local to that source file only.

There are external and public references used in C++ to specify which variables or functions are shared but that's a different matter.

Brian.

Hi brian thank you,

What will be the out ? Will it MyVariable = 101
Code:
void function ()
{
  extern int MyVariable = 100;
}
  
int main()
{
  
    MyVariable++;
     
    printf( "MyVariable; = %d", MyVariable);
  
return 0;
}
 

extern int MyVariable = 100;
isn't really valid code although it might compile.
You wouldn't normally place it in a function, it would go at the top of the source file as "extern int MyVariable;" to indicate it was defined in a different source file.

In a program with a single source (.c) file 'extern' has no meaning. It is used when there is more than one source file, the definition in one and the 'extern' reference in the others. When a program has more than one source file, each source is compiled as though it was a program in its own right. Before it can be come an executable program, all the source files and any libraries used are fed to a 'linker' program that resolves the cross references between them so they can share variables and functions if necessary. The 'extern' tells the linker not to allocate space to store the variable but instead to use the space already allocated in another source file. Hence, each source file can 'see' the same variable even though it wasn't defined within its own code.

So in your case it would be something like:
Source file 1:
Code:
int MyVariable = 100;
and in source file 2:
Code:
extern int MyVariable;
int main()
{
  
    MyVariable++;
     
    printf( "MyVariable; = %d", MyVariable);
  
return 0;
}
which would print "MyVariable; = 101"

Brian.
 

@betwixt I have very difficult time to understand storage class in c language. I have spend so much time on google and tried to understand basic

I am describing life time and scope of variable Take a look at code

Code:
#include <stdio.h>

   int a = 1;
   static int b = 1;
   extern int c = 1;
   
   void storage ()
   {
       int x = 1;
       static int y = 1;
       x++;
       y++;
      printf(" x = %d\n", x) ;
	  printf(" y = %d\n", y) ;
   }
  
    int main ()
	{
	  
	  
	  printf(" a = %d\n", a) ;
	  printf(" b = %d\n", b);
	  printf(" c = %d\n", c);
	  c++;
	  
	  printf(" c = %d\n", c);
	  storage ();
	  printf("\n");
	  storage ();
	  
      return 0;	  
	}

a = 1 ( a is accessible in this source file and it will stay alive for life time of program )
b = 1 ( b is accessible in this source file and it will stay alive for life time of program )
c = 1 ( c is accessible in this source file as well as other source file and it will stay alive for life time of program )
c = 2
x = 2 ( x is accessible in function storage only and it will stay alive until function exist )
y = 2 ( y is accessible in function storage only and it will stay alive for life time of program )

x = 2
y = 3

Remember : you explained use of extern keyword with different source and I am agree with you but I am using extern in same soure in my example and it's valid

Am I wrong somewhere ?
 

a is accesible in this source file, and in other source files if they have an "extern" declaration
b is only accessible in this source file
c is declared somewhere else (that is the meaning of "extern"), and the assignment in the extern declaration is meaningless, even if it compiles.

Edit:
I now see that you have compiled and executed the source code. It seems that the compiler/linker is too kind to you. I expected a linking error if you don't have a declaration of c without "extern" somewhere.

- - - Updated - - -

My compiler does not like your code (extern declaration with assignment), but it compiles:

Code:
linux% make ext
cc     ext.c   -o ext
ext.c:5:15: warning: 'c' initialized and declared 'extern' [enabled by default]
    extern int c = 1;

You should not use code with such warnings.
 
Last edited:

My compiler gcc says:
warning: ‘c’ initialized and declared ‘extern’
5 | extern int c = 1;
| ^
Just for fun I put it in a PIC C compiler and it bombed out with "unplaced extern variable 'c'".

Brian.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top