Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ Operator precedence and the return statement

If I do something like return a ? b : c; or return a && a2 && a3;

Could it ever be evaluated as just return a and then the function just returns immediately before evaluating the rest?

like image 850
user2672807 Avatar asked Sep 11 '13 22:09

user2672807


People also ask

What is the precedence of operators in C?

Operator precedence determines which operation is performed first in an expression with more than one operators with different precedence. Operators Associativity is used when two operators of same precedence appear in an expression. Associativity can be either Left to Right or Right to Left.

What is return statement in C?

A return statement ends the execution of a function, and returns control to the calling function. Execution resumes in the calling function at the point immediately following the call. A return statement can return a value to the calling function.

What is the return type of operator '&'?

Java has two operators for performing logical And operations: & and &&. Both combine two Boolean expressions and return true only if both expressions are true.

What is meant by operator precedence and associativity in C?

The precedence of operators in C dictates the order in which the operators will be evolved in an expression. Associativity, on the other hand, defines the order in which the operators of the same precedence will be evaluated in an expression. Also, associativity can occur from either right to left or left to right.


1 Answers

return is a statement, not an expression. So it can never be misinterpreted the way you think.

The statement is always of the form return [some expression]; (and the expression is optional). The expression, if present, is evaluated first, and its value is bound to the return value of the function.

like image 137
Kerrek SB Avatar answered Nov 03 '22 08:11

Kerrek SB