Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

if(a,b,c,d) how does this work? [duplicate]

Tags:

c

if-statement

int main(void)
{
  int a=0, b=20;
  char x=1, y=10;
  if(a,b,x,y)
    printf("bye");
  return 0;
}

How does "if" condition in the above code work work? Would the value of "y" be only considered by "if"?

like image 991
sreejith.virgo Avatar asked Aug 26 '13 08:08

sreejith.virgo


People also ask

How do duplicate keys work?

ON DUPLICATE KEY UPDATE is a MariaDB/MySQL extension to the INSERT statement that, if it finds a duplicate unique or primary key, will instead perform an UPDATE. The row/s affected value is reported as 1 if a row is inserted, and 2 if a row is updated, unless the API's CLIENT_FOUND_ROWS flag is set.

What is the duplicate ratio of a B?

In duplicate ratios, when the ratio p/q is compounded with itself, the resulting ratio which is p²/q² is called as the duplicate ratio. For example, 16/9 is the duplicate ratio of 4/3. Similarly, for the sub-duplicate ratio, √a/√b is the sub-duplicate ratio of a/b or a:b.

What is duplicate function?

Description. duplicated() determines which elements of a vector or data frame are duplicates of elements with smaller subscripts, and returns a logical vector indicating which elements (rows) are duplicates.


3 Answers

Yes, the value of the comma operator is the right operand. Because none of the other operands have side effects, this boils down to if (y).

like image 58
Joey Avatar answered Oct 10 '22 18:10

Joey


From Wikipedia:

In the C and C++ programming languages, the comma operator (represented by the token ,) is a binary operator that evaluates its first operand and discards the result, and then evaluates the second operand and returns this value (and type).

This in effect means that only the final operand is evaluated for truthfulness, the results of the previous operands are discarded.

In if(a,b,x,y) only the truthfulness of y is considered and therefore whatever y has evaluated to will be considered as true/false.

In your case y is equal to 10 which is considered true in C, therefore the if check will also evaluate to true and the if block will be entered.

You might want to consider this very popular question on StackOverflow for its uses (and misuses).

like image 40
Nobilis Avatar answered Oct 10 '22 19:10

Nobilis


,(comma) operator separates the expression.if the multiple values are enclosed in round bracket then the last value in round bracket gets assigned to variable.

e.g a=(x,y,z);
then  a=z;

while if,

a=x,y,z;

then above expression gets evaluated to (a=x);

Please refer this.

like image 27
Rohan Avatar answered Oct 10 '22 18:10

Rohan