5

I'm getting error like "expected an statement"

my code is as follows

#define IN_Tamper    0X00001000     /*P2.12 = EINT2*/
#define DIR_IN_Tamper    { FIO2DIR &= ~0X00001000 ; } 

/* main */
DIR_IN_Tamper(); 
if(((IN_Tamper >> 12) & 0x01) == 1)
            BUZZER_ON();
         else
            BUZZER_OFF();   

I'm getting error saying

  1. Expected an statement for DIR_IN_Tamper();

  2. expected a statement for the else part.....

old_timer
  • 69,149
  • 8
  • 89
  • 168
YMJ
  • 154
  • 1
  • 2
  • 7
  • or defined like: `#define DIR_IN_Tamper(FIO2DIR) { FIO2DIR &= ~0X00001000 ; }`; and call like `DIR_IN_Tamper(FIO2DIR);` what is `FIO2DIR` ? – Grijesh Chauhan Jul 12 '13 at 10:29
  • #define DIR_IN_Tamper { FIO2DIR &= ~0X00001000 ; } like this...... – YMJ Jul 12 '13 at 10:31
  • No I mean what is `FIO2DIR` ? if its macro then consider @phihag's answer. If its variable you wants to pass then defined like macro function as I suggested. – Grijesh Chauhan Jul 12 '13 at 10:33
  • FIO2DIR is ARM7's key word... – YMJ Jul 12 '13 at 10:38
  • then Consider @phihag's answer, One think he didn't explain is that in macro definitional `{}` is used because programmer wants to give complete statement (including `;`). If at the time of macro calling, suppose use forgets `;` then it will not cause any error. – Grijesh Chauhan Jul 12 '13 at 10:44
  • 1
    lastly I have removed () from every where n its working fine.. thank you all.. – YMJ Jul 12 '13 at 11:39

5 Answers5

7

The C preprocessor is (at least in the way you use it) just a simple search-and-replace, so you're effectively running

/* main */
{ FIO2DIR &= ~0X00001000 ; } (); 

This doesn't make any sense. Remove the parentheses in the line

DIR_IN_Tamper(); 

For BUZZER_ON and BUZZER_OFF, you want to remove the parentheses as well. If the macro isn't enclosed in curly braces, you also want to add those, like

if(((IN_Tamper >> 12) & 0x01) == 1) {
    BUZZER_ON
} else {
    BUZZER_OFF
}
phihag
  • 278,196
  • 72
  • 453
  • 469
  • for BUZZER_ON/OFF I have used macro – YMJ Jul 12 '13 at 10:40
  • like #define DIR_BUZ() FIO0DIR |= 0x10000000 ; //DIRECTION FOR BUZ PIn P0.28 #define B_BUZ_E(X) {(X==SET)? (FIO0SET |= PINO_BUZ) : (FIO0CLR |= PINO_BUZ) ;} #define BUZZER_ON() { DIR_BUZ(); B_BUZ_E(1); } #define BUZZER_OFF() { DIR_BUZ(); B_BUZ_E(0); } – YMJ Jul 12 '13 at 10:41
2

DIR_IN_Tamper is defined as { FIO2DIR &= ~0X00001000 ; }, therefore when the preprocessor parses your code, this line

DIR_IN_Tamper(); 

is converted into

{ FIO2DIR &= ~0X00001000 ; }()

Which is clearly not correct. Not sure what exactly you're trying to achieve, but removing the parentheses will eliminate the syntax error:

DIR_IN_Tamper

Further to it, I suspect you have similar issues with BUZZER_ON and BUZZER_OFF.

Aleks G
  • 56,435
  • 29
  • 168
  • 265
2

If you want to use DIR_IN_Tamper like a function, you need a function-like macro:

#define DIR_IN_Tamper()    { FIO2DIR &= ~0X00001000 ; } 

Then, a better way to do it is:

#define DIR_IN_Tamper()    do { FIO2DIR &= ~0X00001000 ; } while(0)

... but that's a different story.

undur_gongor
  • 15,657
  • 5
  • 63
  • 75
  • 1
    Yes, it is a macro. But you try to "invoke" it like a function (with `()`). Therefore you need a function-like macro. – undur_gongor Jul 12 '13 at 10:50
  • macro of BUZZER_OFF is as follows- – YMJ Jul 12 '13 at 10:55
  • #define BUZZER_OFF() { DIR_BUZ(); B_BUZ_E(0); } – YMJ Jul 12 '13 at 10:56
  • do I need to remove () or {} here also..?? its not single statement but. – YMJ Jul 12 '13 at 10:57
  • First and foremost, you should add the definitions of the other macros to your question. – undur_gongor Jul 12 '13 at 11:00
  • If BUZZER_OFF is defined like in your comment above (function-like macro), it should just work. Otherwise, you either change the macro definition or "call" the macro without `()`. I rather think it is a similar problem with `DIR_BUZ` and/or `B_BUZ_E`. – undur_gongor Jul 12 '13 at 11:02
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/33351/discussion-between-user2571585-and-undur-gongor) – YMJ Jul 12 '13 at 11:05
1

Single-statement, function-like macros

Please do not use curly braces ({ and }) when defining single-statement macros like DIR_IN_Tamper.

To safely define a function-like macro, simply put your definition between parentheses, like this:

#define DIR_IN_Tamper() (FIO2DIR &= ~0X00001000)

Then, call your macro like this:

DIR_IN_Tamper();

It will behave like a functions which changes the value of FIO2DIR and then returns the changed value:

/* Your macro rewritten as a function.
   The return type should be the type of FIO2DIR */
uint32_t DIR_IN_Tamper()
{
    return (FIO2DIR &= ~0X00001000);
}

Multi-statement, function-like macros

If you ever need to define a multi-statement macro, see this other C FAQ entry.

For example, define BUZZER_OFF as:

#define BUZZER_OFF() do { DIR_BUZ(); B_BUZ_E(0); } while (0)
Danilo Piazzalunga
  • 7,590
  • 5
  • 49
  • 75
0

Macros in C are not functions. DIR_IN_Tamper(); should be DIR_IN_Tamper;.

m0skit0
  • 25,268
  • 11
  • 79
  • 127
  • It is like this #define DIR_IN_Tamper FIO2DIR &= ~0X00001000 & not like u have mentioned.. – YMJ Jul 12 '13 at 10:35
  • Huh? I mean the call. Not the macro definition. Seems you're too much confused about C. You should get a decent C book and read it before writing code. – m0skit0 Jul 12 '13 at 11:12
  • Then make a question about your new error, or say what error it is. – m0skit0 Jul 12 '13 at 11:14
  • code says as follows DIR_IN_Tamper; if(((IN_Tamper >> 12) & 0x01) == 1) BUZZER_ON; else BUZZER_OFF; where in BUZZER_OFF is again a macro defined as #define BUZZER_OFF { DIR_BUZ; B_BUZ_E(0); } since its group of statements I have used { } braces... – YMJ Jul 12 '13 at 11:17
  • Mu advice is that you avoid macros in C, they're more complicated and prone to errors than you might think. [This](http://stackoverflow.com/questions/17043090/why-should-i-avoid-macros-in-c) explains why. – m0skit0 Jul 12 '13 at 11:19
  • @mOskitO never advice any one such negative thing (avoid macros in C) I got the answer when I started solving it myself. I'll share it with you- search for intermediate file in your program folder like if you have got xyz.c then search xyz.i file you will find macro expansion in there check if its there according to your logic or not. correct in program file. – YMJ Jul 20 '13 at 12:33