JuanPablo JuanPablo - 1 year ago 125
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++)

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

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

Answer Source

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download