6

my code contains

#define READ_TAMPER_PIN()   {((FIO2PIN & PIN_TAMPER) >> 12) ;}

where PIN_TAMPER is again a macro-

 #define PIN_TAMPER     0x00001000;

in one of the header file, and it is called in main() like

x = READ_TAMPER_PIN();  

it gives an error saying "error: #29: expected an expression"

what could be possible mistake that I'm making here??

old_timer
  • 69,149
  • 8
  • 89
  • 168
YMJ
  • 154
  • 1
  • 2
  • 7

2 Answers2

3

The braces and semicolon in your macro are wrong. Use:

#define READ_TAMPER_PIN()   ((FIO2PIN & PIN_TAMPER) >> 12)
Carl Norum
  • 219,201
  • 40
  • 422
  • 469
  • you mean {} braces? my previous macro #define DIR_TAMPER_IN_PORT() {FIO2DIR &= ~PIN_TAMPER ;} doesent give any such error but? – YMJ Jul 20 '13 at 05:18
  • 1. Yes, I mean `{}`. 2. Presumably you didn't try to assign the result of `DIR_TAMPER_IN_PORT()` to a variable. If you do, you'll get the same error. – Carl Norum Jul 20 '13 at 05:21
  • then what should I assign it to?? – YMJ Jul 20 '13 at 05:27
  • ... what are you asking about? My answer will fix the problem you're having. If you have other problems, you should probably post new questions instead of trying to do that in comments here. – Carl Norum Jul 20 '13 at 05:28
  • I have defined x as unsigned char x what should I assign it to then?? – YMJ Jul 20 '13 at 05:37
  • What does that have to do with anything? `unsigned char x` is fine. – Carl Norum Jul 20 '13 at 05:38
  • next I have bunch of macros #define PIN_TAMPER 0x00001000; /*P2.12 */ #define DIR_TAMPER_IN_PORT() {FIO2DIR &= ~PIN_TAMPER ;} /*Direction for Tamper pin P2.12*/ #define READ_TAMPER_PIN() {((FIO2PIN & PIN_TAMPER)>>12) ;} in main() I have declared x = READ_TAMPER_PIN(); the last macro READ_TAMPER_PIN gives me error expected an expression. where I'm making mistake again?? – YMJ Jul 20 '13 at 07:27
1

According to c99 standard (§6.10.3 #10)

A preprocessing directive of the form

# define identifier lparen identifier-listopt ) replacement-list new-line

# define identifier lparen ... ) replacement-list new-line

# define identifier lparen identifier-list , ... ) replacement-list new-line

defines a function-like macro with arguments, similar syntactically to a function call. The parameters are specified by the optional list of identifiers, whose scope extends from their declaration in the identifier list until the new-line character that terminates the #define preprocessing directive. Each subsequent instance of the function-like macro name followed by a ( as the next preprocessing token introduces the sequence of preprocessing tokens that is replaced by the replacement list in the definition (an invocation of the macro). The replaced sequence of preprocessing tokens is terminated by the matching ) preprocessing token, skipping intervening matched pairs of left and right parenthesis preprocessing tokens. Within the sequence of preprocessing tokens making up an invocation of a function-like macro, new-line is considered a normal white-space character.

0decimal0
  • 3,884
  • 2
  • 24
  • 39
  • 1
    What does this answer have to do with OP's question? – Carl Norum Jul 20 '13 at 05:22
  • @CarlNorum Just to tell about the proper syntax of macro. – 0decimal0 Jul 20 '13 at 05:24
  • OP's macro syntax is fine, though. It's just that his use of `{}` doesn't allow the assignment he wants to do to take place. – Carl Norum Jul 20 '13 at 05:24
  • Sure you can. Macro replacement is pretty much just text substitution. You can put whatever you want in there. – Carl Norum Jul 20 '13 at 05:26
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/33818/discussion-between-phifounder-and-carl-norum) – 0decimal0 Jul 20 '13 at 05:28
  • What do you mean "everything should be in ()"? There's nothing about macros that requires such a thing. – Carl Norum Jul 20 '13 at 05:29
  • @Carl next I have bunch of macros #define PIN_TAMPER 0x00001000; /*P2.12 */ #define DIR_TAMPER_IN_PORT() {FIO2DIR &= ~PIN_TAMPER ;} /*Direction for Tamper pin P2.12*/ #define READ_TAMPER_PIN() {((FIO2PIN & PIN_TAMPER)>>12) ;} in main() I have declared x = READ_TAMPER_PIN(); the last macro READ_TAMPER_PIN gives me error expected an expression. where I'm making mistake again?? – YMJ Jul 20 '13 at 09:06