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

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.