Header Scope

main.c

#include <stdio.h>

extern void init_f();
extern int f();
extern void init_g();
extern int g();

int main()
{
    init_f();
        init_g();
        printf("%d\n", f());
        printf("%d\n", g());

        return 0;
}

mod1.c

#include "header.h"

void init_f()
{
        a = 10;
}

int f()
{
        return a+foo();
}

mod2.c

#include "header.h"

void init_g()
{
        a = 5;
}

int g()
{
        return a+foo();
}
static version

there are two copies of a, one in mod1 and the other in mod2, but both are in file scope so there is no collision, so is like function

header.h

static int a;
static int foo(void)
{
        static int c = 1;
        return c++;
}
extern version

there is only one a in header.o

header.h

extern int a;
extern int foo(void);

header.c

int a;
int foo(void)
{
    static int c=1;
    return c++;
}
other version

it seems like extern version..

header.h

int a;
int foo(void);
/*
int foo(void)
{
    static int c = 1;
    return c++;
}
if no header.c,only define here will cause compile error, multiple definition of foo
*/

header.c
int foo(void)
{
    static int c = 1;
    return c++;
}

以上的測試在function方面也一樣
c
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License