static global and global

example:

main.c:
1
2
3
4
5
6
7
8
9
10

static int A;
int B;

int main()
{
  A = 0;
  B = 0;
  return 0;
}


Whats the difference between A and B?
or is static only usefull inside functions?
static makes the variable local to the current translation unit. You can still access B from other translation units, but not A.
so when i define a static int A in (for example) extra.h
and use it in main.c and extra.c it will become 2 seperate things?

So it makes not sense to use static globally inside a .c file?
so when i define a static int A in (for example) extra.h
and use it in main.c and extra.c it will become 2 seperate things?


So it makes not sense to use static globally inside a .c file?

Why would you think that? When you need a global variable only in the current translation unit, make it static.
so when i define a static int A in (for example) extra.h
and use it in main.c and extra.c it will become 2 seperate things?


Yes there will be two separate variables with the same name: one in main.c and other in extra.c

So it makes not sense to use static globally inside a .c file?

No, there is sense because static global variable is visible in any function of the compilation unit.
Last edited on
Why would you think that? When you need a global variable only in the current translation unit, make it static.


because a non static global variable inside a c file is only available inside the current file. So it makes not difference to use static or not...
because a non static global variable inside a c file is only available inside the current file. So it makes not difference to use static or not...

It is invalid statement.
Compare two examples

main.c

1
2
3
int x = 10;

void f() { printf( "x = %d\n", x ); }


extra.c

1
2
3
int x = 20;

void g() { printf( "x = %d\n", x ); }


and

main.c

1
2
3
static int x = 10;

void f() { printf( "x = %d\n", x ); }


extra.c

1
2
3
static int x = 20;

void g() { printf( "x = %d\n", x ); }



In the first example the linker will issue an error or warning (depending on the linker) that the same name was defined twice. In the second example the code will be saccessfully compiled.
Last edited on
If you make it static you avoid name clashes with other static variables and functions with the same name in other translation units.
ah.. tested it and indeed it doesn't work ;)

But i do not really understand why...
I always thought a global variable inside a c file ony was available for the functions inside that c file. So 2 globally defined variables with the same name in 2 different c files are 2 seperate things...(in my view)

when i define a global variable inside extra.c i can't access it from main.c and vice versa .. so why does the linker not make it behave like a static would?
when i define a global variable inside extra.c i can't access it from main.c and vice versa ..

You can. The same rules as with functions and anything else applies: you have to declare it before you can use it. In the case of a global variable: extern int x;
You can. The same rules as with functions and anything else applies: you have to declare it before you can use it. In the case of a global variable: extern int x;


I know this... but in my example i don't use extern... so why do 2 non static variables inside 2 c files not work?
so why do 2 non static variables inside 2 c files not work?

It violates the one definition rule.
According to you, if you have two global variables x in two different translation units and a third one references x with extern int x;, which one of the two is the linker supposed to pick?
i said:
"in my example i don't use extern"
. only 2 variables in 2 c files with the same name...
The name of a global variable is kept in the table of external symbols which a linker uses to check the one definition rule. So if in two or more modules you have global variables with the same name then the linker issue an error if you do not specify that it is the same variable by using extern.
Opposite to global variables names of static variables do not stored in the table of extternal symbols so the linker knows nothing that they exist.
thnx vlad, so it only gives an error because it may be used as extern?
It gives the error because there may not be two global variables with the same name in a program.
i'm sorry, i must be stupid.

But i can understand when 2 variables with the same name in 2 c files without a extern are 2 seperate things... why is the linker not able to detect this?

Or is it only to prevent a future error when extern could be used?
Or is it only to prevent a future error when extern could be used?

More or less. A shared library loaded at runtime could have a reference to the variable, for example.
But it's just how it is. When you don't mark a variable or function as static, it has external linkage and therefore falls under the one definition rule. Chances are that if you have two variables or functions with the same name in different translation units, it's a mistake and you actually just want one. If you don't, you'd have declared them as static.
ok thnx all!

I think i get it now...
Topic archived. No new replies allowed.