main()
{
       int a=10,b=30,c=0;
       if( c =({a+b;b-a;})) 
       {
          printf("%d",c);
       }
}
why the construct ({;}) is legal in C and why it returns the last statement value as the result of the expression ( why it work similar to comma operator)?
Example 1: for loop This will print the 1 (value of i ) on the screen. The update statement ++i is executed. Now, the value of i will be 2. Again, the test expression is evaluated to true, and the body of for loop is executed.
A construct is simply a concept implementation mechanism used by a given programming language - the language's syntax. In your case, the concept here is a loop and its construct is the manner in which it is implemented by the C programming language.
Eg:- int age = 22; // age is a variable of type int and holds the value 22. Here, data_type specifies the type of variable like int, char, etc. variable_name specifies the name of the variable. val is the value for which we are initializing the variable.
Declaring (Creating) Variablestype variableName = value; Where type is one of C types (such as int ), and variableName is the name of the variable (such as x or myName). The equal sign is used to assign a value to the variable.
It is not legal standard C99, but it is a very useful GCC extension, called statement-exprs (a parenthesized brace compound statement ending by some expression).
IIRC, some other compilers support that extension, e.g. Clang/LLVM
Statement expressions are much more useful when they contain control flow changes and side-effects, like:
 c = 2*({while (a>0) a--,b--; a+b;});
However, in your particular case, you could just use the comma operator
 if (c=(a+b,b-a))
Since a+b does not have any side effect, I guess that an optimizing compiler could handle that as
 if (c=b-a)
GCC provides other useful extensions, notably local labels using __label__ and label as values with computed gotos (very useful in threaded interpreters ...). I don't know exactly why they have not been standardized. I wish that they would.
main()
  {
   int a=10,b=30,c=0;
   if( c =({a+b;b-a;})) 
   {
      printf("%d",c);
   }
  }
Here,{a+b;b-a;} is one scope.In this you have written 2 statements.This is actually treated as 
   {
    c=a+b;
    c=b-a;
   }
Initially c value is 40 because of a+b. Again c is modified by b-a. To prove this consider following three cases..
(1).
 if(c=({(a=a+b);;}))
   {
      printf("%d\n",c);
      printf("%d\n",a);
   }
Here o/p is c=40 and a=40;Because at end of scope (i.e) in last statement is dummy (;).
so,c=a+b is o/p.
(2)
   if(c=({(a=a+b);b-a;}))
   {
      printf("%d\n",c);
      printf("%d\n",a);
   }
Here o/p is c=-10, a=40. Because last statement is b-a. this value is assigned to c.
(3)     main()
        {
    int a=10,b=30,c=0;
    if(c=({(a=a+b);0;}))
      {
       printf("%d\n",c);
       printf("%d\n",a);
      }
     printf("%d\n",c);
    }
Here o/p is c=0 only.If is not executed ,Because of last statement is 0;
C follows procedure oriented.And associativity of () is left to right.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With