Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How is this statement evaluated and what is this kind of thing called?

Tags:

c

gcc

#define FOO(val) \
({                                              \
   int b = val;                                 \
   printf("First, let's count to %d\n", val);   \
   for (int i = 1; i <= val; i++) {             \
      printf("%d...\n", i);                     \
   }                                            \
   b++;                                         \
   b;                                           \
})

int main() {
   int a = FOO(6);
   printf("a=%d\n", a);

   a = (4);     // straightforward, everyone should be familiar with this
   a = (4;);    // does not compile
   a = { 4; };  // does not compile
   a = ({ 4;}); // valid, a will equal 4


   return 0;
}

I'm trying to wrap my head around the above example. How and why does the FOO macro work (the last statement appears to act as a return value)? As far as syntax goes, what are the individual parts called and what other rules apply?

like image 529
josec Avatar asked Jun 17 '11 15:06

josec


People also ask

What is it called when you evaluate something?

Some common synonyms of evaluate are appraise, assess, estimate, rate, and value. While all these words mean "to judge something with respect to its worth or significance," evaluate suggests an attempt to determine relative or intrinsic worth in terms other than monetary.

What is the meaning of evaluate the statement?

The EVALUATE statement operates as if each subject and object were evaluated and assigned a value or range of values. These values may be numeric, nonnumeric, truth values, or ranges of numeric or nonnumeric values.

What type of word is evaluated?

verb (used with object), e·val·u·at·ed, e·val·u·at·ing. to determine or set the value or amount of; appraise: to evaluate property. to judge or determine the significance, worth, or quality of; assess: to evaluate the results of an experiment.

What are the 3 types of evaluation?

The main types of evaluation are process, impact, outcome and summative evaluation.


3 Answers

That's using a GCC non-standard extension to allow return values from code blocks, mostly intended for macros. The last value inside a code block gets treated as a "return value".

A clear example of why this was needed is the usual max macro:

#define max(a,b) (a)<(b)?(b):(a)

Calling max(new A(), new B()) causes 3 objects to be allocated, when in fact you only want 2 (contrived example, but the point is one of the operands gets evaluated twice).

With this extension you can instead write:

#define max(a,b) ({ typeof(a) _a=(a); typeof(b) _b=(b); _a<_b?_b:_a; })

In this case both operands get evaluated exactly once, just like a function, but with all benefits of macros (such as they are).

like image 196
Blindy Avatar answered Sep 28 '22 09:09

Blindy


This is called a statement expression, and is not a part of standard ISO, however GCC supports it.

Quoting from the manual:

A compound statement enclosed in parentheses may appear as an expression in GNU C. This allows you to use loops, switches, and local variables within an expression.

...

The last thing in the compound statement should be an expression followed by a semicolon; the value of this subexpression serves as the value of the entire construct.

like image 23
Michal Kottman Avatar answered Sep 28 '22 09:09

Michal Kottman


Its a GCC extension called statement expressions.

One advice:

Whenever you see such weird syntax, must try compiling it with -pedantic option as well. You will immediately know, if its supported by the language specification or not.

For example, I compiled it with -pedantic option, and it says,

warning: ISO C++ forbids braced-groups within expressions

like image 34
Nawaz Avatar answered Sep 28 '22 08:09

Nawaz