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 support@ccsinfo.com

#define for multiple statements

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



Joined: 09 Feb 2007
Posts: 55

View user's profile Send private message

#define for multiple statements
PostPosted: Fri Feb 09, 2007 7:54 am     Reply with quote

Hello,
I am unsure what the correct way is for defining multiple statements with #define. For example if I want to replace the following lines

output_low(PIN_C0);
output_low(PIN_C1);

with a single command called 'Shift', can I do the following;

#define Shift (output_low(PIN_C0), output_high(PIN_C1))

Then, in my code simply use it by typing 'Shift;'

The compiler does not generate an error but I would like to know if
this is the correct way to go about it.

Thanks.
kender



Joined: 09 Aug 2004
Posts: 768
Location: Silicon Valley

View user's profile Send private message Send e-mail Visit poster's website Yahoo Messenger

PostPosted: Fri Feb 09, 2007 8:46 am     Reply with quote

What you are desribing is called a "macro". Here's how you define it

Code:
// empty parentheses are optional, if you are not passing args to your macro
// by convention names of #defines, including macros, are all capitals
// note the semicolons
#define MY_MACRO() printf("hello"); printf("world");


You would call it just as you have described:
Code:
MY_MACRO; // semicolon is optional on this line, because there is a semicolon at the end of the macro itself
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