I am trying to declare an variable x
in a file demo_f1.c
and use it in two files demo_f2.c
and demo_f3.c
having two functions void f2()
and void f3()
, respectively.
I have a driver program that uses the variable x
and both of these functions, but it gives me an error while I try to compile the driver program.
demo_f1.c
int x=2;
demo_f2.c
#include"C:\TC\BIN\demo_f1.C"
void f2()
{
extern int x;
printf("In f2 x:%d\n",x);
}
demo_f3.c
#include"C:\TC\BIN\demo_f1.C"
void f3()
{
extern int x;
printf("In f3 x:%d\n",x);
}
Driver.c
#include"stdio.h"
#include"conio.h"
#include"C:\TC\BIN\demo_f1.C"
#include"C:\TC\BIN\demo_f2.C"
#include"C:\TC\BIN\demo_f3.C"
void main()
{
clrscr();
printf("In main program,x:%d\n",x);
f2();
f3();
getch();
}
The error:
Compiling C\TC\BIN\Driver.C:
Error C\TC\BIN\DEMO_F1.C 1: Variable 'x' is initialized more than once
Error C\TC\BIN\DEMO_F1.C 1: Variable 'x' is initialized more than once
Why am I getting this error? How can I correct it?