lifetime.c
#include <stdio.h>
#include "otherfile.h"
//extern int step; // has no effect
static void hide_exobj()
{
int exobj;
printf("exobj(hide_exobj) address: %p\n",
&exobj);
}
int main(int argc, char *argv[])
{
extern int exobj;
printf("exobj(main) address: %p\n",
&exobj);
hide_exobj();
//printf("step is %d\n", step);
printf("original exobj is %d\n",
exobj);
printf("add exobj by 1\n");
exobj++;
printf("exobj becomes %d\n",
exobj);
inc_exobj();
dec_exobj();
return 0;
}
otherfile.h
#ifndef OTHERFILE_H_
#define OTHERFILE_H_
extern int exobj;
extern void inc_exobj();
extern void dec_exobj();
#endif
otherfile1.c
#include <stdio.h>
int exobj = 0;
//static int exobj; // error
static int step = 1;
extern int step; // has no effect
static void step_exobj()
{
exobj += step;
}
void inc_exobj()
{
step_exobj();
printf("increased exobj: %d\n",
exobj);
}
otherfile2.c
#include <stdio.h>
extern int exobj;
static int step = -1;
static void step_exobj()
{
exobj += step;
}
void dec_exobj()
{
step_exobj();
printf("decreaseed exobj: %d\n",
exobj);
}