更新外部變量我有這些文件如何在C
test1.h
extern int value;
void inc_value();
int print_value();
test1.c
#include "test1.h"
int value=0;
void inc_value()
{
printf("inc value from test3.c = %d\n", value++);
}
int print_value()
{
printf(" value in test1.c = %d\n", value);
return value;
}
test3.c
# include "test1.h"
main()
{
inc_value();
}
test4.c
# include <stdio.h>
#include "test1.h"
main()
{
printf("value from test4 = %d\n", print_value());
}
我從test3.c更新變量 「值」,並試圖從test4.c閱讀。然而test3.c無法更新「價值」,也就是在test1.h聲明並在test1.c
我在這裏缺少什麼點定義..
你有兩個'main's? O'rlly? – 2012-03-07 10:00:01
是第一次我調用TEST3更新的價值,後來TEST4打印更新值 – marc 2012-03-07 10:03:18
這是兩個單獨的應用程序?你讓我今天很開心。 – 2012-03-07 10:06:01