CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Beginning C user having trouble using #define with functions

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
FirstSteps



Joined: 09 May 2006
Posts: 7

View user's profile Send private message

Beginning C user having trouble using #define with functions
PostPosted: Tue May 09, 2006 7:14 pm     Reply with quote

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
PostPosted: Tue May 09, 2006 7:21 pm     Reply with quote

hey.. you just put too much
#define ON 1
#define OFF 0
jim
Guest







too much info
PostPosted: Tue May 09, 2006 7:24 pm     Reply with quote

hey.. you just put too much
#define ON 1
#define OFF 0
FirstSteps



Joined: 09 May 2006
Posts: 7

View user's profile Send private message

PostPosted: Tue May 09, 2006 7:54 pm     Reply with quote

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
PostPosted: Tue May 09, 2006 8:15 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Tue May 09, 2006 11:12 pm     Reply with quote

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
Code:
output_low(PIN_C0)

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.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group