Understanding `extern` storage class specifier in

2019-09-12 11:38发布

Consider given C codes :

#include<stdio.h>
extern int i;
int main(){
    printf("%d", i);
    return 0;
}

It gives Compilation error. While if I initialize extern int i=10; then output is 10.

#include<stdio.h>
extern int i=10; //initialization statement
int main(){
    printf("%d", i);
    return 0;
}

And also, if I assign int i=10;then output is 10.

#include<stdio.h>
 extern int i;
 int i=10;  //assignment
int main(){
    printf("%d", i);
    return 0;
}

And

#include<stdio.h>
 extern int i;
 int main(){
    int i=10;   //assignment
    printf("%d", i);
    return 0;
}

Variation :

#include<stdio.h>
 extern int i;
 int i=20;
 int main(){
    int i=10;
    printf("%d", i);
    return 0;
}

Since, int i is local variable, so output is 10.

Please, can you explain some important point about extern storage class in C


I read somewhere, a declaration declares the name and type of variable or function. A definition causes storage to be allocated for the variable or the body of the function to be defined. The same variable or function may have many declaration, but there can be only one defition for that variable or function.

标签: c extern
4条回答
放我归山
2楼-- · 2019-09-12 11:40

The extern int i; declaration just tells the compiler that there somewhere (in this or another source) exists an actual declaration of i in global scope. It's not in itself the actual declaration of i. Therefore it's inappropriate to assign a value to i in that declaration (which is why the second try is invalid). Your first attempt is invalid since you don't have an actual declaration of i.

Your third is OK though as you have an actual declaration of i, but you wouldn't have to have the extern int i; declaration.

Your last samples involves i in both local and global scope. In such a case the variable in local scope will be used. That's why you don't get 20 printed.

查看更多
做个烂人
3楼-- · 2019-09-12 11:42

when the code references a symbol via the extern modifier, it means the symbol is actually defined elsewhere rather than in the current file.

An exception, if the extern modififier is used for a symbol that is actually defined in the current file, that is OK.

查看更多
SAY GOODBYE
4楼-- · 2019-09-12 11:47

The extern storage class specifier is used to give a reference of a global variable that is visible to ALL the program files. When you use 'extern', the variable cannot be initialized however, it points the variable name at a storage location that has been previously defined.

查看更多
Emotional °昔
5楼-- · 2019-09-12 11:56

Consider

int i;

declared outside all the functions of the programme including main.

This means i will have

  • file scope
  • static storage duration
  • space is allocated for i. #This is important.

Consider

extern int i;

declared outside all the functions of the programme including main.
This means i will have

  • file scope
  • static storage duration
  • i is only declared, not defined which means space is not allocated.
  • i is assumed to be defined somewhere else, may be, in an include file

Consider

extern int i=10; 

declared outside all the functions of the programme including main.
This is the case where you're initializing the extern variable i. In this case

  • Space is allocated for i.
  • i will be initialized to 10.
  • The keyword extern is neglected which means i is NOT declared only.

Note

For extern int i it is mandatory that the variable should be defined somewhere else, ie in another source file. If this is not the case, you will a compile-time error.

查看更多
登录 后发表回答