Content:
Reading: Textbook Chapter 3
Lecture 4:
Lecture 5:
Lecture 4
Most elementary type of expression.
Can be a variable, a constant, or an expression in parenthesis.
E.g. a, b12, price, calc, INT_MAX, SIZE
E.g. 5, 123.98, 'A', "Welcome"
E.g. (2 * 3 + 4), (a = 23 + b * 6)
Formed by operand-operator-operand (infix) combination.
Most common type of expression.
Multiplicative expressions:
E.g. 10 * 15
E.g. 5 / 2 is 2,
E.g. 5.0 / 2 is 2.5.
E.g. 5 % 2 is 1,
E.g. 5 % 3 is 2.
Integer |
Float |
||
Expression |
Value |
Expression |
Value |
3 * 5 |
15 |
3.0 * 5 |
15.0 |
20 / 6 |
3 |
20.0 / 6 |
3.333333 |
20 % 6 |
2 |
N. A. |
N. A. |
Additive expressions:
E.g. 2 + 3 is 5.
E.g. a + 7
E.g. 3 - 2 is 1.
E.g. b - 11
Assignment expressions:
- Operator is =.
- Simple assignment:
E.g. a = 5
E.g. b = x + 1
E.g. i = i + 1
- Evaluates expression on the right side of = and places its value in the variable on the left.
- The value of the whole expression is the value of the expression on the right of the = operator.
- The left operand must be a single variable.
Expression |
x |
y |
Expr. Value |
Expr. result |
x = y + 2 |
10 |
5 |
7 |
x = 7 |
x = x / y |
10 |
5 |
2 |
x = 2 |
x = y % 4 |
10 |
5 |
1 |
x = 1 |
Shorthand notation for simple assignment. Requires left operand be repeated as a part of the right expression.
Five operators: *=, /=, %=, +=, -=.
Compound expression |
Equivalent simple expression |
x *= y |
x = x * y |
x /= y |
x = x / y |
x %= y |
x = x % y |
x += y |
x = x + y |
x -= y |
x = x - y |
Expression |
x |
y |
Expr. Value |
Expr. result |
x *= y |
10 |
5 |
50 |
x = 50 |
x /= y |
10 |
5 |
2 |
x = 2 |
x %= y |
10 |
5 |
0 |
x = 0 |
x += y |
10 |
5 |
15 |
x = 15 |
x -= y |
10 |
5 |
5 |
x = 5 |
One operator and one operand.
E.g. sizeof (int),
E.g. sizeof (-345.23),
E.g. sizeof (x),
the results may vary from computer to computer.
Expression |
Value of a |
Value of expression |
+a |
3 |
3 |
-a |
3 |
-3 |
E.g. ++i means to increment i by 1, the effect is equivalent to i = i + 1, and the value of the expression is i after the increment. i.e. the effect takes place before the evaluation.
The operand of a prefix increment/decrement operator must be a variable, not an expression or a constant.
Expression |
value of a before |
value of a after |
expression value |
++a |
10 |
11 |
11 |
a = a + 1 |
10 |
11 |
11 |
--a |
10 |
9 |
9 |
a = a - 1 |
10 |
9 |
9 |
Operator placed after all operands.
- Postfix increment / decrement:
Expression |
value of a before |
value of a after |
expression value |
a++ |
10 |
11 |
10 |
a = a + 1 |
10 |
11 |
11 |
a-- |
10 |
9 |
10 |
a = a - 1 |
10 |
9 |
9 |
Precedence
Associativity
(((3 * 8) / 4) % 4) * 5
since the operators have the same precedence and have left associativity.
a += (b *= (c -= 5))
since the operators have the same precedence and have right associativity. The above is then expanded to
a = a + (b = b * (c = c – 5)).
Lecture 5
C will automatically convert intermediate values of an expression to the proper type so that the expression can be evaluated. This is called implicit type conversion.
Implicit type conversions will always be made to the more general type of the operands.
The promotion order is: char, short, int, unsigned int, long int, unsigned long int, float, double, long double, the last one being the most general one.
E.g. For an expression with operands of the following types: (short + long) / float, the compiler will first covert the short to long before the addition, and then from long to float before the division. The result will be in float.
Expression |
Intermediate type |
char + float |
float |
int – long |
long |
int * double |
double |
float / long double |
long double |
(short + long) / float |
long then float |
You can convert data from one type to another yourself, using the cast expression operator. This is called explicit type conversion.
E.g. (float) a will convert an integer, a, to a float.
E.g. (float) (x + y) will convert the integer expression into a float value. Note the pair of parentheses.
E.g. (float) a / 10 will convert a to float explicitly and the 10 to float implicitly, resulting in a float result. Note that the result will be different if parentheses are used.
You should always make an explicit type conversion for demotion.
E.g. int i;
float x =2.5;
i = (int) (f*5);
An expression is turned into a statement by placing a semi-colon (;) after it.
The semi-colon (;) is a statement terminator which tells the compiler that the statement is finished.
When a (;) is encountered, any pending side effect (action that will be performed by the expression) is completed, the expression value is discarded, and control is passed to the following statement.
E.g. For the statement a = 2; , the value 2 will be stored in the variable a (side effect of the expression), and the value of the expression (which is 2) will be discarded before going on to the next statement.
E.g. For the statement a++; , if the original value of a is 5, then after executing the statement, the value will be 6, and the value of the expression (which is 5) will be discarded.
E.g. For the statement b; , since there is no side effect, and the value of b will be discarded, this statement has no use at all.
Also known as block.
Unit of code consisting of 0 or more statements.
Allows a group of statements to become one single entity.
Consists of an opening brace ({), an optional declaration and definition section, and an optional statement section, followed by a closing brace (}).
{ /* Local Declarations */ int x; int y; /* Statements */ x=1; y=2; … } |
- All C functions has a compound statement as the function body. We have already came across a compound statement of the main function.
Note that no semi-colon is required after the closing brace.
Predefined Constants
Such as:
#define TAX_RATE 0.15 |
To place a semicolon ":" at the end of the command,
#define TAX_RATE 0.15;
then, for the following statement:
salaryTax = TAX_RATE * income;
the statement after replacement becomes:
salaryTax = 0.15; * income;
Solve the problem in the simplest possible way.
Always make the code easy to read and unambiguous.
Simplify the problem by breaking it down into small, easily understood parts. Blocks of code should be no longer than one screen (about 20 lines).
Use parentheses even when necessary to make sure that no misunderstanding of the precedence and associativity rules will happen.
Make sure that the user understands what the program wants and can interpret the results correctly.
Give enough instructions in the program to users.
- End of Lectures 4 and 5-