CS 102 (Fall '03)
[Schedule] [Examples] [Programs] [Notes & Reference] [Syllabus] [Lab & TA] [Tests] [Grades]

Shortcut Operators, Logical Operators, ++ & --, Precedence

Shortcut Operators

Since we so often combine an assignment statement with an arithmetic operation, C gives us shortcut operators to do both at once. So instead of having to write:
x = x + 3;
we can write:
x += 3;	// Same as x = x + 3
This works similarly for the other arithmetic operators, giving us: -=, *=, /=, and %=.

Logical Operators

Logical operators allow us to evaluate combined elements in a statement to yield a value of True (non zero) or False (zero). The logical operators are:
Symbol Used Description
&& AND
|| OR
! NOT

The OR symbol (two bars, also called "bar bar") can be found on your keyboard usually just above the enter key.

For an example of AND, consider the following situation:

int x, y;
scanf("%d %d", &x, &y);
if ( x && y) {
	printf("Was true, both x and y were non-zero. \n");
}

The condition inside the if statement can be formed using a combination of logical operators. Parenthesis can be used to group parts of the boolean (True/False) expression. Whether or not the result is true can be determined using a truth table:

x y x&&y x||y !x
T T T T F
T F F T F
F T F T T
F F F F T

 

Pre and Post increment and decrement

Another common operation is increment or decrementing by one. Instead of writing:

x = x + 1;

we can write:

x++;	// Same as x = x + 
This works similarly for subtraction. The increment (++) or decrement (--) operators can be applied either before or after the variable is used. For instance:
int x=5;
printf("%d", x++);   // prints 5, then x is changed to 6

As opposed to:

int x=5;
printf("%d", ++x);   // x is changed to 6, then prints 6

This works similarly for subtraction.

Precedence

Ambiguity can arise when combining the operators we've seen so far. Parenthesis are recommended for making an expression unambiguous, but what happens, for instance, when we have:

x = 2 + 3 * 4; 

or even worse,

int x=2;
int y=5;
int z = x+++y;
printf("%d %d %d", x,y,z);

Should the "x+++y" be interpreted as "(x++) + y" or as "x + (++y)". Note that in both cases the value 8 will be stored in z, but in "(x++) + y" x is changed, while in "x + (++y)" y is changed instead. Here is a precedence chart (note that we haven't yet seen many of the operators shown below):

Operators                                   Associativity   Description
() [] -> . ++ --                            Left to right   Postfix ++ and --
! ++  --  + - *  &  (type)  sizeof          Right to left   Unary +, -, and *;
                                                            Prefix ++ and --
*  / %                                      Left to right   Multiplicative
+  -                                        Left to right   Binary +, -
<<  >>                                      Left to right   Shift binary numbers
<  <=  >  >=                                Left to right   Relational
==  !=                                      Left to right   Equality / Inequality
&                                           Left to right   Bitwise and
^                                           Left to right   Bitwise xor
|                                           Left to right   Bitwise or
&&                                          Left to right   Logical and
||                                          Left to right   Logical or
?:                                          Left to right   Conditional (ternary)
=  +=  -=  *=  /=  %=  &=  ^=  |=  <<= >>=  Right to left   Assignment
,                                           Left to right   Sequential evaluation

I highly recommend that you use parenthesis to disambiguate your expressions. Take a look at the examples/postfix.c program to see a highly torturous precedence example using - and --..

/* postfix.c
   Show how postfix -- and ++ are done before
   prefix -- and ++
 */

#include <stdio.h>

int main()
{
   int x,y;

   x=5; y=3;
   printf("x = %d, and y = %d \n", x, y);

   x=5; y=3;
   printf("(x--)-y equals: %d, and x is now %d \n", (x--)-y, x);

   x=5; y=3;
   printf("(x-(--y) equals: %d, and x is now %d \n", (x-(--y), x);

   x=5; y=3;
   printf("x---y equals: %d, and x is now %d \n", x---y, x);
}