Lesson-3 Operators

3.1 INTRODUCTION

C supports a rich set of operators. Operators are used in programs to manipulate data and variables. They usually form a part of the mathematical of logical expressions. C operators are classified into a number of categories. They include:

1. Arithmetic operators

2. Relational operators

3. Logical operators

4. Assignment operators

5. Increment and Decrement operators

6. Conditional operators

7. Bitwise operators

8. Special operators

3.2 ARITHMETIC OPERATORS

C provides all the basic arithmetic operators. The operators +, -, *, / all works the same way as they do in other languages. These operators can operate on any built-in data types in C. Arithmetic operators are listed in Table 3.1

Table 3.1 Arithmetic operators

Operators

Meaning

+

Addition

-

Subtraction

*

Multiplication

/

Division

%

Modulo division

Examples of arithmetic operators are

 a-b             a+b             a*b             

a%b            a/b               -a*b

Here a and b are variables and are known as operands. The modulo division produces the remainder of an integer division. The modulo division operator cannot be used on floating point data. C does not have any operator for exponentiation.

3.2.1Integer Arithmetic

When both the operands in a single arithmetic expression are integers, the expression is called an integer expression , and the operation is called integer arithmetic. In above examples, if a and b are integers, then for a = 13 and b = 5we have the following results:

          a – b = 8               a + b = 21             a * b = 65            

          a % b= 3(remainder of division)                  a / b = 2 (decimal part truncated)                

During modulo division the sign of the result is always the sign of the first operand.

That is:

-14 % 3 = -2

-14 % -3 = -2

14 % -3 = 2

3.2.2 Real Arithmetic

An arithmetic operation involving only real operands is called real arithmetic. If x and y are floats then we will have:

x = 6.0 / 7.0 = 0.857143

y = 1.0 / 3.0 = 0.333333

The operator % cannot be used with real operands.

3.2.3 Mixed-mode Arithmetic

When one of the operands is real and the other is integer, the expression is called a mixed-mode arithmetic expression and its result is always a real number. Thus

 15 / 10.0 = 1.5

 Whereas

15 / 10 = 1

3.3 RELATIONAL OPERATORS

In real world we always compare two or more quantities and depending upon their relation, take certain decisions. For example, we may compare the age of two person or price of two items. These comparisons can be done with the help of relational operators. The expression containing a relational operator is termed as a relational expression. The value of a relational expression is either one or zero. It is one if the specified relation is true and zero if the relation is false. For Example:

          10  <  25 is true

          25  <  10 is false

C Supports six relational operators in all. The operators and their meaning are shown in Table 3.2

Table 3.2 Relational operators

Operators

Meaning

is less than

<=

is less than or equal to

is greater than

>=

is greater than or equal to

= =

is equal to

!=

is not equal to

A simple relational expression contains only one relational operator and takes the following form:

ae1 relational operator ae2

ae1 and ae2 are arithmetic expressions, which may be simple constants, variable or combination of them. Examples of simple relational expression are given below:

          4.9  <=  10           True

          4.9  <    -10                    False

          -34  >=  0             False

          10  <   7+5           True

When arithmetic expressions are used on either side of a relational operator, the arithmetic expression will be evaluated first and then the results compared.

3.4 LOGICAL OPERATORS

C has the following three logical operators.

Table 3.3 Logical operators

Operators

Meaning

&&

logical AND

||

logical OR

!

logical NOT

The logical operators && and || are used when we want tot test more than one conditions and make decisions. An example is:

                   a>b && x = = 10

An expression of this kind, which combines two or more relational expressions, is termed as a logical expression or a compound relational expression. Like the simple relational expression, a logical expression also yields a value of one or zero according to the Table 3.4. The logical expression given above is true only if a>b is true and x = = 10 is true. If either (or both) of them are false, the expression is false.

Table 3.4 Truth table

op1

op2

Value of expression

op1 && op2

op1 || op2

1

1

1

1

1

0

0

1

0

1

0

1

0

0

0

0

Example of logical operators:

          if(age>55 && sal<1000)

          if(number<0 || number>100)

3.5 ASSIGNMENT OPERATORS

Assignment operators are used to assign the results of the expression to the variable. The usual assignment operator is ‘=’. In addition, C has a set of ‘shorthand’ assignment operators of the form

v op = exp;

where v is a variable, exp is an expression and op is a C binary arithmetic operator. The operator op= is known as shorthand assignment operator.

The assignment statement

v op = exp;

is equivalent to

v= v op ( exp ) ;

Consider an example:

x += y+1;

This is same as the statement

x=x+(y+1);

3.5.1 Shorthand Assignment Operators

Table 3.5 Shorthand assignment operators

Shorthand operators

simple assignment operators

a + =1

a = a + 1

a - = 1

a = a – 1

a *= n + 1

a = a * (n+1)

a /= n + 1

a = a / (n+1)

a %= b

a = a % b

3.6 INCREMENT AND DECREMENT OPERATORS

C has two very useful operators that are not generally found in other languages. These are the increment and decrement operator:

++ and --

The operator ++ adds 1 to the operands while – subtracts 1.It takes the following form:

 ++m; or m++;

 --m; or m--;

Use of increment and decrement operators are with for and while loops extensively. While ++m and m++ means the same thing when they form the statements independently, they behave differently when they are used in expression on the right hand side of an assignment statement. Consider the following:

m = 5;

y = ++m;

In this case, the value of y and m would be 6. Suppose, if we write the above statement as

m = 5;

y = m++;

Then, the value of y would be 5 and m would be 6. A prefix (++m) operator first adds 1 to the operand and then the result is assigned to the variable on left. On other hand a postfix (m++) operator first assign the value to the variable on the left and then increment the operand.

3.7 CONDITIONAL OPERATOR

A ternary operator pair “? : ” is available in C to construct conditional expression of the form:

exp1 ? exp2 : exp3;

Here exp1 is evaluated first. If it is true then the expression exp2 is evaluated and becomes the value of the expression. If exp1 is false then exp3 is evaluated and its value becomes the value of the expression. Only one of the expressions (either exp2 or exp3) is evaluated. For example, consider the following statements:

                   a = 12;

                   b = 15;

                   x = ( a >b ) ? a : b ;

In this example, x will be assigned the value of b. this can be achieved using if… else statements as follow:

          if(a > b)

                   x = a;

          else

                   x = b;

3.8 BITWISE OPERATORS

C has distinction on supporting special operators known as bitwise operators for manipulation of data at bit level. These operators are used for testing the bits, or shifting them right or left. Bitwise operators may not be applied to float or double. Table 3.6 lists the bitwise operators and their meaning.

Table 3.6 Bitwise operators

 

Operator

Meaning

&

Bitwise AND

|

Bitwise OR

^

Bitwise XOR

<< 

Shift left

>> 

Shift right

~

One’s complement

3.9 SPECIAL OPERATORSC supports some special operators such as

  • Comma operator

  • Size of operator

3.9.1 The Comma Operator

The comma operator can be used to link the related expressions together. A comma-linked list of expressions are evaluated left to right and the value of right-most expression is the value of the combined expression. For example:

value = (x = 10, y = 5, x + y);

This statement first assigns the value 10 to x, then assigns 5 to y, and finally assigns 15 (i.e,10+5) to value.

3.9.2 The Size of Operator

The size of is a compiler time operator and, when used with an operand, it returns the number of bytes the operand occupies.

Examples:

                                      m = sizeof(sum);

                                      n = sizeof(long int)

                                      k = sizeof(235L)

Last modified: Monday, 28 October 2013, 9:55 AM