Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

More information on `({});` in C?

Tags:

c

gcc

macros

I've noticed that sometimes, C macros are written as something like this:

#define foo(bar) ({ ++bar; })

After some experimentation, I've found that:

  1. ({}); will compile, but do nothing. (As expected.)
  2. Leaving the ; off will cause a syntax error. A side effect of this is ensuring that foo() looks like a function in your code. (Although, if you leave the semicolon off, the error isn't very useful for diagnosing the problem!)
  3. return ({}); complains about a void value not being ignored, just like if I had tried to use a void function.

Is this just to make developers add a semicolon to their macros, or does it have another purpose? I've tried Google, but it fails miserably with punctuation. Is there a name for this?

like image 606
Ryan Fox Avatar asked Jan 16 '10 02:01

Ryan Fox


2 Answers

This is a GNU extension called statement expressions.

When declaring macros in standard-C, you often see do...while(0) loops used for similar purposes (ie creating a block scope). A statement expression is superior to the loop hack because it can return a value. If you want to do something similar in standard-C, you'd have to define an additional function and lose the convenience of lexical scoping.

like image 123
Christoph Avatar answered Sep 29 '22 18:09

Christoph


The cool thing about statement expressions (if there is a cool thing) is that the last statement is the result of the expression.

#define foo(bar) ({ ++bar; 3.1415927; })

int i = 0;
float pi = foo(i);
like image 20
Richard Pennington Avatar answered Sep 29 '22 18:09

Richard Pennington