Operators & Expressions

 

Operators are symbols to specify the action to be performed on variables or constants or combinations of both. For example, suppose we have two variables say A and B. now what is to be done on these variables is indicated by some operator.

 

A                     +                      B

 

 

Operand          Operator        Operand

 

Categories:-

  1. Arithmetic operators
  2. Relational operators
  3. Logical operators
  4. Assignment operators
  5. Increment and Decrement operators
  6. Conditional operators
  7. Bitwise operators
  8. Other operators

 

  1. Arithmetic operators:-

Arithmetic operations are used for numeric calculations. Say addition, subtraction, multiplication, divide, and module. They are of two types:

(i) Unary arithmetic operator

(ii) Binary arithmetic operators

 

(i) Unary arithmetic operators:

It requires only one operand

E.g.: +x,-y

Here ‘-‘change the sign of the operand y.

(ii) Binary arithmetic operator:

Its require two operands. There are five operators for binary

Arithmetic operators:

Operator                                         Meaning                                  Purpose

+                                               plus                                        addition

–                                                minus                                     subtraction

*                                               asterisk                                  multiplication

/                                               slash                                      division

%                                              module (percent)                   gives the remainder in integer division

 

NOTE: module (%) operator can’t be applied with floating number. There is no exponent operator in C. however there is a library function (pow) to carry out exponent.

 

Eg:

a+b;

a-b;

a/b;                             Here ‘a’ & ‘b’ are two operands

a*b;

a%b;

 

(iii) Integer arithmetic

When both operands are integer then the arithmetic operation with these operands is called integer arithmetic.

Eg:   let us take two variables a&b. The value of a=17, & b=4

Then result of these operation are:

Expression                               Result

a+b                                     17+4=21

a-b                                       17-4=13

a*b                                      17*4=68

a/b                                       17/4=4.5=4( integer decimal not allowed)

a%b                                     17%4=1 remainder after integer division

 

NOTE: after division operation the decimal part will be truncated and result is only integer part of quotient.

After modules operation the result will be remainder part of the integer division.

NOTE: it is necessary for division and modules operation that second operand must be non-zero.

 

Hierarchy of Operations:

Computer scan on expression from left to right and performs only one operation at a time as a human being does. The order in which various operations are performed is termed as hierarchy of operations. Following is the hierarchy for arithmetic operations:

 

 

Order of evaluation                 Arithmetic operations

1                                              * / %

2                                              + –

 

Thus to evaluate an arithmetic expression computer also follows concept similar to BODMAS i.e first bracket, then division, multiplication, addition & subtraction.

 

(iv) Floating Point Arithmetic:

When both the operands are of float type then the arithmetic operations with these operands is called floating point arithmetic.

 

Eg: let us take two variables a&b . the value of a=12.4 and b=3.1 , then the result of these operation are as:

Expression                                           Result

a+b                                                 12.4+3.1=15.5

a-b                                                  12.4-3.1=9.3

a*b                                                 12.4*3.1=38.44

a/b                                                  12.4/3.1=4.0

 

NOTE: the modules operator (%) can’t be used with floating point numbers.

 

 

  1. Relational operator

Relational operators are used to compare two values depending on their relations. It requires two operands.

Expressions that contain the relational operator are called relational expression. If the relation is true then it returns the value 1, if the relation is false then it return the value 0.

These operators are:

Operator                      Meaning                      Example (a=8,b=2)                             Result

<                            less than                      8<2                                                      false

>                            greater than                 8>2                                                      true

<=                           less than or

equal to                       8<=2                                                    false

>=                           greater then or

equal to                       8>=2                                                    true

==                           equal to                       8==2                                                    false

!=                            not equal to                 8!=2                                                     true

 

 

 

 

  1. Logical or Boolean operator:

An expression that combines two or more expression is termed as a logical expression. For combining these expression we use the logical operator. After testing the value of the condition (which is true or false) , its gives the logical stuts(true / false) as a net result.

The operands may be constants, variables and expression. C has three logical operators.

Operator          Meaning                                  Example                                  Result

&&              logical and                               (8<2)&&(8>3)             false

||                 logical or                                (8<2)||(8>3)                             true

!                 logical NOT negation              !(8<2)                                      true

 

 

 

 

Logical AND

When both operands are true , && operands gives result true. But if any of operands is false, it gives value false.

Boolean table :

 

A B C
T T T
T F F
F T F
F F F

 

 

 

Logical OR

When any one of the input or operand is true , then || (OR) operator gives result true .

Boolean table:

A B C
T T T
T F T
F T T
F F T

 

 

Logical NOT

Not gate is also called Compliment gate. Mean ???????????????

 

 

4) Assignment operator

 

A value can be stored in variables with the use of assignment operator.

 

This assignment operator   ”=” is used for assignment statement.

Assignment statement is also used for updating statement.

E.g.:

x=x+5   can also be written as

x+=5

 

Similarly

 

5) Increment/Decrement operator:

 

C has two useful operators increment (++) and decrement (–).These are the unary operators these operate only single operand.

 

The Increment operator (++) increment the value of the variable by 1 and decrement operator (–) decrement the value of the variable by 1.

We can write this as –

++a   or   a=a+1

–a     or   a=a-1

 

These operators are of two types:

