FULL table of operators and precedence — expanded beyond our book's Table 9.1:
Compound Assignment Operators Tweak available for the assignment operator: short-hand for the cases
where the variable on the left is identical with the variable at the beginning
of the right side. C was initially generated as a "mid-level" language to
replace assembly language, a "low-level" langauge. This provides the logic
seen in assembly language for instructions that modify a variable or register by
naming the operation and providing the right-hand side in the expression.
for instance: Prod = Prod * k or Sum = Sum + k. These can be handled by putting one arithmetic operator before the equal sign.
Prod *= k or Sum += k.
Factorial.txt Factorial.c Factorial.exe Not using the compound assignment operators
Factorial_2.txt Factorial_2.c Factorial_2.exe Using the compound assignment operators
More Operators (Ch. 13) Since
incrementing (adding one) and decrementing (subtracting one) are very common
operations, C provides increment and decrement operators. again mimicking
something available in some assembly languages.. They are "unary"
operators, meaning that they have only one operand. They also have the
second-highest precedence. The value of the increment or decrement
expression depends on whether the operator precedes the variable or follows it.
It can only be used on a variable. If the operator comes before the
variable name, the value of the expression is the value of the variable
after it has been incremented or decremented. If the operator
comes after the variable name, the value of the expression is the value of the
variable before it has been incremented or decremented.
Increment.txt Increment.c Increment.exe
Another unary operator is the sizeof operator, which reports the
number of bytes required for its operand, which can either be a data type or a
variable or an array. We saw this briefly 9 January as we were examining
the kinds of variables in C, but here is that code amplified to also show having
a variable as the operand. When we do arrays we will see a further use of
Size.txt Size.c Size.exe
Finally, the operator that can replace a simple if/else structure — and can
lead to confusing code if not used carefully: the conditional operator. Unlike the unary operators, it has a very weak
precedence. it is three expressions separated by '?' and ':'.
<logical expression> ? <first arithmetic expression> : <second arithmetic expression>
If the logical expression evaluates as true, the value of the conditional expression is the first arithmetic expression.
If it evaluates as false, the value of the conditional expression is the second arithmetic expression.
It's precedence is right above that of the assignment operators and the
Conditional.txt Conditional.c Conditional.exe