CSCD-255
2015-01-12

Expressions, Arithmetic and Logical

For arithmetic expressions involving multiplication, division, addition, and subtraction, all multiplications and division operations are performed before any addition or subtraction operations. In case several expressions of the same level are strung together, they are evaluated from left to right.

For instance, a / b / c is evaluated as ( a / b ) / c. This is really important for division and subtraction operations!

If you want a different grouping, just put expressions in parentheses.
   y1 + x2 – x1  /  x3 – x1  *  y3 – y1  — generates a very different value from
   y1 + (x2 x1) / (x3 x1) * (y3 y1)
It will in fact be evaluated as
   ( ( ( ( y1 + x2 ) – ( x1  /  x3 ) )  – ( x1  *  y3 ) ) – y1 

Arithmetic Expressions:
ExpressionDemo.txt    ExpressionDemo.c
InterpTest.txt    InterpTest.c

FULL table of operators and precedence — expanded beyond our book's Table 9.1:
Precedence.pdf


Logical Expressions

C has the convention that every variable can be treated as a logical value (true or false): if the variable contains a zero, it is false; if it contains a non-zero, it is true.

For a logical expression, if the expression is false its value is zero.  If it is true, its value is one.  In other words a logical expression is 0 or 1.

Comparison Expressions

C has six operators that compare two numerical fields:
>   strictly greater than
>=  greater than or equal
==  equal
!=  not equal
<=  less than or equal
<   strictly less than

>= is the exact opposite of <
<= is the exact opposite of >
!= is the exact opposite of ==

ComparisonDrill.txt    ComparisonDrill.c    ComparisonDrill.exe