Go to Post FIRST is the only sort of competition that I've seen where somebody can win and the opponents can be genuinely happy for them. - RiceRobotica [more]
Home
Go Back   Chief Delphi > Technical > Programming
CD-Media   CD-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 24-01-2008, 13:55
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: 241
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 24-01-2008, 14:04
Andrew Schreiber Andrew Schreiber is offline
Joining the 900 Meme Team
FRC #0079
 
Join Date: Jan 2005
Rookie Year: 2000
Location: Misplaced Michigander
Posts: 4,068
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
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 24-01-2008, 14:21
wt200999's Avatar
wt200999 wt200999 is offline
Texas Instruments
AKA: Will Toth
FRC #3005 (Robochargers)
Team Role: Mentor
 
Join Date: Mar 2006
Rookie Year: 2004
Location: Dallas, Texas
Posts: 325
wt200999 has much to be proud ofwt200999 has much to be proud ofwt200999 has much to be proud ofwt200999 has much to be proud ofwt200999 has much to be proud ofwt200999 has much to be proud ofwt200999 has much to be proud ofwt200999 has much to be proud ofwt200999 has much to be proud of
Send a message via MSN 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 24-01-2008, 14:47
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 24-01-2008, 16:52
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 24-01-2008, 17:34
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 24-01-2008, 21:59
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: 241
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 24-01-2008, 22:05
wt200999's Avatar
wt200999 wt200999 is offline
Texas Instruments
AKA: Will Toth
FRC #3005 (Robochargers)
Team Role: Mentor
 
Join Date: Mar 2006
Rookie Year: 2004
Location: Dallas, Texas
Posts: 325
wt200999 has much to be proud ofwt200999 has much to be proud ofwt200999 has much to be proud ofwt200999 has much to be proud ofwt200999 has much to be proud ofwt200999 has much to be proud ofwt200999 has much to be proud ofwt200999 has much to be proud ofwt200999 has much to be proud of
Send a message via MSN 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 24-01-2008, 22:25
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: 241
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 24-01-2008, 22:56
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 25-01-2008, 08:03
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: 241
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 25-01-2008, 12:28
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 25-01-2008, 14:02
wt200999's Avatar
wt200999 wt200999 is offline
Texas Instruments
AKA: Will Toth
FRC #3005 (Robochargers)
Team Role: Mentor
 
Join Date: Mar 2006
Rookie Year: 2004
Location: Dallas, Texas
Posts: 325
wt200999 has much to be proud ofwt200999 has much to be proud ofwt200999 has much to be proud ofwt200999 has much to be proud ofwt200999 has much to be proud ofwt200999 has much to be proud ofwt200999 has much to be proud ofwt200999 has much to be proud ofwt200999 has much to be proud of
Send a message via MSN 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
__________________
Programming in LabVIEW? Try VI Snippets!

FIRST LEGO League 2004 - 2005
FRC Team 870 Student 2006 - 2009
FRC Team 3005 Mentor 2013 -
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 Programming 2 11-02-2007 23:12
camera scope problems xrabohrok Programming 4 15-01-2007 20:48
Camera variable scope question Validius Programming 1 28-01-2006 09:24
Interrupt Handlers and Variable Scope kaszeta Programming 2 14-02-2004 18:30


All times are GMT -5. The time now is 22:48.

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


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