I have this probably silly question concerning the use of ternary expression.
I do understand how it works in its original form. What i wanted to ask is if the code i wrote below is okay to be used on an arduino (or am i ubusing it?). I have tested it on an uno board and it seems to work all right.
anyway here it is.
would normally only execute the last of the commands.
...
Nope. "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); there is a sequence point between these evaluations."
#include <stdio.h>
int a, b, c, d;
int main()
{
printf("Hello, World!\n");
a = 2, b = 3, c = 4, d = 5;
printf("%d %d %d %d \n", a, b, c, d);
return 0;
}
Try it.
I do agree however that the construct is unusual and could cause others to be confused.
My apologies for my misunderstanding, but the real explanation of what happens leaves me even more convinced that use of the ternary operator with multiple dependant statements should not be used even if it works
UKHeliBob:
My apologies for my misunderstanding, but the real explanation of what happens leaves me even more convinced that use of the ternary operator with multiple dependant statements should not be used even if it works
I guess i have to agree with you. I suppose that in terms of programming, it is wrong to be used like that.