This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

if condition

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

Parents
  • 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.

Reply
  • 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.

Children