File scope variables

I am a little bit confused about file scope variables.

As far as I am aware, if I declare a variable as static whilst it is outside
of all functions, then it should have file scope instead of global scope,
and not be seen by any files that have included the file in which it was
included.

So, I've written a test.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/* Begin a.h*/

#include <stdio.h>

static const int i = 0;

void a(void);

/* End a.h */ 

/* Begin a.c */

#include "a.h"

void a(void)
{
    printf("%d\n", i);
}

/* End a.c */

/* Begin b.h */

#include "a.h"

int main(void);

/* End b.h */

/* Begin b.c */

#include "b.h"

int main(void)
{
    a();

    printf("%d\n", i);

    return 1;
}

/* begin b.c */


So, my theory is that this should not compile. It should fall over
because I'm trying to call i in b.c, when, I would think, it shouldn't
be available outside a.h, or at least a.*.

I have written the above in c because that's the language I was
using for the project where I accentually came across this. I compiled
the above with:

 
$ gcc b.c a.c


though for my other project I am using:

1
2
3
4
$ gcc -W -Wformat  -Wno-format-extra-args -Wno-format-zero-length \
-Wformat-nonliteral -Wimplicit -Wmissing-braces -Wparentheses \
-Wreturn-type -Wswitch -Wunknown-pragmas -Wstrict-aliasing -Winline \
-lSDL -lSDL_mixer -g $(file)


How off is my understanding of file scope vars?
Last edited on
Your understanding of file scope variables is good.
But your understanding of the C preprocessor is not ;)

To understand what happened here, you should remember that the #include directives (as well as any other line beginning with #) are processed by the preprocessor, not the compiler. So when you write:
 
#include "a.h" 

in a C file, the preprocessor will just replace this line by the content of the file a.h, which means that the compiler has no clue which part of a C file was actually included from a header file.

So in your example, each time you include a.h, you declare a new, different static variable i.
To illustrate this, you can try to include a.h twice in the same file, and you will get a compile error about i being declared twice.
Or you can try to change the value of i at the beginning of your main() function (e.g. by adding i++ before the call to a()) and see what happens.
Ahh thanks.

Suddenly I think I understand the point of the extern keyword.

So, if I changed my example so that i was declared inside of a.c, not as static and then in b.c I declared i again with the extern keyword, they would both be the same variable. However, if i is declared in a.c as static, then it wont compile because it is looking for something it then can not find. I feel a bit silly.

I guess then that I need to hide those all those variables in .c files instead of .h files.

Thanks.
Topic archived. No new replies allowed.