"Expected a statement" error in embedded C
Asked Answered
C

5

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

Cutpurse answered 12/7, 2013 at 10:14 Comment(6)
or defined like: #define DIR_IN_Tamper(FIO2DIR) { FIO2DIR &= ~0X00001000 ; }; and call like DIR_IN_Tamper(FIO2DIR); what is FIO2DIR ?Tarragona
#define DIR_IN_Tamper { FIO2DIR &= ~0X00001000 ; } like this......Cutpurse
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.Tarragona
FIO2DIR is ARM7's key word...Cutpurse
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.Tarragona
lastly I have removed () from every where n its working fine.. thank you all..Cutpurse
O
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
}
Olcott answered 12/7, 2013 at 10:17 Comment(2)
for BUZZER_ON/OFF I have used macroCutpurse
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); }Cutpurse
T
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.

Tinatinamou answered 12/7, 2013 at 10:18 Comment(5)
Shouldn't DIR_IN_Tamper rather be defined as a function-like macro?Renatarenate
Maybe it should but I see the question unclear in terms of what he exactly wants to doGreeson
@Renatarenate Maybe it should be, but, as KiaMorot pointed out, it's rather unclear from the question what the OP is trying to do. I showed one way to syntax errors.Tinatinamou
if I remove {} it gives error error: #109: expression must have (pointer-to-) function typeCutpurse
@user2571585: You have to remove the normal parentheses (()), not the curly braces.Renatarenate
R
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.

Renatarenate answered 12/7, 2013 at 10:21 Comment(7)
Yes, it is a macro. But you try to "invoke" it like a function (with ()). Therefore you need a function-like macro.Renatarenate
macro of BUZZER_OFF is as follows-Cutpurse
#define BUZZER_OFF() { DIR_BUZ(); B_BUZ_E(0); }Cutpurse
do I need to remove () or {} here also..?? its not single statement but.Cutpurse
First and foremost, you should add the definitions of the other macros to your question.Renatarenate
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.Renatarenate
let us continue this discussion in chatCutpurse
S
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)
Schubert answered 12/7, 2013 at 10:31 Comment(5)
Armaccess.c(973): error: #109: expression must have (pointer-to-) function typeCutpurse
@user2571585: You have to remove the normal parentheses (()), not the curly braces.Renatarenate
@user2571585 I commented something about {} to your questionTarragona
I have removed () braces from main.. now it looks like BUZZER_OFF; but still error in elseCutpurse
When in doubt, look at the pre-processed source: see https://mcmap.net/q/162838/-how-to-view-c-preprocessor-output/238421.Schubert
E
0

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

Earldom answered 12/7, 2013 at 10:18 Comment(6)
It is like this #define DIR_IN_Tamper FIO2DIR &= ~0X00001000 & not like u have mentioned..Cutpurse
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.Earldom
Then make a question about your new error, or say what error it is.Earldom
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...Cutpurse
Mu advice is that you avoid macros in C, they're more complicated and prone to errors than you might think. This explains why.Earldom
@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.Cutpurse

© 2022 - 2024 — McMap. All rights reserved.