using extern variable in c language

Thread Starter

skyr6546

Joined Mar 22, 2019
73
I have written following code for extern variable

File Test1.c
Code:
#include <stdio.h> 
int x = 0;   
void Func();

int main() {
  Func(); 
  printf("%d", x);
 
  return 0;
}
File Test2.c
Code:
extern int x;
void Func() { 
  x++;
}
Program output : 1

1. Am I using the extern variable correctly ?
2. Why program output 1 appear even if I remove the extran keyword from File Test2.c ?
3. Why program show error if I am declaring x as local variable ?
 

BobaMosfet

Joined Jul 1, 2009
2,113
No. 'extern' is used for opacity (explained further down).

For example, other than main.c, every C file you create, should have an associated H file.

i2c.c // source file for i2c communication functions
i2c.h // header file for i2c communication functions.

Inside the C file we might have a function foo() that is a global function (which means it can be accessed by other functions). In the C file, we declare a prototype for it thusly:

Code:
int foo(char*);
In the H ile, we declare it thusly:

Code:
extern  int foo(char*)
This way, when the linker finds anywhere in the C files that a call to foo() is made, it will know where the function is declared and defined, it won't duplicate it, because it can find an 'external' reference to it via the i2c.h file, pointing it at the i2c.c file.

Any global variable is handled the same way.

C File:
Code:
int   myErr;
H File:
Code:
extern int  myErr;
By writing code this way, you control your global and static scope, making your code cleaner and safer, because each C file is somewhat opaque (that is revealing only what it wants anyone else to have access to in terms of variables and functions).
 

Thread Starter

skyr6546

Joined Mar 22, 2019
73
No. 'extern' is used for opacity (explained further down)

By writing code this way, you control your global and static scope, making your code cleaner and safer, because each C file is somewhat opaque (that is revealing only what it wants anyone else to have access to in terms of variables and functions).
@BobaMosfet Not understanding clearly, How many c files and h files in your examples.

Can you give a complete example as I shown my example code.
 

BobaMosfet

Joined Jul 1, 2009
2,113
@BobaMosfet Not understanding clearly, How many c files and h files in your examples.

Can you give a complete example as I shown my example code.
I did give you examples that were adequate. In what was shown there is 1 c file, and 1 h file. But it does not matter how many C and H files you have, this methodology should be used for all.

  1. do not use 'extern' in c files; it is used in header files ONLY
  2. extern is used to tell the linker that a definition of a variable, function/procedure, or type exists in a C file, elsewhere
  3. size specifications for a variable exist only in C files; do NOT put them in header files, it confuses the linker

The last item above means that if (for an example) you create an array in a C file:
Code:
char    myArray[15];
The size specification (ie '15') should NOT be in the coinciding header file:
Code:
extern char   myArray[];
This way the linker knows that the variable 'myArray' is defined and declared in just one location.

This will help you:

Title: Standard C [Quick Ref]
Author(s): P.J.Plauger, Jim Brodie
ISBN: 1-55615-158-6
 
Top