Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is there a variation in the working of the comma operator in the below codes?

Consider the following code snippets Case1:

int main() {
    int a;
    a=printf("hello"),printf("joke");
    printf("%d",a);
    return 0;
}

Case2:

int main() {
    int a;
    a=(printf("hello"),printf("joke"));
    printf("%d",a);
    return 0;
}

Case3:

int main() {
    int a;
    a=10>8?(printf("hello"),printf("joke")):printf("hello");
    printf("%d",a);
    return 0;
}

Case4:

int main() {
    int a;
    a=10>8?printf("hello"),printf("joke"):printf("hello");
    printf("%d",a);
    return 0;
}

I am unable to catch out the reason that when I use parenthesis in case 2,then I get the output as hellojoke4,while without using pantheists I get the output as hellojoke5.

As per the output it is when I tried using ternary operator ,then the same expression when executed using parenthesis or without using parenthesis,returns the last output value of printf statement that is hellojoke4 ,so how does altogether the behaviour differs in the case of ternary operator. And how does the presence of parenthesis affects the working of the comma ,does it act like a separator or as an operator

like image 964
RADHA GOGIA Avatar asked Feb 24 '15 08:02

RADHA GOGIA


People also ask

What is the purpose of the comma operator?

The comma operator ( , ) evaluates each of its operands (from left to right) and returns the value of the last operand. This lets you create a compound expression in which multiple expressions are evaluated, with the compound expression's final value being the value of the rightmost of its member expressions.

What does a comma mean in code?

1) Comma as an operator: The comma operator (represented by the token, ) is a binary operator that evaluates its first operand and discards the result, it then evaluates the second operand and returns this value (and type).

What is the purpose of comma operator in C?

The comma operator in c comes with the lowest precedence in the C language. The comma operator is basically a binary operator that initially operates the first available operand, discards the obtained result from it, evaluates the operands present after this, and then returns the result/value accordingly.

How does comma operator work in Python?

On the left-hand side of an assignment, the comma indicates that sequence unpacking should be performed according to the rules you quoted: a will be assigned the first element of the tuple, b the second.


1 Answers

It is all down to the low precedence of the comma operator. Without parentheses, the expression is grouped as

(a=printf("hello")), printf("joke");

So, an assignment to a from the first printf, followed by the second printf. In the second example, the result of the second printf is assigned to a.

To simplify:

a = 1, 2;   // (a = 1), 2; post-condition a==1
a = (1, 2); // a = (1, 2); is equivalent to a = 2; post-condition a==2
like image 124
juanchopanza Avatar answered Sep 19 '22 14:09

juanchopanza