1

In C, (GCC Compiler), what is the default storage class of global variables?

Let me share a code for example:

int i; 

void f()
{ --statements--}

main()
{ --- set of statements--- }

Am I correct if I say that variable i would be static and not extern ?

Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
Doherty
  • 85
  • 1
  • 8
  • Why would this be specific to GCC? Doesn't the language say what it is? – Barmar Nov 01 '16 at 08:36
  • As far as I am aware. there are some differences between different compilers. In formal arguments, like --> int func(int x) , x can be register (in gcc) or auto (in turbo c) based upon compiler version. – Doherty Nov 01 '16 at 08:39
  • What's an _auto_ in Turbo C ?? – Jabberwocky Nov 01 '16 at 08:52
  • 1
    @Doherty Function parameters have absolutely nothing to do with storage duration and linkage of file scope variables... That being said, all standard compilers behave the same in either case. – Lundin Nov 01 '16 at 08:54
  • @Lundin. I am sorry. I didn't get that. Could you please rephrase that for me? – Doherty Nov 03 '16 at 13:47

2 Answers2

6

In this code, i has static storage duration, and external linkage.

Storage duration refers to the lifetime of the variable's storage. Static storage duration means that the variable exists for the entire lifetime of the program.

Linkage refers to the relationship between names and objects. External linkage means that all instances of a name with external linkage denote the same object. Your int i; will match a declaration extern int i; from another translation unit.

The keyword static is used in different contexts as a storage duration specifier and as a linkage specifier, so it is not clear to ask whether a variable is static. Instead, the variable's storage duration and linkage should be considered.

M.M
  • 138,810
  • 21
  • 208
  • 365
  • Could you please share an example which to show the external linkage by default ? I encountered a linking error when ran the same. – Doherty Nov 04 '16 at 05:12
  • And by external linkage, did you mean that the variable can be accessed by another different file/program written ? Or it would be accessible by the functions within the same program ? – Doherty Nov 04 '16 at 05:16
  • @Doherty other files in the same program. If you have a linking error then post a new question with your *exact* code (and the command you used to compile and link). But check [this thread](http://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) first to see if the problem is mentioned there. – M.M Nov 04 '16 at 05:19
1

You have the following cases for variables declared outside of any function:

static int variableWithfileScope;  // Not exported. Only visible in current file.

int globalVariableThatIsExported;  // Exported. 

// Forward declaration. No variable defined.
extern int globalVariableThatIsDefinedSomewhereElse;  

This is defined by the standard and is applicable to all C compilers.

Klas Lindbäck
  • 33,105
  • 5
  • 57
  • 82