Topic 4 - Expressions Flashcards

1
Q

What are expressions built from?

A

Variables, constants & operators.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What does the unary operator i = +1 do

j = -i?

A

increase by one, decrease by i

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Do all binary arithmetic operators allow integer and floating point operand mixing?

A

Yes except modulo.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is the type of there result when you mix float and int with binary operators?

A

It always has type float

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What happens when you use / and % operators with integers and true answer is has a decimal point?

A

The answer gets truncated (ie. all numbers after decimal and the decimal itself are dropped ie. truncated)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

The behaviour when / and % are used with negative operands is what? (In C89 and C99)?

A
C89: implementation-defined
C99: 
10%3 > 1
10%-3 > 1
-10 % 3 > -1
-10 % -3 > -1
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is the order precedence?

A

postfix
prefix
unary, * ? %, + -

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is the binary operator associativity?

A

Left associative

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

In many programming languages, an assignment is a statement, however, in C, what is it called?

A

An assignment is an operator, just like +, * etc.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is another name for an assignment statement?

A

A side effect - where the right side is computed and a side-effect is the computed value is assigned to the variable to the left

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is the = operator associativity?

A

Right-associative
ie. i = j = k = 0; is equal to:
i = (j = (j = 0));

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is an advantage of side-effects of assignment statements?

A

They can be chained together:

i = j = k = 0;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What’s an Lvalue?

A

a variable (object stood in computer memory, not a constant or the result of a computation)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Give examples of compound assignment operators:

A

+=, -=, *=, /=, %=

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Operator precedence in compound assignments are like the usual or different?

A

Different:
i = j+k is same as:
i = i
(j+k)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is the order of subexpression evaluations?

A

Undefined. Doesn’t matter

17
Q

Comment on the order of the expression:

c = (b = a + 2) - (a = 1);

A

The effect of executing the second statement is undefined

18
Q

What is the cure to undefined precedence for subexpression evaluation?

A

Separating assignments into series of assignments

19
Q

What is the value of:
i = 2;
j = i * i++;

A

Undefined behaviour; avoid when you can

20
Q

What is happening here:
++i;
Is it useful at all to do this?

A

i is first incremented, then the new value of i is fetched but then discarded.
No, only expressions with side effects are useful.