-1

In c, it seems to me that everything written outside of a function is already 'extern'. When, then, exactly do you have to specify a variable as extern?

Does it have to do with definition vs declaration?

Also, is declaring a variable with extern necessary in order to access a variable defined in another file already?

Jobs
  • 3,317
  • 6
  • 26
  • 52
  • Maybe read the accepted answer here: http://stackoverflow.com/questions/1433204/how-do-i-share-a-variable-between-source-files-in-c-with-extern-but-how – AntonH Jul 11 '14 at 04:21
  • By using `extern`, you are telling the compiler that whatever follows it will be found (non-static) at link time. – Jayesh Bhoi Jul 11 '14 at 04:25
  • 1
    and and `me that everything written outside of a function is already 'extern'.` how? it's only local to file in which it written. – Jayesh Bhoi Jul 11 '14 at 04:27
  • But when you #include that file, you can access the variable, right? – Jobs Jul 11 '14 at 04:32
  • 1
    yes you can access it. but make sure you have `extern` it in header file other wise multiple definition error cause. – Jayesh Bhoi Jul 11 '14 at 04:34

2 Answers2

4

the extern extends the visibility to the whole program, by externing a variable we can use the variables anywhere in the program provided we know the declaration of them and the variable is defined somewhere.

Declarations of variables at file scope (Not in other files ) are external by default.

In c, it seems to me that everything written outside of a function is already 'extern'. When, then, exactly do you have to specify a variable as extern?

this is most probably true for c functions By default, the declaration and definition of a C function have “extern” prepended with them

For example have one file named file1.c and declared function lilke

int my_function();

There’s an extern present in the beginning which is hidden and the compiler treats it as below.

extern int my_function();

Therefore whenever we define a C function, an extern is present there in the beginning of the function definition.

For C variables

While defining a function, we can prepend it with extern without any issues. But it is not the case with C variables.

If we put the presence of extern in variable as default then the memory for them will not be allocated ever, they will be declared only. Therefore, we put extern explicitly for C variables when we want to declare them without defining them. `

Jayesh Bhoi
  • 24,694
  • 15
  • 58
  • 73
2

Here is how you would use extern variables.

I have a UART.c:

#include<stdio.h>  
#include<string.h>
#include<UART.h>

int UART_Rcv(unsigned char* Rx_Str[], unsigned int len);

int status;

int UART_Rcv(unsigned char* Rx_Str[], unsigned int len)
{
    //Some code for implementation of UART receive.

    //set status value and return it, May be (SUCCESS or FAILURE?)
    return status;
}

Now I have a UART.h

 //header guard
 #ifndef __UART_H
 #define __UART_H

 //Some UART related macros

 extern int status;
 extern int UART_Rcv(unsigned char* Rx_Str[], unsigned int len);

 //other externs if any

 #endif

Now I have a main.c which is going to use the function and variable declared in UART.c

#include<stdio.h>  
#include<string.h>
#include<UART.h>

#define WIMP_OUT_N_GO_HOME     (0)

int main(void)
{
    unsigned char Received_String[30];

    //Some code and may be initialization of UART?

    //Flush the Received_String before using it
    UART_Rcv(Received_String, 10);

    //Some code

    return WIMP_OUT_N_GO_HOME;
}

When a variable is defined, the compiler allocates memory for that variable and possibly also initializes its contents to some value. When a variable is declared, the compiler requires that the variable be defined elsewhere. So By externing your UART_Rcv() function and the status variable, you hinted your compiler that those are defined outside of the functional block, or may be in a different source, like we did here. the definitions for those will be found at the linker time.

An external variable must be defined, exactly once, outside of any function