Unable to understand the reason for exit

I am running a piece of code. But I can not understand the code and the output that it produces.

#include <stdio.h>
int main()  
{ 
  int a, b,c, d;    
  a=3;    
  b=5;    
  c=a,b;    
  d=(a,b);      
  printf("c = %d" ,c);    
  printf("\nd = %d" ,d);    
  return 0;
}  

The output of this program:

c=3
d=5

I don’t understand how the exit is going.

+3
source share
3 answers

Consider the priority of the C-comma operator.

+3
source
  • When you have a comma, the expression evaluates to the correct parameter, therefore d=(a,b);evaluates to d=b.
  • =has a higher comma priority , therefore the expression c=a,b;evaluates to(c=a),b;

It is not part of the answer, but it is worth mentioning that the whole expression c=a,b;evaluates to b, rather than a, for example. if you write d=(c=a,b);, you will get c=aAND d=b;

+9
source

, ++. , , .

As an example, Boost.Spirit uses the comma operator pretty well to implement list initializers for symbol tables. Thus, it makes the following syntax possible:

keywords = "and", "or", "not", "xor";

Please note that due to the priority of the operator, the code (intentionally!) Is identical

(((keywords = "and"), "or"), "not"), "xor";

That is, the first statement is called keyword.operator = ("and"), which returns the proxy object on which the remaining statement is called: s:

keywords.operator =("and").operator ,("or").operator ,("not").operator ,("xor");
+1
source

All Articles