Go to Post As a real pig farmer*, I definitely endorse team 1902. - Andy Baker [more]
Home
Go Back   Chief Delphi > Technical > Programming
Team 51   CD-Events   CD-Media   CD-Swap   CD-Spy   FRC-Spy   Unsung FIRST Heroes   WFA
portal register members calendar search Today's Posts Mark Forums Read FAQ rules
VEXpro
The Chief Delphi Forums are sponsored by Innovation First International, Inc.
Team 221 LLC
ADVERTISEMENT

 
Closed Thread
Thread Tools Rate Thread Display Modes
  #1   Spotlight this post!  
Unread 01-24-2008, 12:55 PM
Joohoo's Avatar
Joohoo Joohoo is offline
Registered User
FRC #0340 (G.R.R.)
Team Role: Mentor
 
Join Date: Jun 2005
Rookie Year: 2005
Location: Rochester
Posts: 234
Joohoo is just really niceJoohoo is just really niceJoohoo is just really niceJoohoo is just really niceJoohoo is just really nice
Send a message via AIM to Joohoo Send a message via Yahoo to Joohoo
#define's scope?

hello everyone I am now writing a few routines to test some of my gyro code. I was wondering if anyone knew what the scope of the #define was, I am calling it at the top of my function but I want to use the same name but different numbers inside a different function in the same file. I know I could just rename them and is something I think I might do, but could someone tell me what the scope of the #define command is? is it global to the file? function? inside a conditional? or global to the project?
  #2   Spotlight this post!  
Unread 01-24-2008, 01:04 PM
Andrew Schreiber's Avatar
Andrew Schreiber Andrew Schreiber is offline
Data Nerd
no team (EWCP)
Team Role: Mentor
 
Join Date: Jan 2005
Rookie Year: 2000
Location: Tampa, FL
Posts: 2,561
Andrew Schreiber has a reputation beyond reputeAndrew Schreiber has a reputation beyond reputeAndrew Schreiber has a reputation beyond reputeAndrew Schreiber has a reputation beyond reputeAndrew Schreiber has a reputation beyond reputeAndrew Schreiber has a reputation beyond reputeAndrew Schreiber has a reputation beyond reputeAndrew Schreiber has a reputation beyond reputeAndrew Schreiber has a reputation beyond reputeAndrew Schreiber has a reputation beyond reputeAndrew Schreiber has a reputation beyond repute
Send a message via AIM to Andrew Schreiber Send a message via Skype™ to Andrew Schreiber
Re: #define's scope?

#define is a preprocessor command, the scope is through whatever file(s) you include it in. Least that is how it works if you have them in .h files. If your #define is in a .c file it is global to the file. Hope this helps.
  #3   Spotlight this post!  
Unread 01-24-2008, 01:21 PM
wt200999's Avatar
wt200999 wt200999 is offline
Registered User
AKA: Will
FRC #0870 (R.I.C.E.)
Team Role: Mentor
 
Join Date: Mar 2006
Rookie Year: 2005
Location: Long Island, New York
Posts: 184
wt200999 is an unknown quantity at this point
Send a message via ICQ to wt200999 Send a message via AIM to wt200999 Send a message via MSN to wt200999 Send a message via Yahoo to wt200999 Send a message via Skype™ to wt200999
Re: #define's scope?

If you want to use different values I would suggest just using a global variable. What #define does is when you compile your code the compiler replaces wherever you have what your define is with its value.

example

Code:
#define VARIABLE rc_dig_in01;


printf("%d",VARIABLE);

will look like this after you compile it

Code:
printf("%d",rc_dig_in01);
  #4   Spotlight this post!  
Unread 01-24-2008, 01:47 PM
Salik Syed Salik Syed is offline
Registered User
FRC #0701 (RoboVikes)
Team Role: Alumni
 
Join Date: Jan 2003
Rookie Year: 2001
Location: Stanford CA.
Posts: 514
Salik Syed has much to be proud ofSalik Syed has much to be proud ofSalik Syed has much to be proud ofSalik Syed has much to be proud ofSalik Syed has much to be proud ofSalik Syed has much to be proud ofSalik Syed has much to be proud ofSalik Syed has much to be proud ofSalik Syed has much to be proud of
Send a message via AIM to Salik Syed
Re: #define's scope?

