Can some one tell me that global variable are static bydefault or not?

Dani AI

Generated

Short answer: file-scope (what most people call "global") variables are not static by default in the sense of the static keyword. Two different properties are being mixed up in the thread: storage duration (how long the object lives) and linkage (whether other translation units can name the same object). A file‑scope object declared without a storage‑class specifier has static storage duration (it lives for the whole program) and — unless you explicitly use static at file scope — it has external linkage (other translation units can refer to it). (sigbus.info)

Short clarification of terms: storage duration = lifetime (file‑scope objects without _Thread_local have static storage duration); linkage = visibility/matching of the name across translation units (external vs internal); storage‑class keywords affect linkage or duration depending on where they appear (static at file scope gives internal linkage; static at block scope gives a local object static storage duration). This distinction explains the apparent contradiction you saw. (sigbus.info)

Declaration vs. definition: extern is used to declare a name defined elsewhere; int x; at file scope with no initializer is a tentative definition (the standard treats it as a definition with an implicit zero initializer if no other definition is supplied). That is why the common, safe pattern is: put extern declarations in headers and put one real definition (with initializer or a single non‑extern definition) in exactly one .c file. (cigix.me)

Practical takeaway (addresses the original confusion and the helpful points by and ): think in terms of “lifetime” versus “visibility.” Use static at file scope to hide a variable to one file; use extern+single definition to share a variable across files; avoid defining non‑static globals in headers. The C standard sections above are the authoritative reference if deeper precision is needed. (oifans.cn)

Recommended Answers

All 6 Replies

The default is non-static.

But sir ..on google I read it that if no storage class is mentioned than bydefault global variable are of extern linkage.

" If you do not specify a storage class (that is, the extern or static keywords), then by default global variables have external linkage. From the C99 standard"

If you know that all global variables, with no storage class mentioned, are

1) kept until the end of the program.

and

2) available to your program in any function, without using a parameter.

and

3) may be overshadowed or covered, by a local variable with the same name, in that function.

What else is there that you need to know, exactly?

Sir what is troubling me is that ..somewhere it is mentioned that global are static bydefault that means they are available within a file...but somewhere it's mentioned that global variable have external linkage by default..if static is not mentioned.

Sir I'll be very helpful if you make me understand this concept.

The quote you posted does NOT say globals are static by default, it says just the opposite. "external linkage" means that if there are two *.c files, say A.c and B.c, a global variable declared in A.c can be used in B.c. In B.c you declare the variable as extern.

Example use:

A.c

#include <stdio.h>

int MyGlobal = 123;

int main()
{

}

/////////////////////////////////////////

B.c

#include <stdio.h>

extern int MyhGlobal;

void foo()
{
   printf("%d\n", MyGlobal);
}

Thank you so much for helping me so kindly

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.