# Operators And Expressions in C

#### Operators:

There are three general classes of operators: arithmetic, relational and logical and
bitwise.

#### Arithmetic Operators:

As well as the standard arithmetic operators ( + - * /) found in most languages, C
provides some more operators.
Assignment is = i.e. i = 4; ch = `y';

Increment ++, Decrement -- which are more efficient than their long hand equivalents.
For example, X = X + 1 can be written as ++X or as X++. There is however a
difference when they are used in expression.
The ++ and -- operators can be either in post-fixed or pre-fixed. A pre-increment
operation such as ++a, increments the value of a by 1, before a is used for computation,
while a post increment operation such as a++, uses the current value of a in the
calculation and then increments the value of a by 1. Consider the following:
X = 10;
Y = ++X;
In this case, Y will be set to 11 because X is first incremented and then assigned to Y.
However if the code had been written as
X = 10;
Y = X++;
Y would have been set to 10 and then X incremented. In both the cases, X is set to
11; the difference is when it happens.
The % (modulus) operator only works with integers.
Division / is for both integer and float division. So be careful.
The answer to: x = 3 / 2 is 1 even if x is declared a float!!
RULE: If both arguments of / are integer then do integer division. So make sure you do
this. The correct (for division) answer to the above is x = 3.0 / 2 or x= 3 / 2.0 or
(better) x = 3.0 / 2.0.
There is also a convenient shorthand way to express computations in C.
It is very common to have expressions like: i = i + 3 or x = x * (y + 2)
This can written in C (generally) in a shorthand form like this:
We can rewrite i = i + 3 as i += 3
and x = x * (y + 2) as x * = y + 2.
NOTE: that x * = y + 2 means x = x * (y + 2) and NOT x = x * y + 2.

#### Relational Operators:

The relational operators are used to determine the relationship of one quantity to
another. They always return 1 or 0 depending upon the outcome of the test. The
relational operators are as follows:

To test for equality is ==
If the values of x and y, are 1 and 2 respectively then the various expressions and
their results are:

A warning: Beware of using ―='' instead of ―= ='', such as writing accidentally
if (i = j) .....
This is a perfectly LEGAL C statement (syntactically speaking) which copies the value in

"j" into "i", and delivers this value, which will then be interpreted as TRUE if j is non-
zero. This is called assignment by value -- a key feature of C.

Not equals is:!=
Other operators < (less than), > (grater than), <= (less than or equals), >= (greater
than or equals) are as usual.

#### Logical (Comparison) Operators:

Logical operators are usually used with conditional statements. The three basic logical
operators are && for logical AND, || for logical OR and ! for not.
The truth table for the logical operators is shown here using one‘s and zero‘s. (the idea
of true and false under lies the concepts of relational and logical operators). In C true is
any value other than zero, false is zero. Expressions that use relational or logical
operators return zero for false and one for true.

Example:
(i) x == 6 && y == 7
This while expression will be TRUE (1) if both x equals 6 and y equals 7, and FALSE (0)
otherwise.

(ii) x < 5 || x > 8
This whole expression will be T RUE (1) if either x is less than 5 or x is greater than 8
and FALSE (0) other wise.

#### Bit wise Operators:

The bit wise operators of C are summarised in the following table:

The truth table for Bitwise operators AND, OR, and XOR is shown below. The table uses
1 for true and 0 for false.

DO NOT confuse & with &&: & is bit wise AND, && logical AND. Similarly for | and ||.
~ is a unary operator: it only operates on one argument to right of the operator. It finds
1‘s compliment (unary). It translates all the 1 bits into O‘s and all O‘s into 1‘s
Example:
12 = 00001100 ~12 =
11110011 = 243
The shift operators perform appropriate shift by operator on the right to the operator on
the left. The right operator must be positive. The vacated bits are filled with zero ( i.e.
when shift operation takes places any bits shifted off are lost).
Example:
X << 2 shifts the bits in X by 2 places to the left.
So:
if X = 00000010 (binary) or 2 (decimal)
then:
X >>= 2 implies X = 00000000 or 0 (decimal)
Also: if X = 00000010 (binary) or 2 (decimal)
X <<= 2 implies X = 00001000 or 8 (decimal)
Therefore a shift left is equivalent to a multiplication by 2.
Similarly, a shift right is equal to division by 2.

NOTE: Shifting is much faster than actual multiplication (*) or division (/) by 2. So
if you want fast multiplications or division by 2 use shifts.
The bit wise AND operator (&) returns 1 if both the operands are one, otherwise it
returns zero. For example, if y = 29 and z = 83, x = y & z the result is:

0 0 0 1 1 1 0 1 29 in binary
&
0 1 0 1 0 0 1 1 83 in binary
0 0 0 1 0 0 0 1 Result

The bit wise or operator (|) returns 1 if one or more bits have a value of 1, otherwise it
returns zero. For example if, y = 29 and z = 83, x = y | z the result is:

0 0 0 1 1 1 0 1 29 in binary
|
0 1 0 1 0 0 1 1 83 in binary
0 1 0 1 1 1 1 1 Result

The bit wise XOR operator (^) returns 1 if one of the operand is 1 and the other is zero,
otherwise if returns zero. For example, if y = 29 and z = 83, x = y ^ z the result is:

0 0 0 1 1 1 0 1 29 in binary
^
0 1 0 1 0 0 1 1 83 in binary
0 1 0 0 1 1 1 0 Result

#### Conditional Operator:

Conditional expression use the operator symbols question mark (?)
(x > 7) ? 2 : 3
What this says is that if x is greater than 7 then the expression value is 2. Otherwise the
expression value is 3.
In general, the format of a conditional expression
is: a ? b : c
Where, a, b & c can be any C expressions.
Evaluation of this expression begins with the evaluation of the sub-expression ̳a‘. If the

value of ̳a‘ is true then the while condition expression evaluates to the value of the sub-
expression ̳b‘. If the value of ̳a‘ is FALSE then the conditional expression returns the

value of the sub-expression ̳C‘.

#### sizeof Operator:

In situation where you need to incorporate the size of some object into an expression
and also for the code to be portable across different machines the size of unary operator
will be useful. The size of operator computes the size of any object at compile time. This
can be used for dynamic memory allocation.

Usage: sizeof (object)
The object itself can be the name of any sort of variable or the name of a basic type
(like int, float, char etc).
Example:

sizeof (char) = 1
sizeof (int) = 2
sizeof (float) = 4
sizeof (double) = 8

#### Special Operators:

Some of the special operators used in C are listed below. These are reffered
as separators or punctuators.
Ampersand (&)

Comma ( , )

Asterick ( * )

Ellipsis ( ... )

Braces ( { } )

Hash ( # )

Brackets ( [ ] )

Parenthesis ( () )

Colon ( : )

Semicolon ( ; )

Ampersand:
Ampersand (&) also referred as address of operator usually precedes the identifier
name, which indicates the memory allocation (address) of the identifier.
Comma:
Comma ( , ) operator is used to link the related expressions together. Comma used
expressions are linked from left to right and the value of the right most expression is
the value of the combined expression. The comma operator has the lowest precedence
of all operators. For example:
Sum = (x = 12, y = 8, x + y);
The result will be sum = 20.
The comma operator is also used to separate variables during declaration. For example:
int a, b, c;
Asterick:
Asterick ( * ) also referred as an indirection operator usually precedes the identifier
name, which identifies the creation of the pointer operator. It is also used as an unary
operator.
Ellipsis:
Ellipsis ( ... ) are three successive periods with no white space in between them. It is
used in function prototypes to indicate that this function can have any number of
arguments with varying types. For example:
void fun (char c, int n, float f, . . . . )
The above declaration indicates that fun () is a function that takes at least three
arguments, a char, an int and a float in the order specified, but can have any number of
additional arguments of any type.

Hash:
Hash (#) also referred as pound sign is used to indicate preprocessor directives, which is
discussed in detail already.
Parenthesis:
Parenthesis () also referred as function call operator is used to indicate the opening and
closing of function prototypes, function calls, function parameters, etc., Parenthesis are
also used to group expressions, and there by changing the order of evaluation of
expressions.
Semicolon:
Semicolon (;) is a statement terminator. It is used to end a C statement. All valid C
statements must end with a semicolon, which the C compiler interprets as the end of the
statement. For example:
c = a + b;
b = 5;