|
Re: Global Variables, anyone?
The easiest way (in my opinion) is to use the extern keyword. You can do this using two methods: - Declare the variable in one file and declare it in each additional file in which it must be used with the extern keyword.
- Declare the variable in a file and declare it using the extern keyword in a header file to be included in all files requiring the variable.
I always find that it is easiest to use method two as follows:
Code:
main.c (or another file)
#include "globals.h"
int iGlobalVar;
iGlobalVar=1;
globals.h
extern int iGlobalVar;
usingglobals.c
#include "globals.h"
if(iGlobalVar==1)printf("Awesome! It worked! ;)");
Hope this helps.
__________________
Mentor to Teams 555, 1929, and 2070!
Currently working in hardware design at Cisco.
Cornell University DARPA Urban Challenge - http://www.cornellracing.com
Co-Captain Team 555 - 2003,2004,2005
Trust, Love, and Magic
|