OCCRA
Go to Post Look past the robot. Look past your team. Science and technolgy are studies worth devoting your life too. - KenWittlief [more]
Home
Go Back   Chief Delphi > Technical > Programming
CD-Events   CD-Media   CD-Spy   FRC-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
Closed Thread
Thread Tools Rate Thread Display Modes
  #1   Spotlight this post!  
Unread 02-05-2004, 03:38 PM
Darkman_X000 Darkman_X000 is offline
Registered User
#0612
 
Join Date: Feb 2004
Location: Chantilly, VA
Posts: 8
Darkman_X000 is an unknown quantity at this point
Question Global Variables, anyone?

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   Spotlight this post!  
Unread 02-05-2004, 03:46 PM
Astronouth7303's Avatar
Astronouth7303 Astronouth7303 is offline
An inspired alumnus
AKA: Jamie Bliss
no team (previously 904 (D-cubed) and 1227 (Techno-Gremlins))
Team Role: Alumni
 
Join Date: Jan 2004
Rookie Year: 2004
Location: Grand Rapids, MI
Posts: 2,071
Astronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud of
Send a message via ICQ to Astronouth7303 Send a message via AIM to Astronouth7303 Send a message via MSN to Astronouth7303 Send a message via Yahoo to Astronouth7303
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   Spotlight this post!  
Unread 02-05-2004, 04:44 PM
Adam Shapiro's Avatar
Adam Shapiro Adam Shapiro is offline
Registered User
FRC #0555 (Montclair Robotics)
Team Role: Alumni
 
Join Date: Jan 2002
Rookie Year: 2001
Location: Montclair, NJ
Posts: 401
Adam Shapiro has much to be proud ofAdam Shapiro has much to be proud ofAdam Shapiro has much to be proud ofAdam Shapiro has much to be proud ofAdam Shapiro has much to be proud ofAdam Shapiro has much to be proud ofAdam Shapiro has much to be proud ofAdam Shapiro has much to be proud ofAdam Shapiro has much to be proud of
Send a message via AIM to Adam Shapiro
Re: Global Variables, anyone?

The easiest way (in my opinion) is to use the extern keyword. You can do this using two methods:
  1. Declare the variable in one file and declare it in each additional file in which it must be used with the extern keyword.
  2. 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
  #4   Spotlight this post!  
Unread 02-05-2004, 09:11 PM
deltacoder1020's Avatar
deltacoder1020 deltacoder1020 is offline
Computer Guy
AKA: Dav
#1020 (The Indiana Prank Monkeys)
Team Role: Programmer
 
Join Date: Jan 2004
Location: Muncie, Indiana
Posts: 340
deltacoder1020 has a spectacular aura aboutdeltacoder1020 has a spectacular aura about
Send a message via AIM to deltacoder1020
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)
__________________
Team 1020, the Indiana Prank Monkeys (www.team1020.org)
Closed Thread


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Global Constants Ryan Cumings Programming 17 02-08-2004 06:05 PM
Simple problem with variables sear_yoda Programming 4 02-05-2004 08:12 AM
Help On Coding 2K1 Controller GregTheGreat Programming 9 12-05-2003 05:35 PM
Slightly humorous story about global warming & space explor. archiver 2001 3 06-24-2002 03:01 AM
VB Program to monitor robot variables DanL Programming 7 02-15-2002 09:35 PM


All times are GMT -5. The time now is 08:33 PM.

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi