C Programming:
operators
Arithmetic Operators
An operator is a symbol that tells the compiler to perform specific mathematical
or logical functions.
C allows *= += /= -= operators.
For example: int i = 5; i *= 5; The int i would have the value 25 after the
operation.
The two arithmetic operators that
are used frequently are ++ and --.
You can place these in front or at
the back of variables.
++ specifies incrementing.
-- specifies decrementing.
If the operator is placed in front, it
is prefix but if it is placed behind, it
is postfix.
Prefix means, increment before
any operations are performed,
postfix is increment afterwards.
These are important
considerations when using these
operators.
© Prof Suvendi Rimer, UJ
Assignment Operators
© Prof Suvendi Rimer, UJ
Relational Operators
The relational operators
from mathematics < and
the > are used for
comparing two objects.
There are six possibilities
in C:
<, <=, >, >=, !=, and
==.
The first four are self-
explanatory, the !=
stands for "not equals
to" and == is
"equivalent to".
NOTE: a = b is different from a == b. Most
C compilers will allow both statements to be
used in conditionals like if, but they have
© Prof Suvendi Rimer, UJ
two completely different meanings.
Logical Operators
Logical operators simulate Boolean algebra in C.
Examples of Logical/Boolean operators: &&, ||, &, |, and ^.
For example, && is used to compare two objects with AND: x != 0 && y != 0
Expressions involving logical operators undergo Short-Circuit Evaluation.
Take the above example into consideration. If x != 0 evaluates to false, the
whole statement is false regardless of the outcome of y != 0.
© Prof Suvendi Rimer, UJ
C Operator Precedence
© Prof Suvendi Rimer, UJ
Summary
The "=" character is not an equality.
A statement of the form a=10; should be interpreted as take the numerical
value 10 and store it in a memory location associated with the integer variable
a.
A statement of the form a=a+10; should be interpreted as take the current
value stored in a memory location associated with the integer variable a; add
the numerical value 10 to it and then replace this value in the memory location
associated with a.
To assign a numerical value to the floating point and double precision
variables, we would use the following C statement:
total=0.0;
sum=12.50;
© Prof Suvendi Rimer, UJ