Consider the following partial C code: int i, j, k; i = 1; j = 100; k = (an expr
ID: 3598370 • Letter: C
Question
Consider the following partial C code:
int i, j, k;
i = 1;
j = 100;
k = (an expression using one i, one j, and exactly five +’s);
Here the identifier k is assigned the value of an expression using exactly one copy of i, one copy of j, five copies of + sign, and any number of pairs of parentheses. List as many distinct such expressions as possible (and use parentheses for better readability), and state the values for variables i, j, k after executing this statement:
1. (For example, both i and j change, and change after assignment)
k = (i++) + (j++); afterwards, i = 2, j = 101, k = 101.
2. (Neither i nor j changes)
3. (Only i changes, and changes before assignment)
4. (Only i changes, and changes after assignment)
5. (Both i and j change, and change before assignment)
6. (Any others?)
Explanation / Answer
Please find my answer.
1. (For example, both i and j change, and change after assignment)
k = (i++) + (j++); afterwards, i = 1, j = 100, k = 101.
2. (Neither i nor j changes)
k = +(+i) + (+(+j));i = 2, j = 101, k = 101.
3. (Only i changes, and changes before assignment)
k = ++i + (+(+j));i = 2, j = 100, k = 102.
4. (Only i changes, and changes after assignment)
k = (i++) + (+(+j));i = 1, j = 100, k = 101.
5. (Both i and j change, and change before assignment)
k = ++i + (++j); i = 2, j = 101, k = 103.
6. (Any others?)
k = ++i + (j++); i = 2, j = 100, k = 101.