在cal.h文件中是这样的:
#ifndef CAL_H
#define CAL_H
#define NUMBER '0'
static int test_include = 100;
void push(double);
double pop(void);
int getop(char[]);
int getch(void);
void ungetch(int);
#endif
在stack.c文件中是这样的:
#include "cal.h"
void push(double c)
{
test_include += 20;
}
在getop.c中是这样的:
#include "cal.h"
int getop(char s[])
{
test_include = 58;
return 0;
}
在getch.c中是这样的:
#include "cal.h"
int getch(void)
{
test_include -= 10;
return 0;
}
在main.c中是这样的:
#include "cal.h"
#include <stdio.h>
void main()
{
char s[10];
push(0);
printf("%d\n", test_include);
getch();
printf("%d\n", test_include);
getop(s);
printf("%d\n", test_include);
}
小弟单步调试,被调用的三个函数都能进去,并且在函数里面test_include的值是已经被改变了的。但是输出的结果如下:
小弟在发此贴之前对#include 和static的理解如下:
1、#include指令会将被include进来的文件拷贝一个副本,并且把副本引入。
2、static修饰外部变量,会将外部变量的作用域限制在本文件之内,而将其include的文件访问不到这个变量。
小弟的疑问是:
1、在三个文件的三个函数中都没报错,且在函数里面test_include的值已经被改变,
然而,输出的test_include的值却没变。
2、如果去掉cal.h文件里
static int test_include = 100;的static其它地方不变,那么输出如下
三个文件引入的cal.h不是把副本引入么,但是为何三个函数修改的是同一个变量?
究竟这个#include和static的作用和机制是什么!
请各位前辈指教!
多谢!!