Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

variable 1 = ({statement 1;statement 2;}) construct in C

Tags:

c

gcc

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)?

like image 761
bare_metal Avatar asked Sep 02 '14 04:09

bare_metal


People also ask

What is an example of loop construct in C?

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.

What is C constructs?

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.

What is variable declaration in C with example?

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.

How do you declare a variable in C?

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.


2 Answers

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.

like image 200
Basile Starynkevitch Avatar answered Oct 13 '22 21:10

Basile Starynkevitch


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.

like image 1
Anbu.Sankar Avatar answered Oct 13 '22 20:10

Anbu.Sankar