简体   繁体   中英

Extern Variable

int main()
{
  extern int i;
  i=20;
  printf("%d",i);
  return 0;
}

The compiler is giving an Error that 'i' is undefined

Whats the Reason?

By saying extern you tell the compiler that i is defined in a different translation unit , which I'm guessing you don't have. There's a difference between declaration and definition in C. In short, the former is telling the compiler the type of the variable, the latter is telling to allocate storage for it.

Just drop that extern for now.

Difference :

1.int i; // i is defined to be an integer type in the current function/file
2.extern int i; // i is defined in some other file and only a proto-type is present here.

Hence while compiling, the compiler(LDD) will look for the original definition of the variable and if it does'nt find, it'll throw an error 'undefined reference to `i'.

i has no storage location.

You must link with a translation unit that includes int i; (not extern ).

extern

Allows one module of your program to access a global variable or function declared in another module of your program. You usually have extern variables declared in header files. If you don't want a program to access your variables or functions, you use static which tells the compiler that this variable or function cannot be used outside of this module.

errno.h

#ifndef ERRORS /* prevent multiple inclusion */
#define ERRORS 0
extern int errno; <- Declaring the variable to be external.
extern void seterrorcode(int errcode);
#endif

errno.c

#include "errno.h"
int errno; <- This is where the definition of errno occurs
static int stuff; <- This is private to this module.
void seterrorcode(int errcode)
{
errno = errcode;
}

main.c

#include "errno.h"
/* extern int stuff : This line will produce an undefined variable error when linking */

int main()
{
seterrorcode(0);
if (errno > 0)
; /* Error code > 0, do something */
}

For more Info

extern keyword is used to tell the compiler that the variable

is defined in another file

but during linking, the linker does not resolve the variable

so you get an error

1.For this question,you need to understand that definition is a superset of declaration.In declaration,we just introduce the variable that is going to be used but do not allocate any memory to it.But in the case of definition,we both declare and allocate memory to the variable.

2.int i;

This sentence will both declare and define the variable i

3.On the other hand

extern int i;

This sentence will just extend the visibility of the variable to the whole program.Effectively,you are just defining the variable and not declaring it. i=20; this line is trying to assign 20 to a variable that does not exist.

4.It can be corrected by writing

extern int i=20;

Hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM