开发者

How to make this code compile?

// File: foo.c

static int var;

void foo()
{
var++;
}

// end of file foo.c

// File bar.c:

static int var;

void bar()
{
var+开发者_运维问答+;
}

// end of file bar.c

// file main.c

static int var;
void main()
{
foo();
bar();
printf("%d", var);
}

// end of file main.c

Question: Will the above program compile ? If so what will be the result ?

I tested the code and found it couldn't be compiled. I try to use extern in main.c to use the function foo() and bar() but it still couldn't be compiled.


main.c has a few minor problems - it should be something like this:

#include <stdio.h>

static int var;

extern void foo();
extern void bar();

int main(void)
{
    foo();
    bar();
    printf("%d\n", var);
    return 0;
}

It should build OK like this:

$ gcc -Wall main.c foo.c bar.c -o main

and the result should be:

$ ./main
0


I would expect it to compile and print 0 (though if you want to compile it as C++, you'll have to add declarations for foo() and bar(), and in either C or C++, you might get a warning that main() should really return an int).

Since var is defined as static in each of the three files, you really have three separate variables that all happen to have the same name. Perhaps it's easiest to think of each file as defining a struct that contains its static variables. What you've done is called foo(), which increments foo.var. Then you've called bar(), which increments bar.var. Then you've printed out main.var, which was initialized to zero, and never modified.


This compiles for me (although with a warning about the return type from main()).

The result, in terms of what main() will print, is undetermined because you have not initialized the value of var in main.c. The most likely result when the compiler is invoked without optimizations is zero because the OS will have zeroed the physical memory supplied to the process for data storage (the OS does this to avoid leaking confidential data between processes).

The static qualifier to the var variable definitions means that the variable is not visible outside the source file it is defined in. It also means that each of the three var variables gets its own storage location.

If we add a printf("[module name] *var=%p\n", &var) to foo(), bar() and main() respectively to print the address of the memory location that stores those three variables you should get something like this:-

foo() *var=0x8049620
bar() *var=0x8049624
main() *var=0x8049628

Note that each variable gets its own storage location. The code in each source file will access the version of var that is specific to to that source file. Using static like this in .c files is typically used to implement the concept of information hiding in C.


The code (as it is) will compile and result will be 0 (as Jerry explains) because static variable will have file scope.

But, if you include foo.c and bar.c in main.c, and compile as

gcc main.c

then the result will be 2 because there will only be one global variable var.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