View previous topic :: View next topic |
Author |
Message |
nazoa
Joined: 09 Feb 2007 Posts: 58
|
#define for multiple statements |
Posted: Fri Feb 09, 2007 7:54 am |
|
|
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
|
|
Posted: Fri Feb 09, 2007 8:46 am |
|
|
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 |
|
|
|
|