hi All,
This is my first post.. I am trying to understand how #include
works... I wrote a simple multi file project like this in C::B.
///// MyFunc.h ///////////
#ifndef MYFUNC_H_INCLUDED
#define MYFUNC_H_INCLUDED
int Val;
void PrintVal();
#endif // MYFUNC_H_INCLUDED
////////////// MyFunc.c ///////////////
#include <stdio.h>
#include "myfunc.h"
void PrintVal()
{
printf("%d",Val);
}
/////////////////// main.c //////////////////
#include <stdio.h>
#include <stdlib.h>
#include "myfunc.h"
int main()
{
Val = 30;
PrintVal();
return 0;
}
I "think" #include "xyz.pqr" dumbly replaces the line with contents of
file xyz.pqr.. Then it compiles and links.
With this "assumption" there should be a 'int Val' local to both
MyFunc.c and main.c. So whatever modifications i do for 'Val' should
not be reflected in MyFunc.c and PrintVal should print garbage. To my
surprise it prints the value of 'Val' with exact value i have
modified.
Also tell me how "extern" works exactly.. For me, in the above
program, i should declare void PrintVal() as extern i suppose so that
main.c understands it properly.
Thanks in advance.