1. Home >
  2. Apps >
  3. Groups >

Variation in the working of the comma operator in the below codes

Question asked by radha gogia in #Coffee Room on Feb 23, 2015
radha gogia
radha gogia · Feb 23, 2015
Rank C3 - EXPERT
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"))😛rintf("hello");
printf("%d",a);
return 0;
}
Case4:
int main()
{
int a;
a=10>8?printf("hello"),printf("joke")😛rintf("hello");
printf("%d",a);
return 0;
}
I am unable to catch out the reason that when I use paranthesis in case 2,then I get the output as hellojoke4,while without using parantheis 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 paranthesis or without using paranthesis,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 paranthesis affects the working of the comma ,does it act like a separator or as an operator. Posted in: #Coffee Room

You must log-in or sign-up to reply to this post.

Click to Log-In or Sign-Up