Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why exactly this will not compile if (a) b = c, d = e, return;?

I am addicted to "braceless" ifs, like this:

if (a) b++, c++, d = e; 

But one annoying thing is that return cannot be a part of the last part. Intuitively I feel why is that, but can anyone explain in programming language terms why this will not compile?

main() {
    int a, b, c, d, e;
    if (a) b = c, d = e, return;
}

If you care, please also explain why is that designed like that, it seems like a flaw to me. I can understand in C but in C++ it could have been redesigned without major compatibility loss with the existing C code.

Just for comparison: these will compile and do exactly what expected:

while (a < 10) a++, b--, c += 2;

while (a < 10) if (a == 5) half = a, save();
like image 602
exebook Avatar asked Dec 24 '13 18:12

exebook


People also ask

What is compilation error in Java?

Compile-time errors occur when there are syntactical issues present in application code, for example, missing semicolons or parentheses, misspelled keywords or usage of undeclared variables. These syntax errors are detected by the Java compiler at compile-time and an error message is displayed on the screen.

Which lines of code will generate compilation error?

Line 2 will generate a compiler error. C. The code will compile but will give an error at execution time.

How do you clear a Java program error?

The clearError() method of PrintStream Class in Java is used to clear the error state of this PrintStream instance. It clears any error that might have or not happened in the stream. Hence the checkError() method will always return false after this method. Parameters: This method do not accepts any parameter.

Which of the following will compile successfully?

Answer. Answer: here is your answer; system.


3 Answers

The "comma" operator is exactly that, an operator. It's left and right sides must be expressions, and return is not an expression.

To elaborate, the comma operator evaluates its left-hand side first, and discards the value. Then, it evaluates its right-hand side, and the whole comma expression evaluates to the right-hand side's value.

It's similar to this:

template <typename T, typename U>
U operator,(T t, U u)
{
    return u;
}

Therefore, you cannot put anything in a comma expression that is not an expression itself.

If you're looking to simultaneously execute a series of statements and group them together, that's exactly what ; and {} are for. There is no reason to duplicate that behavior in the comma operator.

like image 73
Apples Avatar answered Dec 23 '22 03:12

Apples


It can be done the following way

if (a) return ( b = c, d = e, 0 );

Oe if there is no return expression

if (a) return ( b = c, d = e, ( void )0 );
like image 30
Vlad from Moscow Avatar answered Dec 23 '22 03:12

Vlad from Moscow


It may be open to question whether this answers the question the OP was really asking, but in case anybody cares about why the comma operator was designed the way it was, I think it goes back to BCPL.

In BCPL, you could combine a series of assignments like:

L1 := R1
L2 := R2

...into a single statement (command) like:

L1, L2 := R1, R2

Much like in C and C++, these were executed in order from left to right. Unlike C and C++, this "comma operator" didn't produce a single expression (at least as C uses the term).

BCPL also had a resultis that let you make a block of statements into something almost like a function.

At least to me, it looks like in C, Dennis1 decided decided to sort of combine these two concepts into a single one that was rather simpler: a comma operator that would allow evaluation of a number of expressions in succession, and yield a single result.

Reference: BCPL Reference Manual


  1. I suppose in fairness I should mention the possibility that this decision was actually made by Ken Thomson in the design of B. Little enough documentation on B has survived that it's almost impossible to even guess about that.
like image 30
Jerry Coffin Avatar answered Dec 23 '22 04:12

Jerry Coffin