manohar c

17

Click here to load reader

Upload: manohar-prasad

Post on 06-Apr-2018

216 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Manohar C

8/3/2019 Manohar C

http://slidepdf.com/reader/full/manohar-c 1/17

‘C’

OPERATORS

By- ManoharPrasad.

11/23/11

11

Manohar Prasad.

Page 2: Manohar C

8/3/2019 Manohar C

http://slidepdf.com/reader/full/manohar-c 2/17

INTRODUCTION:

  An operator is a symbol which helps the user tocommand the computer to do a certain mathematicalor logical manipulations. Operators are used in Clanguage program to operate on data and variables.

C has following operators :

1. Arithmetic Operator

2. Relational Operator

3. Logical Operator

4. Assignment Operator

5. Increment and Decrement Operators

6.

Conditional Operator 11/23/11

22

Manohar Prasad.

Page 3: Manohar C

8/3/2019 Manohar C

http://slidepdf.com/reader/full/manohar-c 3/17

Arithmetic Operators:

Operators Meaning

+ Addition

_ Subtraction

* Multiplication

/ Division

% Modulus Operator11/23/11

33

Manohar Prasad.

Page 4: Manohar C

8/3/2019 Manohar C

http://slidepdf.com/reader/full/manohar-c 4/17

Examples of arithmetic operators:

x + y

x - y-x + ya * b + c-a * b

here a, b, c, x, y are known as operands. The modulusoperator is a special operator in C language whichevaluates the remainder of the operands after division.

Integer Arithmetic:

When an arithmetic operation is performed on twowhole numbers or integers than such an operation is

called as integer arithmetic. It always gives an integeras the result.

11/23/11

44

Manohar Prasad.

Page 5: Manohar C

8/3/2019 Manohar C

http://slidepdf.com/reader/full/manohar-c 5/17

Examples of arithmetic operators:

Let x = 27 and y = 5 be 2 integer numbers. Then the

integer operation leads to the following results.

x + y = 32x – y = 22x * y = 115x % y = 2x / y = 5

In integer division the fractional part is truncated.

Floating point arithmetic :

When an arithmetic operation is preformed on two real

numbers or fraction numbers such an operation iscalled floating point arithmetic. The remainder11/23/11

55

Manohar Prasad.

Page 6: Manohar C

8/3/2019 Manohar C

http://slidepdf.com/reader/full/manohar-c 6/17

Examples of floating types:

Let x = 14.0 and y = 4.0 then

x + y = 18.0x – y = 10.0x * y = 56.0x / y = 3.50

Mixed mode arithmetic

When one of the operand is real and other is an

integer and if the arithmetic operation is carried outon these 2 operands then it is called as mixed modearithmetic. If any one operand is of real type then theresult will always be real thus 15/10.0 = 1.5

11/23/11

66

Manohar Prasad.

Page 7: Manohar C

8/3/2019 Manohar C

http://slidepdf.com/reader/full/manohar-c 7/17

Hierarchy of Operations :

The priority or precedence in which theoperations in an arithmetic statement areperformed is called the hierarchy of operations.The hierarchy of commonly used operators are:

Priority Operators Discription1st. * / % Multiplication,

Division,

Modulus2nd. + - Addition,Subtraction

3rd. = Assignment

11/23/11

77

Manohar Prasad.

Page 8: Manohar C

8/3/2019 Manohar C

http://slidepdf.com/reader/full/manohar-c 8/17

Example:

i = 2 * 3 / 4 + 4 / 4 + 8 - 2 + 5 / 8

i = 6 / 4 + 4 / 4 + 8 - 2 + 5 / 8operation: *

i = 1 + 4 / 4 + 8 - 2 + 5 / 8operation: /

i = 1 + 1+ 8 - 2 + 5 / 8operation: /

i = 1 + 1 + 8 - 2 + 0operation: /

i = 2 + 8 - 2 + 0

operation: + 11/23/11

88

Manohar Prasad.

Page 9: Manohar C

8/3/2019 Manohar C

http://slidepdf.com/reader/full/manohar-c 9/17

