We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
Hello, I was compiling my firmware program written in C using Keil microVision2 V2.4 compiler. The target is c8051F120 microcontroller.
I noticed that the compiler accepted the following: if (A == B, 1) {;}
Does anyone know why? I would think that a syntatically error is committed here.
Thanks in advance.
HL
Does anyone know why?
Yes !
Because it's perfectly valid C.
Comma is the "sequential evaluation operator" in C. It allows several expressions in places where only one of them would be expected (e.g. in an if, for, while or similar). The value of the sequential evaluation operator is equal to that of the right-most expression, in your case, "1".
No errors. It's perfectly valid syntax.
Check your C reference for the comma operator. 'A==B' is evaluated first and the result discarded, then '1' is evaluated as TRUE for the result of the 'if' expression.
OPINION!!, nothing else. [inserted for the benefit of those who are more interested in my language that facts]
Another example of the obscure "syntactical allowances" built into C to guarantee that, if you so decide, you can write C than 'nobody' can understand.
The comma operator, of course, has it's uses, but allowing it where "the result is discarded" is nothing but an obfusciation allowance.
Erik
"The comma operator, of course, has it's (sic) uses, but allowing it where "the result is discarded" is nothing but an obfusciation allowance."
The whole point of the comma operator is precisely that it allows you to discard the result of the left-hand operand!
The place where it can be useful is where the expression has side-effects...
As neither expression in the example given has side effects, would some compilers give a "Code with no effect" warning here?
...how did you come to write
if (A == B, 1) {;}
in the first place?
Was it just a typo?
It is actually quite useful to have an operator that throws away the return value of an expression. You might be interested in the side effect of an expression, but not the return value.
Note that an expression may just as well be a call to a function.
However, in this case, many compilers would notice that a==b doesn't have a side effect (unless a bad C++ operator override is used) and warn about the meaningless construct.
The comma operator is extremely often used in the first and last part of a for statement:
for (a=1,b=2,c=3; a < c; a++, b=a-c) {}
where we only want side effects, but there are other uses too:
(t = a, a = b, b = t)
will swap a and b, and then return the new b value.
"As neither expression in the example given has side effects, ..."
Without knowing what 'A' and 'B' are, we can't really say that. They could be macros.
yes, it was a typo. Did not even know it was a legitimate syntax in the first place.
i get error in compile
Error compiling what code? What error?
What code?
What error?
Buggy forum?
When I looked at the thread, I could see all posts but my last from 9:03. I assume that I somehow forgot to press Post and write a new message. After I add the new message, I can see my 9:03 message...
Anyone else who have seen this problem?
"i get error in compile"
Yes, it frequently happens to me, too!
OPINION!!, nothing else.
Thank you.
[inserted for the benefit of those who are more interested in my language that facts]
We're more interested in facts than your language or your opinion...
...as your opinion merely highlights your lack of knowledge. But again, thank you for making it clear to all on this occasion that you were not trying to tout your nonsense as fact.
Mr. smoked sardine:
I insert [inserted for the benefit of those who are more interested in my language that facts] to save you from having to spew your bile, but yoiu just had to do so any7how,