|
|
|
![]() |
|
|||||||
|
||||||||
![]() |
| Thread Tools | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
So, uh.... how do you use global varibles in C? I am a C++ vet, and this primative programming language and rigid syntax requirements are VERY frustrating!!!
Thanx for any help you offer |
|
#2
|
|||||
|
|||||
|
Re: Global Variables, anyone?
I believe that it's either including a header, or using the extern keyword.
I'm the other end of the spectrum: I'm a VB pro, not used to the power and details of C. There's a thread on extern (and probably globals, too). |
|
#3
|
|||||
|
|||||
|
Re: Global Variables, anyone?
The easiest way (in my opinion) is to use the extern keyword. You can do this using two methods:
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! ;)");
|
|
#4
|
||||
|
||||
|
Re: Global Variables, anyone?
technically, any variable declared at the beginning of a file and not within a function is global variable, but only in file-level scope. To make it span across multiple files, declare it in the other files as well but add "extern" before the other definition (remember, exactly ONE definition of the variable should not have an "extern" in front of it)
|
![]() |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Global Constants | Ryan Cumings | Programming | 17 | 08-02-2004 19:05 |
| Simple problem with variables | sear_yoda | Programming | 4 | 05-02-2004 09:12 |
| Help On Coding 2K1 Controller | GregTheGreat | Programming | 9 | 05-12-2003 18:35 |
| Slightly humorous story about global warming & space explor. | archiver | 2001 | 3 | 24-06-2002 04:01 |
| VB Program to monitor robot variables | DanL | Programming | 7 | 15-02-2002 22:35 |