Task 1: Expressions
Task
Let a
, b
, c
, and d
be variables of type int
.
- Which of the following character sequences are valid expressions in the sense that they are accepted by a C++ Compiler? Explain your answer.
- a = b = 5
- 1 = a
a + b- a + b = c + d
- a = 2 b
Assume that all the variables have been defined and correctly initialized and that all expressions are completely independent from each other.
- For each of the expressions that you have identified as valid, decide whether the entire expression is an
lvalue
or anrvalue
, and explain your decision. - Determine the values of the expressions that you have identified as valid and explain how these values are obtained.
Solution
# Task 1
---
### Expression 1: `a = b = 5`
The expression is valid and will be accepted by the CPP compiler. It will result with the l-values `a = 5` and `b = 5`. This is due to the fact the the `=` opperation is read from right to left so `b = 5` will run first and `a = b` afterwards.
### Expression 2: `1 = a`
The expression is invalid.
### Expression 3: `++a + b++`
The expression is valid and will be accepted by the CPP compiler. It will result in a r-value since the result is not assigned to a variable.
### Expression 4: `a + b = c + d`
The expression is invalid.
### Expression 5: `a = 2 b`
The expression is valid and will be accepted by the CPP compiler. It will result with the l-value `a = 2 b`. This is valid since a l-value can be a r-value.
Made by JirR02 in Switzerland 🇨🇭