View previous topic :: View next topic |
Author |
Message |
FirstSteps
Joined: 09 May 2006 Posts: 7
|
Beginning C user having trouble using #define with functions |
Posted: Tue May 09, 2006 7:14 pm |
|
|
I'm trying to use a #define pre-processor directive to pass a parameter to a function but the compiler is throwing an error when I use it. Here is a portion of my code relevant to the problem issue. The compiler complains about error 51.
Code: |
#define OFF = 0
#define ON = 1
void power_relays( int on_off)
{
// do stuff here
}
void main( void )
{
while ( TRUE )
{
power_relays( OFF );
delay_ms ( 500);
power_relays( ON );
delay_ms ( 500);
}
}
|
If someone would correct me, I would appreciate it. |
|
|
jim Guest
|
too much info |
Posted: Tue May 09, 2006 7:21 pm |
|
|
hey.. you just put too much
#define ON 1
#define OFF 0 |
|
|
jim Guest
|
too much info |
Posted: Tue May 09, 2006 7:24 pm |
|
|
hey.. you just put too much
#define ON 1
#define OFF 0 |
|
|
FirstSteps
Joined: 09 May 2006 Posts: 7
|
|
Posted: Tue May 09, 2006 7:54 pm |
|
|
Thank you. I see my mistake now. I was trying to use #byte and #bit before and thought the usage was the same for #define. I spent an hour looking over the preprocessor chapter in a "Learning C" type of book and missed it completely. I erroneously thought the assignment operator (?) was required in #define for the CCS compiler because it was for embedded systems.
Neither of my books:
C, A Reference Manual; Harbison & Steele
Practical C Programming; (An O'Reilly "Animal Book")
mention #byte or #bit. They do not seem to be part of the ANSI spec. Are they just in CCS C or are they in other embedded compilers. The CCS manual seems light on a real definition of what they can do for me.
Is the way that I'm using #define the best (more straight-forward and practical) way of doing what I'm doing or would a professional programmer do it differently? |
|
|
jim Guest
|
ya.. that is ok |
Posted: Tue May 09, 2006 8:15 pm |
|
|
ya.. using #define helps the readability of your code and therefore makes is better. Just make sure your not reinventing the wheel.. some like ccs have some things already defined for you in the .h files.. |
|
|
newguy
Joined: 24 Jun 2004 Posts: 1911
|
|
Posted: Tue May 09, 2006 11:12 pm |
|
|
FirstSteps wrote: | Is the way that I'm using #define the best (more straight-forward and practical) way of doing what I'm doing or would a professional programmer do it differently? |
You're doing it right. When you use defines, it makes the job of changing the code super easy. For instance, if you have pin C0 hooked up to drive a relay and use
Code: | output_high(PIN_C0) |
and
in your code to turn the relay on & off, it's okay.......as long as you don't have to change the pin that drives the relay. If you do, you have to search through all of your code (which can easily become several thousand lines very quickly) and change all instances of PIN_C0 to the new pin. If you instead did it this way:
Code: | #define RELAY_DRIVE PIN_C0 |
All you'd have to change is the #define, ONCE, and not worry about all the times you used/addressed that pin. |
|
|
|