Topic : C Lessons
Author : Christopher Sawtell
Page : << Previous 9  Next >>
Go to page :

*     Indirection via a Pointer.
-     Arithmetic Negative.
!     Logical Negation or Not.
~     Bit-wise One's Complement.
++    Increment.
--    Decrement.
sizeof  Which is self explanitary.

Now the binary operators:

Arithmetic Operators.

*     Multiply.                                       My
/     Divide.                                         Dear
%     Modulo, or Remainder of Integer Division.
-     Subtraction.                                    Sally

The Shifting Operators.

>>    Bit-wise Shift to the Right.
<<    Bit-wise Shift to the Left.

Logical Relation Operators.

<     Less Than.
>     Greater Than.
<=    Less Than or Equal.
>=    Greater Than or Equal.
==    Equal.
!=    Not Equal.

Bit-wise Boolean Operators.

&     Bit-wise And.
^     Bit-wise Exclusive-or.
|     Bit-wise Or.

The Logical Operators.

&&    Logical And.
||    Logical Or.

The Assignment Operators. ( They all have the same priority. )

=     The normal assignment operator.

The Self-referencing Assignment Operators.

+=
-=
*=
/=
%=
>>=
<<=
&=
^=
|=

Some explanation is in order here. The machine instructions in your
computer include a suit of what are called "immediate operand" instructions.
These instructions have one of the operands in a register and the other
is either part of the instruction word itself ( if it is numerically small
enough to fit ) or is the next word in the address space "immediately" after
the instruction code word. 'C' makes efficient use of this machine feature
by providing the above set of operations each of which translates directly
to its corresponding machine instruction. When the variable in question is a
'register' one, or the optimiser is in use, the compiler output is just
the one "immediate" machine instruction. Efficiency Personified!!!

These two lines will make things clearer.

a = 8;
a += 2;     /* The result is 10 */

The exclusive-or operation is very useful you can toggle any
combination
of bits in the variable using it.

a = 7;
a ^= 2;    /* Now a is 5 */
a ^= 2;    /*  and back to 7. */

Naturally, you can use the other operations in exactly the same way,
I'd like to suggest that you make a utterly simplistic little program
and have a look at the assembler code output of the compiler. Don't be
afraid of the assembler codes - they don't bite - and you will see
what I was on about in the paragraph above.

Historical Note and a couple of Cautions.

In the Oldend Days when 'C' was first written all the self-referencing
operations had the equals symbol and the operand around the other way.
Until quite recently ( unix system V release 3.0 ) the 'C' compiler had a
compatability mode and could cope with the old style syntax.

A sample or test program is probably in order here.

/* ----------------------------------------- */

#include <stdio.h>

char *mes[] =
{
" understands",
" does not understand",
" the old-fashioned self-referencing style."
};

main()
{
int a;

a = 5;
a=-2;
printf ( "%s %s %s\n", mes [ 0 ], mes [ ( a == -2 ) ? 2 : 1 ], mes [ 3
] );
}

/* ----------------------------------------- */

The 'C' compiler issued with unix System V release 3.2 seems to have
( thankfully ) dropped the

Page : << Previous 9  Next >>