20

I define a variable in a C file: int x, and I know I should use extern int x to declare it in other files if I want to use it in other files.

My question is: where should I declare it in other files?

  1. Outside of all functions,

    // in file a.c:
    int x;
    
    // in file b.c:
    extern int x;
    void foo() { printf("%d\n", x); }
    
  2. within the function(s) that will use it?

    // in file b.c:
    void foo() {
       extern int x;
       printf("%d\n", x);
    }
    

My doubts are:

  • Which one is correct?, or
  • Which is preferred if both are correct?
yulian
  • 1,601
  • 3
  • 21
  • 49
Wang Tuma
  • 893
  • 5
  • 14
  • 24
  • 4
    See [What are `extern` variables in C](http://stackoverflow.com/questions/1433204/what-are-extern-variables-in-c/) for an extensive disquisition on the subject. The declaration should be in a header so that it is only written once — this means you can change it much more easily than if there are twenty copies of the declaration in twenty functions in six source files. Both `a.c` and `b.c` include the header — it's included in `a.c` to ensure that the declaration matches the definition. Both variants that you show are 'technically correct'; they work as you want. Neither is desirable, though. – Jonathan Leffler Aug 20 '13 at 06:13

3 Answers3

20
  1. Both are correct.

  2. Which one is preferred depend on the scope of variable's use.

    • If you only use it in one function, then declare it in the function.

      void foo() 
      {
           extern int x;   <--only used in this function.
           printf("%d",x);   
      }
      
    • If it is used by more than one function in file, declare it as a global value.

      extern int x;   <-- used in more than one function in this file
      void foo()
      {
          printf("in func1 :%d",x);   
      }    
      void foo1() 
      {
          printf("in func2 :%d",x);   
      }  
      
livibetter
  • 19,832
  • 3
  • 42
  • 42
Lidong Guo
  • 2,817
  • 2
  • 19
  • 31
  • Please clear my doubt: for extern variables declared inside function, how is this any different from automatic storage class? Is the extern variable has static lifetime scope? – Himanshu Mittal Jul 17 '21 at 17:57
6

Suppose if you declare within function:

// in file b.c:
void foo() {
    extern int x;
    printf("%d\n", x);
}
void foo_2() {
    printf("%d\n", x);  <-- "can't use x here"
}

then x is visible locally inside function foo() only and if I have any other function say foo_2(), I can't access x inside foo_2().

Whereas if you declares x outside before all function then it would be visible/accessible globally in complete file (all functions).

  // in file b.c:
  extern int x;
  void foo() { printf("%d\n", x); }
  void foo_2() { printf("%d\n", x); }  <--"visible here too"

So if you need x in only single function then you can just declare inside that function but if x uses in several function then declare x outside all function (your first suggestion).

Grijesh Chauhan
  • 57,103
  • 20
  • 141
  • 208
5

you can use another technique in the way you can declare a variable with the specifier extern.

// in file a.c:
int x;

// in file b.h  //   make a header file and put it in 
                //   the same directory of your project and every
                //   time you want to declare this variable 
                //   you can just INCLUDE this header file as shown in b.c
extern int x;

// in file b.c:
#include "b.h"
void foo() { printf("%d\n", x); }
Armia Wagdy
  • 567
  • 6
  • 22