1) Prefix increment/decrement

2) Post-fix increment/decrement

 

 

  1. i) Prefix increment/decrement

 

 

In prefix, first the value of variable is incremented / decremented then value of variable is taken for the operation

Let us take the value of a=3

Then the statement b=++a; means first increment the value of a by 1 then b takes the value of a.

Hence   a=4, b=4

 

The statement   b=–a; means first decrement the value of ‘a’ by 1, then b takes the value of “a”

Hence a=3, b=3

 

 

 

  1. ii) Postfix increment/decrement

Here, first the value of variable is taken for operation then value of variable is incremented/decremented.

Eg: a++ ;

a–;

Let us take the value of a=3   then statement b=a++; means list “b” takes the value of “a” then it incremented it by 1.

Hence   a=4   b=3

The statement b=a—means list “b” takes the value of “a” then decrement it by 1

Hence   a=2,b=3

 

 

  • condition operator/ Ternary operator

 

Ternary operator / Conditional Operator (? &:) requires three operand. This is written as-

Eg:

Maximum=a>b? a:b

First the condition a>b is evaluated , if the value is true then the value of “a” is taken by maximum, otherwise value of “b” is given to the maximum.

 

7) Bitwise operator

C has the ability to support the manipulation of data at the bit level. Bitwise operator are used for operations on bits. Bitwise operators are operated on only integer. The bitwise operators are of given below:-

Bitwise operator                                              Meaning

&                                                         bitwise AND

|                                                          bitwise OR

~                                                          one’s complement

<<                                                        left shift

>>                                                        right shift

^                                                         bitwise OR

 

 

 

8) Other operator

Comma operator (,) is used to print two different expression to appear in situations where only one expression would be used. The expressions are separated by the comma operator. Since comma operator has the lowest precedence of all operators. Hence the paratheses are necessary for using this operator. eg:

sum=(a=8, b=7, a+b);

first 8 is assign to the variable a, then 7 is assign to the variable b and after this resultant value a+b=15 is assigned to variable sum.

 

 

 

Operator Precedence:

If the expression has more than if must be necessary to give the precedence to each operator. Sometimes there is problem for evaluating the expression when two operators have some precedence. For solving this problem C has the facility to provide the associativity to each other. Precedence & associativity of each operator are given below:

 

 

 

Precedence order

 

 

operator

 

 

Description

 

 

Associativity

 

 

 

1

 

 

() Function call  

 

Left to right

 

 

[] array expression
-> structure operator
. structure operator
 

2

 

 

 

 

 

 

 

 

 

 

 

+ unary plus  

 

 

 

 

Right to left

 

 

 

 

 

unary minus
++ increment
decrement
! negation
~ one’s compliment
                       & address

 

* value at address
sizeof size in bytes
(type) type cost

 

 

3

 

 

*

Multiplication

 

 

Left to right

/ Division
% modulus
4 + Addition  

Left to right

subtraction
5 << Left shift Left to right

 

>> Right shift
6 < Less than Left to right

 

 

 

 

> Greater than
<= Less than equal to
>= Greater than equal to
7 = = Equal to Left to right
!= Not equal to
8 & Bitwise AND Left to right
9 | Bitwise OR Left to right
10 ^ Bitwise XOR Left to right
11 && Logical AND Left to right
12 || Logical OR Left to right
13 ?: Ternary Right to left
 

 

14

=

*= /= %=

+= -= &=

^= |=

<<= >>=

 

assignment Right to left
15 , Comma Left to right

 

 

 

 

 

 

 

 

 

 

OPERATERS IN C

/EXERCISE/

1.) What do you mean by operator? State various type of operator available in c.

2.) What do you mean by arithmetic operators? State various arithmetic operators in c.

3.) State various relational operators in c.

4.) What do you mean by hi era c h y of operators?

5.) What do mean by logical operator?

6.) Distinguish between prefix and postfix increment operator. Illustrate giving example.

7.) State various assignment op e rat or available in c.

8.) State purpose of conditional operator.

9.) Why we use size of operator.

10.) Distinguish between implicit conversion and explicit conversion.

11.) What do you mean by an arithmetic expression? Write C equivalent of following arithmetic expressions.

(1) a * b

(2) a2-b2

(3) b2-4ac

(4) {a+ b / p-q} (d +e)

(5)   a /b +c/d

 

12.) Write C equivalent of following expressions:

(1) a _<b

(2) a=/ b

(3) s in (a2-b2)

(4) d >_e

(5) /s(s-a) (s-b) (s-c)

(6) r 2

 

13.) Evaluate the following expressions;

(1)   4+3*5

(2)   35-8+6/3

(3)   (5+3)*8/2

(4)   s q r t . (5*(5-2)*(5-4)*(5-4))

(5)   (5+3) > (7-2)

 

14.) What will be the value of y?

(Given a=1, b=2, c=8, d=10)

(1)   y= c/b*a

(2)   y= c-a*b

(3)   y = (a+ b)-(c +d)*b

(4)   y =d % c+ b %a

 

15.)   State equivalent C expression for

(1)   e * +y_ sin (a/b)

(2)   l o g m+ log n

(3)   [a +b]

 

16.) Distinguish between m—and—m giving some example.

Leave a Reply

Leave a Reply

Your email address will not be published.