Actually #define works from the place where it was defined to any other files being processed afterwards.

This is why you typically have the following statement in a header file:
#ifndef _GYRO_H
#define _GYRO_H
{code}
#endif

What this does is tell the pre processor to only compile each header file once... so the preprocessor keeps the #define in an internal lookup table while processing files.

If you are not familiar with how compiling works here is a brief overview:
First the pre processor goes over all the files before compile time and creates a new file which conforms to what is told using #statements .... (i.e replace all instances of VARIABLE with rc_dig_in01)

You then typically have a main program, this main program may use additional functionality from other files, in order to access variables and functions from another file it uses a "header" the header is just a list of what is available.

This header is then connected to an actual C file which contains the functionality.
When the compiler compiles a single C file (whether it is main, or another file) it creates an intermediate .o file which contains assembly instructions and information for the linker on where to find functions that are referenced that are in another C file.

What the preprocessor directive i mentioned above does is prevent the same header from being re-included twice (and thus re-introducing the same variable and function names creating a conflict)
__________________
Team 701
  #5   Spotlight this post!  
Unread 01-24-2008, 03:52 PM
Jake M Jake M is offline
void* Jake;
FRC #1178 (DURT)
Team Role: Programmer
 
Join Date: Jan 2006
Rookie Year: 2005
Location: Missouri
Posts: 118
Jake M has a spectacular aura aboutJake M has a spectacular aura about
Re: #define's scope?

Quote:
Originally Posted by Damien1247 View Post
#define is a preprocessor command, the scope is through whatever file(s) you include it in. Least that is how it works if you have them in .h files. If your #define is in a .c file it is global to the file. Hope this helps.
The only thing I would add to this is that it is only global after the line on which it is defined. I suppose this is the same as any other global variable, except global variables have to be defined at the top of the file, or the top of the function. #defines can be used anywhere.
__________________
Code:
void function(void)
 {
  function();
 }
  #6   Spotlight this post!  
Unread 01-24-2008, 04:34 PM
mneary mneary is offline
Registered User
FRC #1759
Team Role: Mentor
 
Join Date: Mar 2006
Rookie Year: 2005
Location: Los Angeles
Posts: 45
mneary is an unknown quantity at this point
Send a message via AIM to mneary
Re: #define's scope?

Quote:
I suppose this is the same as any other global variable, except global variables have to be defined at the top of the file, or the top of the function. #defines can be used anywhere.
#define does not create a variable, code is not generated, memory is not allocated. It is basically a command to the preprocessor to substitute one phrase for another.
  #7   Spotlight this post!  
Unread 01-24-2008, 08:59 PM
Joohoo's Avatar
Joohoo Joohoo is offline
Registered User
FRC #0340 (G.R.R.)
Team Role: Mentor
 
Join Date: Jun 2005
Rookie Year: 2005
Location: Rochester
Posts: 234
Joohoo is just really niceJoohoo is just really niceJoohoo is just really niceJoohoo is just really niceJoohoo is just really nice
Send a message via AIM to Joohoo Send a message via Yahoo to Joohoo
Re: #define's scope?

ok thanks now lets see if anyone knows the answer to this one. If I have a code structure like so.
Quote:
#define VAR 100
{code1}
#define VAR 50
{code2}
Which the compiler doesn't crash on me, i've tried this, in the second segment of code(code2) what does the compiler put in place of VAR. 50 or 100? Is this an effective way to have my cake and eat it too?
  #8   Spotlight this post!  
Unread 01-24-2008, 09:05 PM
wt200999's Avatar
wt200999 wt200999 is offline
Registered User
AKA: Will
FRC #0870 (R.I.C.E.)
Team Role: Mentor
 
Join Date: Mar 2006
Rookie Year: 2005
Location: Long Island, New York
Posts: 184
wt200999 is an unknown quantity at this point
Send a message via ICQ to wt200999 Send a message via AIM to wt200999 Send a message via MSN to wt200999 Send a message via Yahoo to wt200999 Send a message via Skype™ to wt200999
Re: #define's scope?

