JuanPablo JuanPablo - 13 days ago 7
C Question

macro in visual studio 2015: expected an expression

I have this code

// show_time.c
#include <time.h>
#include <stdio.h>

#define _timer() ({((double)(clock()))/CLOCKS_PER_SEC;})

int main() {

int i, j;

double timer = _timer();

for (i = 0; i < 1000; i++)
for (j = 0; j < 10000; j++)
i*j;

printf("%f", _timer() - timer);
return 0;
}


with
gcc
work fine, I get the time. But in Visual Studio 2015,
_timer
is marked and I get the message
expected an expression
,

Answer

I don't know why it works with gcc (now I know after following the duplicate link), but it's really overcomplex. A simpler expression does the same and is obviously more portable.

So drop the curly braces and the semi-colon.

That will work:

#define _timer() ((double)(clock())/CLOCKS_PER_SEC)

or create a real function

double _timer() { return ((double)(clock()))/CLOCKS_PER_SEC; }

but not a mix of the 2 constructs