0

I am referring some old c code base and unable to understand the part which is in between brackets (...). Surprised how it compiled ? I am just pasting the simplified version of it. Using gcc. Please also refer code at : https://onlinegdb.com/hT7sYnCz9j


#include <stdio.h>


void main()
{
    int i = 10;

    for (i=0; i<2 && (1,1,0); i++)
    {
        printf("Message 1\n");
    }
    
    for (i=0; i<2 && (1,1,1); i++)
    {
        printf("Message 2\n");
    }
    
    printf("Message 3 : %d\n",(0,0,0));
    printf("Message 4 : %d\n",(1,1,1));

    printf("Message 5 : %d\n",(1,0,0));
    printf("Message 6 : %d\n",(1,1,0));

}

output:

Message 2
Message 2
Message 3 : 0
Message 4 : 1
Message 5 : 0
Message 6 : 0

Gowtam
  • 495
  • 2
  • 8
  • 20
  • 1
    in (exp, exp, exp), it just takes whatever is last in the comma separated values. See comma operator for c. I think it's different if it's without parentheses. – Asphodel Dec 29 '21 at 02:30
  • @Asphodel You make is seem like everything except whatever is after the last comma is discarded and that is not entirely correct. Operations with effects can happen inside the group. Ex: `int x = 1; int y = (x++, 99);`. After this, `x = 2` and `y = 99`. – 001 Dec 29 '21 at 02:35
  • sorry, I didn't mean that it is discarded, the expressions will be evaluated, but it won't be taken as value for current line. In the above example, they're just integer literals between the commas, so they're discarded. – Asphodel Dec 29 '21 at 02:47

0 Answers0