I don't see why you are using the #Define anyway. If you want to modify it in your code just make it a global variable. If you want one constant in the top and one constant in the bottom then make the #Define a different name.
  #9   Spotlight this post!  
Unread 01-24-2008, 09:25 PM
Joohoo's Avatar
Joohoo Joohoo is offline
Registered User
FRC #0340 (G.R.R.)
Team Role: Mentor
 
Join Date: Jun 2005
Rookie Year: 2005
Location: Rochester
Posts: 234
Joohoo is just really niceJoohoo is just really niceJoohoo is just really niceJoohoo is just really niceJoohoo is just really nice
Send a message via AIM to Joohoo Send a message via Yahoo to Joohoo
Re: #define's scope?

I understand but I just want to know.
  #10   Spotlight this post!  
Unread 01-24-2008, 09:56 PM
Jake M Jake M is offline
void* Jake;
FRC #1178 (DURT)
Team Role: Programmer
 
Join Date: Jan 2006
Rookie Year: 2005
Location: Missouri
Posts: 118
Jake M has a spectacular aura aboutJake M has a spectacular aura about
Re: #define's scope?

What compiler are you using? All the compilers I've ever worked with would generate an error for redefining a pre-processor directive.
__________________
Code:
void function(void)
 {
  function();
 }
  #11   Spotlight this post!  
Unread 01-25-2008, 07:03 AM
Joohoo's Avatar
Joohoo Joohoo is offline
Registered User
FRC #0340 (G.R.R.)
Team Role: Mentor
 
Join Date: Jun 2005
Rookie Year: 2005
Location: Rochester
Posts: 234
Joohoo is just really niceJoohoo is just really niceJoohoo is just really niceJoohoo is just really niceJoohoo is just really nice
Send a message via AIM to Joohoo Send a message via Yahoo to Joohoo
Re: #define's scope?

I am using the norm, mplab 7.2 and c18 2.4. Nothing unusual, if this clarifys any thing I am doing each one in a separate function.
  #12   Spotlight this post!  
Unread 01-25-2008, 11:28 AM
Jake M Jake M is offline
void* Jake;
FRC #1178 (DURT)
Team Role: Programmer
 
Join Date: Jan 2006
Rookie Year: 2005
Location: Missouri
Posts: 118
Jake M has a spectacular aura aboutJake M has a spectacular aura about
Re: #define's scope?

No, the preprocessor doesn't understand functions or anything about the code for that matter. All it understands is text. If both of those functions are in the same file, that should be causing an error. If not, then I don't know. If I had MPLAB in front of me, I'd give it a shot right now. Since I don't, I'd recommend printf-ing the value after each #define statement. That should tell you what you want to know.
__________________
Code:
void function(void)
 {
  function();
 }
  #13   Spotlight this post!  
Unread 01-25-2008, 01:02 PM
wt200999's Avatar
wt200999 wt200999 is offline
Registered User
AKA: Will
FRC #0870 (R.I.C.E.)
Team Role: Mentor
 
Join Date: Mar 2006
Rookie Year: 2005
Location: Long Island, New York
Posts: 184
wt200999 is an unknown quantity at this point
Send a message via ICQ to wt200999 Send a message via AIM to wt200999 Send a message via MSN to wt200999 Send a message via Yahoo to wt200999 Send a message via Skype™ to wt200999
Re: #define's scope?

You can also make cool little macro functions in the preprocessor.

I did a quick test in Dev-C++ and all it does is give me a warning about the two #defines, and it does change the value to the second one. It would probably do the same thing in MPlab
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
Variable Scope billdar WPILib 2 02-11-2007 10:12 PM
camera scope problems xrabohrok Programming 4 01-15-2007 07:48 PM
Camera variable scope question Validius Programming 1 01-28-2006 08:24 AM
Interrupt Handlers and Variable Scope kaszeta Programming 2 02-14-2004 05:30 PM


All times are GMT -5. The time now is 03:12 AM.

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


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Copyright © Delphi and Pontiac Central High School