<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:

OperatorExampleMeaning
*x * yMultiply x and y
/x / yDivide x by y
%x % yThe remainder after dividing x by y
+x + yAdd x and y
-x - ySubtract y from x
++xValue of x
--xArithmetic negative of x
++++xIncrease x by 1 before using x
----xDecrease 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
  5. Addition and subtraction 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:

OperatorExampleMeaning
=x = yAssign the value of y to the variable x
*=x *= yAssign the product of x multiplied by y to the variable x
/=x /= yAssign the quotient of x divided by y to the variable x
+=x += yAssign the sum of x and y to the variable x
-=x -= yAssign 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:

OperatorExampleMeaning
<x < y1 if x is less than y
<=x <= y1 if x is less than or equal to y
>x > y1 if x is greater than y
>=x >= y1 if x is greater than or equal to y
==x == y1 if x is equal to y
!=x != y1 if x is not equal to y



Errors often happen with the use of the equal signs:



Logic operators interpret values differently:

OperatorExampleMeaning
&&x && y1 if the values of x and y are both not 0
||x || y1 if the values of x or y are not 0
!!x1 if the value of x is equal to zero.



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

OperatorExampleMeaning
&&xThe address in memory for variable x
**xThe value of the memory at address x
sizeofsizeof( x )The number of bytes in memory occupied by variable x
(type)(type) xThe value of variable x interpreted or cast as a different data type