0

I just started learning C programming. In some of the books and web articles, I could find that any Global Variable in C by default corresponds to static storage class but has external linkage. Does this mean it is partially static and partially extern? Because as per my understanding any global variable with static storage class specifier has internal linkage only and can be accessed within the same file.

P.S: I referred this question Global variables in C are static or not? , but could not get really whether Global variables are static or extern by default in C.

Lundin
  • 195,001
  • 40
  • 254
  • 396
0xdeadbeef
  • 25
  • 7
  • Also the standard provide details on the *Storage Duration of Objects* in [C11 Standard - 6.2.4 Storage durations of objects](http://port70.net/~nsz/c/c11/n1570.html#6.2.4). Make sure you understand `extern` storage class relates to whether a variable is visible to other sources, while *storage duration* relates to the defined lifetime within each of the storage classes. – David C. Rankin Aug 17 '20 at 08:26
  • "I referred this question Global variables in C are static or not? , but could not get really whether Global variables are static or extern by default in C" - The very first line of the linked answer says: *If you do not specify a storage class (that is, the extern or static keywords), then by default global variables have external linkage.*. – P.P Aug 17 '20 at 08:47
  • @P.P do you mean by that default storage class for global variables is extern?? – 0xdeadbeef Aug 17 '20 at 09:29
  • A variable cannot "be" `extern`. `Extern` means "it's not a variable, the variable is elsewhere". – Agent_L Aug 17 '20 at 09:32
  • @Agent_L That's not in C, but C++. A global variable in C is `extern` if it has no storage-class specifier. – yao99 Aug 17 '20 at 09:38
  • 1
    Folks, please stop using `extern` as an adjective. Since this question is about some muddled terminology in the C standard, we should be clear. `static` and `extern` are keywords; they are text in source code. “Static” and “external” are adjectives; they appear in phrases with multiple meanings. “External linkage” means a name can be made to refer to (linked to) the same object in multiple translation units. “External declaration” means a declaration outside of (external to) any function. Saying “external” by itself risks ambiguity if the context is not clear. – Eric Postpischil Aug 17 '20 at 10:07
  • 1
    Further, the keywords `static` and `extern` both have multiple effects and different effects depending on where they appear, so saying some identifier is `extern` or `static` is unclear about the specific characteristic intended. – Eric Postpischil Aug 17 '20 at 10:08
  • @yao99 I'm talking about C. `extern` marks declaration, as opposed to definition. If you write `extern int bar;` you don't create a variable, you only get ability to refer to a variable that lies elsewhere. This is what I mean by "variable cannot be extern". Just like `int foo(void);` doesn't create a function. – Agent_L Aug 18 '20 at 16:47

3 Answers3

1

Whenever you don't intend to use a variable declared in one file in a Different file the you should use static keyword before the declaration

file1.c:

static int number = 63;   // this variable is used only in this file

...

file2.c:

float brightness = 0.5;   // is needed in some Other file;

...

file3.c:

extern float brightness;  // use external declaration to use it here

...

When ever possible, you should use the static variables.

If you want to use two global variables in two different translation units (c files) then your compiler will throw an error saying that the variable is already declared elsewhere.

Using static will make it hidden for other translation units.

Dhruv Vanjari
  • 122
  • 10
1

Global Variable in C by default corresponds to static storage class but has external linkage. Does this mean it is partially static and partially extern?

The English word “static” has muddled and multiple meanings in C, and, yes, the default for a variable declared outside a function is to have static storage duration and external linkage.

Because there are multiple concepts here and some mixed use of word meanings, we should clarify some terminology and formatting:

  • Use code style to refer to specific text in source code, such as the keyword static. When speaking of static storage duration or external linkage, “static” and “external” are mere English adjectives and should not be in code style.
  • “Global” means visible throughout an entire program. The C standard does not use this word for this purpose. It uses “external” to refer to things that are outside of (external to) any function. (But it also uses “external” for other purposes.) A global variable could not have internal linkage, because it would not be visible throughout the entire program.
  • A variable consists of an object (memory reserved for representing the value) and an identifier (the name). Storage duration is a property of the object. Linkage is a property of the identifier.

The English word “static” generally means unchanging. The C standard uses this word and the keyword static in multiple ways:

  • Static storage duration means the memory for an object is reserved throughout all of program execution.
  • Using the static keyword in a declaration, other than as below, both gives an object static storage duration and, in a declaration outside a function, gives the identifier internal linkage.
  • Using the static keyword inside subscript markers in a parameter declaration, as in void foo(int a[static 3]), indicates that the parameter points to at least the stated number of elements.
  • Static assertions, using _Static_assert, provide compile-time tests (which can help detect bugs or ensure a program is being compiled with expected settings).

These multiple uses are unfortunate and are due at least partly to the history of how the C language was developed.

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
0

You mix up static and static storage duration.

Storage duration and linkage are different terms. There is nothing in C called "global", though the term is often sloppily used for any variable declared at file scope - that is: outside any function. More correct use of the term "global" would be that the variable has external linkage, meaning it may be referred to anywhere in the whole project.

All variables declared at file scope have static storage duration. This dictates how such variables are initialized and that they persist throughout the whole exeuction of the program (practically, it likely also means that the variable ends up in .data or .bss segments of the implementation).

A variable declared at file scope but without any storage class specifier (neither static nor extern) has external linkage, but it still got static storage duration. From 6.2.4:

If the declaration of an identifier for an object has file scope and no storage-class specifier, its linkage is external.

But if you add a storage class specifier to a file scope variable, you specify both storage duration and linkage. 6.2.4/3:

If the declaration of a file scope identifier for an object or a function contains the storage-class specifier static, the identifier has internal linkage.

And as you say, a variable with internal linkage can only be referred to from the same file (strictly speaking same "translation unit").

There's some subtle difference here in case you add static to a local variable - it then specifies storage duration but not linkage. The whole term linkage is rather confusing - simply put the purpose of the term is to dictate when two variables with the same name refer to the same object or different ones. This is mostly a concern for those making compilers/linkers as well as for spaghetti programmers. Avoid belonging to the latter category by following the advise below.


Beginners need not worry about any of the above. Rules of thumb for beginners:

  • Never declare variables outside functions unless they are static.
  • Never declare variables in header files.
  • Never use extern.
  • Use static in front of functions if they aren't meant to be called from other files.

This will get you very far, and the rare cases where you actually need to share variables between different files is another story/advanced topic.

Lundin
  • 195,001
  • 40
  • 254
  • 396