Relational OperatorS:

Often it is required to compare the relationshipbetween operands and bring out a decision andprogram accordingly. This is when the relationaloperator come into picture. C supports the followingrelational operators.

 

11/23/11

99

Manohar Prasad.

Operator Meaning

< Is less than

<= Is less than or equals to

> Is greater than

>= Is greater than or equals to

== Is equals to

!= Is not equals to

Page 10: Manohar C

8/3/2019 Manohar C

http://slidepdf.com/reader/full/manohar-c 10/17

Examples:

It is required to compare the marks of 2 students, salaryof 2 persons, we can compare them using relationaloperators.

A simple relational expression contains only one

relational operator and takes the following form.

exp1 relational operator exp2

6.5 <= 25 TRUE-65 > 0 FALSE10 < 7 + 5 TRUE

Relational expressions are used in decision making

statements of C language such as if, while and forstatements to decide the course of action of a11/23/11

1010

Manohar Prasad.

Page 11: Manohar C

8/3/2019 Manohar C

http://slidepdf.com/reader/full/manohar-c 11/17

Logical Operators:

C has the following logical operators, theycompare or evaluate logical and relationalexpressions.

11/23/11

1111

Manohar Prasad.

Operators Meaning

&& Logical AND

|| Logical OR

! Logical NOT

Page 12: Manohar C

8/3/2019 Manohar C

http://slidepdf.com/reader/full/manohar-c 12/17

Example :

The logical AND is used to combine 2expressions or the condition evaluates totrue if both the 2 expressions is true.

a > b && x = = 10

Logical OR (||) :

The logical OR is used to combine 2 expressions orthe condition evaluates to true if any one of the 2expressions is true.

Example :11/23/11

1212

Manohar Prasad.

Page 13: Manohar C

8/3/2019 Manohar C

http://slidepdf.com/reader/full/manohar-c 13/17

Logical NOT (!):The logical not operator takes single expression and

evaluates to true if the expression is false andevaluates to false if the expression is true. In otherwords it just reverses the value of the expression.

For example:

! (x >= y) the NOT expression evaluates to true onlyif the value of x is neither greater than or equal to y.

11/23/11

1313

Manohar Prasad.

1414

Page 14: Manohar C

8/3/2019 Manohar C

http://slidepdf.com/reader/full/manohar-c 14/17

Assignment Operator:

The Assignment Operator evaluates an expression

on the right of the expression and substitutes it tothe value or variable on the left of the expression.

Example :

x = a + b

Here the value of a + b is evaluated and substituted

to the variable x.

11/23/11

1414

Manohar Prasad.

1515

Page 15: Manohar C

8/3/2019 Manohar C

http://slidepdf.com/reader/full/manohar-c 15/17

Increment And DecrementOperators:

The increment operator (++) adds 1 to its operand.

The decrement operator (--) subtracts 1 from its operand.

Postfix and Prefix Forms

There are two forms for each of the operators:

postfix and prefix.

Both forms increment or decrement the appropriate variable,but they do so at different times. The statement ++i (prefixform) increments i before using its value, while i++ (postfixform) increments it after its value has been used.

11/23/11

1515

Manohar Prasad.

1616

Page 16: Manohar C

8/3/2019 Manohar C

http://slidepdf.com/reader/full/manohar-c 16/17

Conditional Operator:

The conditional operators ? and : are sometimes calledternary operators since they take three arguments. Infact, they form a kind of foreshortened if-then-else.Their general form is

expression 1 ? expression 2 : expression 3

Example

y = ( x > 5 ? 3 : 4 )

This statement will store 3 in y if x is greater than 5,otherwise it will store 4 in y.

The limitation of the conditional operators is that after

the ? or after the : only one C statement can occur.Therefore, in serious C programming conditional

11/23/11

1616

Manohar Prasad.

1717

Page 17: Manohar C

8/3/2019 Manohar C

http://slidepdf.com/reader/full/manohar-c 17/17

 

Thanks !11/23/11

1717

Manohar Prasad.