Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C program syntax "({statement;});" is reported as error by keil

Tags:

c

I am trying to define a macro in the format

#define SUM(x,y) ({log_var = x; log_var += y;})

void main(void)
{
    unsigned int log_var;
    SUM(10,20);
}

Compilation of the same by ARMCC throws an error "Expected an expression" but compilation with GCC doesn't throw the error.

Is it the syntax ({<statements>}); is not allowed in ARMCC or is there any other reason for the same ?

The same disappears when the parentheses is removed. i.e {<statements>}

like image 711
user3609184 Avatar asked Apr 01 '26 09:04

user3609184


1 Answers

If you want to have a multi-statement macro body, the usual way is to have a one-iteration do while loop:

#define SUM(x,y) do {log_var = x; log_var += y;} while (0)
like image 199
Some programmer dude Avatar answered Apr 04 '26 00:04

Some programmer dude