error: #29: expected an expression in C
Asked Answered
S

2

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??

Shop answered 20/7, 2013 at 5:8 Comment(1)
note: its "error: expected an expression" in embedded CShop
E
4

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

#define READ_TAMPER_PIN()   ((FIO2PIN & PIN_TAMPER) >> 12)
Eolic answered 20/7, 2013 at 5:9 Comment(7)
you mean {} braces? my previous macro #define DIR_TAMPER_IN_PORT() {FIO2DIR &= ~PIN_TAMPER ;} doesent give any such error but?Shop
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.Eolic
then what should I assign it to??Shop
... 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.Eolic
I have defined x as unsigned char x what should I assign it to then??Shop
What does that have to do with anything? unsigned char x is fine.Eolic
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??Shop
B
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.

Bounded answered 20/7, 2013 at 5:19 Comment(7)
What does this answer have to do with OP's question?Eolic
@CarlNorum Just to tell about the proper syntax of macro.Bounded
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.Eolic
Sure you can. Macro replacement is pretty much just text substitution. You can put whatever you want in there.Eolic
let us continue this discussion in chatBounded
What do you mean "everything should be in ()"? There's nothing about macros that requires such a thing.Eolic
@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??Shop

© 2022 - 2024 — McMap. All rights reserved.