<http://lib.cnfolio.com/ENG421ExpressionsAndOperators>
Introduction to Algorithms and Programming

## Expressions and operators

An expression is a combination of operators and operands.
1. Unary operators require only 1 operand.
2. Binary operators require 2 operands.
3. There is only one ternary operator in the C language, which is the conditional operator ? : that requires 3 operands.

The C compiler automatically converts the value on the right side of an assignment operation to match the data type of the variable on the left side
.

Arithmetic operators include:

 Operator Example Meaning * x * y Multiply x and y / x / y Divide x by y % x % y The remainder after dividing x by y + x + y Add x and y - x - y Subtract y from x + +x Value of x - -x Arithmetic negative of x ++ ++x Increase x by 1 before using x -- --x Decrease x by 1 before using x ++ x++ Increase x by 1 after using x -- x-- Decrease x by 1 after using x

Evaluation trees show order of precedence

Operators in C arithmetic expressions follow the same order of precedence as normal math expressions.
1. Parentheses are the highest precedence
2. Unary operators, such as ++ or --
3. Positive and negative sign operators, e.g. + or -
4. Multiplication and division operators
6. Assignment operators are the lowest precedence, such as = or *=

Like math expressions, parentheses can be used to indicate specific orders of precedence. The use of extra parentheses in a complex arithmetic expression can make it easier to read and change later.

The result of division using all integer operands is an integer value. If a fractional result is required, then one of the operands must be forced to be float or double type.

Assignment operators include:

 Operator Example Meaning = x = y Assign the value of y to the variable x *= x *= y Assign the product of x multiplied by y to the variable x /= x /= y Assign the quotient of x divided by y to the variable x += x += y Assign the sum of x and y to the variable x -= x -= y Assign the result of subtracting y from x to the variable x

The C compiler automatically converts the value on the right side of an assignment operation to match the data type of the variable on the left side
.

When using combination assignment operators, such as *=, the expression on the right side of the operator is fully evaluated before the assignment operator is applied.

x *= 37 - 15;

is the same as

x *= ( 37 - 15 );

Comparison operators have only 2 possible results:
• Value 1 to mean logical true.
• Value 0 to mean logical false.

 Operator Example Meaning < x < y 1 if x is less than y <= x <= y 1 if x is less than or equal to y > x > y 1 if x is greater than y >= x >= y 1 if x is greater than or equal to y == x == y 1 if x is equal to y != x != y 1 if x is not equal to y

Errors often happen with the use of the equal signs:
• One equal sign, = , is the assignment operator
• Two consecutive equal signs, == , is the equality comparison operator

Logic operators interpret values differently:
• Non-zero value to mean logical true.
• 0 value to mean logical false.

 Operator Example Meaning && x && y 1 if the values of x and y are both not 0 || x || y 1 if the values of x or y are not 0 ! !x 1 if the value of x is equal to zero.

There are also operators related to the use of data variables and memory:

 Operator Example Meaning & &x The address in memory for variable x * *x The value of the memory at address x sizeof sizeof( x ) The number of bytes in memory occupied by variable x (type) (type) x The value of variable x interpreted or cast as a different data type