A Little C Primer/C Operators

C supports the following arithmetic operators:

   c = a * b     multiplication
   c = a / b     division
   c = a % b     mod (remainder division)
   c = a + b     addition
   c = a - b     subtraction

It also supports the following useful (if cryptic) "increment" and "decrement" operators:

   ++a           increment
   --a           decrement

These operators can also be expressed as "a++" and "a--". If the only thing that's needed is an increment or decrement, the distinction between the two forms is irrelevant. However, if a variable is being incremented or decremented as a component of some expression, then "++a" means "increment the variable first, then get its value", while "a++" means "get the value of the variable first, then increment it". Failing to remember this distinction can lead to subtle programming errors.

Finally, C supports a set of bitwise operations:

   a = ~a         bit complement
   a = b << c     shift b left by number of bits stored in c
   a = b >> c     shift b right by number of bits stored in c
   a = b & c      b AND c
   a = b ^ c      b XOR c
   a = b | c      b OR c

C allows math operations to be performed in a shortcut fashion:

   operation        shortcut
   _________________________

   a = a * b         a *= b 
   a = a / b         a /= b 
   a = a % b         a %= b 
   a = a + b         a += b 
   a = a - b         a -= b 
   a = a << b        a <<= b 
   a = a >> b        a >>= b 
   a = a & b         a &= b 
   a = a ^ b         a ^= b 
   a = a | b         a |= b 
   _________________________

The C relational operations were discussed in the previous chapter and are repeated here for completeness:

   a == b    equals
   a != b    not equals
   a < b     less than
   a > b     greater than
   a <= b    less than or equals
   a >= b    greater than or equals

These are actually math operations that yield 1 if true and 0 if false. The following operation actually makes syntactic sense:

   a = b * ( b < 2 ) + 10 * ( b >= 2 );

This would give "a" the value "b" if "b" is less than 2, and the value "10" otherwise. This is cute, but not recommended. It's cryptic; may make porting to other languages more difficult; and in this case, it can be done much more effectively with the conditional operator discussed in the previous chapter:

   a = ( b < 2 ) ? b : 10;

This conditional operator is also known as the "triadic" operator.

There are similar logical operators:

   !     logical NOT
   &&    logical AND
   ||    logical OR

Remember that these are logical operations, not bitwise operations—don't confuse "&&" and "||" with "&" and "|". The distinction is that while the bitwise operators perform the operations on a bit-by-bit basis, the logical operations simply assess the values of their operands to be either 0 or 1 (any nonzero operand value evaluates to 1 in such comparisons) and return either a 0 or a 1:

   if(( A == 5 ) && ( B == 10 ))
   {
      ...
   }

Finally, there is a "sizeof" operand that returns the size of a particular operand in bytes:

   int tvar;
   ...
   printf ( "Size = %d\n", sizeof( int ) );

This comes in handy for some mass storage operations. The "sizeof()" function can be given a data type name or the name of a variable, and the variable can be an array, in which case "sizeof()" gives the size of the entire array.

The precedence of these operators in math functions—that is, which ones are evaluated before others—are defined as follows, reading from the highest precedence to the lowest:

   ()     []     ->     .
   !      ~      ++     --     (cast)      &      sizeof   - (minus prefix)
        /      %
   +      -
   <<     >>
   <      <=     >      >=
   ==     !=
   &

   ^
   |
   &&
   ||
   ?:
   =      +=     -=     *=     /=     %=     >>=     <<=     &=
   ^=     |=
   , 

Of course, parentheses can be used to control precedence. If in doubt about the order of evaluation of an expression, add more parentheses. They won't cause any trouble and might save some pain.

Advanced math operations are available as library functions. These will be discussed in a later chapter.