Message from C, C++ talks

July 2019

— So it's not as powerful as -O2 or -O3

— Well that's what I said

— Yep, just had a mental breakdown e.e

— Exams get me exhausted

— 

*Today's question*

Q)What will be output of the following question

#include <stdio.h>
int main()
{
int i;
if(i=(2,1,0))
printf("welcome ");
else
printf("Hello ");
printf("%d\n",i);
}
a)Hello 3
b)welcome 0
c)Hello 0
d)welcome 3

Hey everyone . I have a doubt in this question . I know precedence of paranthesis is greater than that of equality ,and associativity of paranthesis is from left to right .. that's y 0 gets stored in i. But since 0 is simply assigned in i , how does this makes if condition false?

— Wtf is if(i=(2,1,0))?

— If condition ...

— I=0 is false, because it returns 0, and 0 is the only integral that casts to false

— Can you explain what exactly happens there, why there are 2,1,0?

— Because I don't remember ever seeing such syntax

— I= 0 means assignment of 0 in a variable named i.
Is
int i=0;
if i


Same as
int i=0;

— I=(2,1,0) evaluates to i= 0 bcoz associativity of paranthesis is from left to right .
There is nothing wrong in this syntax

Message permanent page

— Comma operator

— Yep
but you can do
int a,b,c;
a = b = c = 5;
it will execute c = 5, the result will be 5 and it will be assigned to b, and the result of this will again be 5 and then it will be assigned to a.
in your case 0 is assigned and result (again 0) is casted into bool

Message permanent page

— So it's like "execute and forget about it"? in this case 2 and 1 will be just skipped?