declaration - declare extern variable within a C function? -


i define variable in c file: int x, , know should use extern int x declare in other files if want use in other files.

my question is: should declare in other files?

  1. outside of functions,

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

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

my doubts are:

  • which 1 correct?, or
  • which preferred if both correct?

  1. both correct.

  2. which 1 preferred depend on scope of variable's use.

    • if use in 1 function, declare in function.

      void foo()  {      extern int x;   <--only used in function.      printf("%d",x);    } 
    • if used more 1 function in file, declare global value.

      extern int x;   <-- used in more 1 function in file void foo() {     printf("in func1 :%d",x);    }     void foo1()  {     printf("in func2 :%d",x);    }   

Comments

Popular posts from this blog

Detect support for Shoutcast ICY MP3 without navigator.userAgent in Firefox? -

web - SVG not rendering properly in Firefox -

java - JavaFX 2 slider labelFormatter not being used -