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?
1
  • 4
    See 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. Commented Aug 20, 2013 at 6:13

3 Answers 3

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);   
      }  
      
Sign up to request clarification or add additional context in comments.

1 Comment

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?
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).

1 Comment

Thanks Yulian, Lidong and Grijesh. I think I got all the answer that I want. :)
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); }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.