coper in c

Post on 12-Apr-2017

159 Views

Category:

Education

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

OPERATORS & EXPRESSIONS

An operator is a symbol (+,-,*,/)that tells the compiler to perform specific mathematical or logical manipulations. C language is rich in built-in operators and provides the following types of operators:

Operators in C

1. Arithmetic operators2. Relational operators

3. Logical operators4. Assignment operators

5. Increment and decrement operators6. Conditional operators

7. Bitwise operators8. Special operators

Arithmetic operators

Operator example Meaning

+ a + b Addition –unary

- a – b Subtraction- unary

* a * b Multiplication

/ a / b Division

% a % b Modulo division- remainder

Compound Assignment Operator

● Often we use “update” forms of operators– x=x+1, x=x*2, ...

● C offers a short form for this:– Generic Form

variable op= expr equivalent to variable = variable op expr

Update forms have value equal to the final value of expr● i.e., x=3; y= (x+=3); /* x and y both get value 6 */

Operator Equivalent to:

x *= y x = x * y

y -= z + 1 y = y - (z + 1)

a /= b a = a / b

x += y / 8 x = x + (y / 8)

y %= 3 y = y % 3

Comma Operator has Lowest Precedence i.e it is having lowest priority so it is evaluated at last.

Comma operator returns the value of the rightmost operand when multiple comma operators are used inside an expression.

Comma Operator Can acts as –

Operator : In the Expression

Separator : Declaring Variable , In Function Call Parameter List

Comma Operators

Let a=5 and b=10a++; //a becomes 6a--; //a becomes 5++b; //a becomes 11--b; //a becomes 9

Program for pre­increment &post­increment#include<stdio.h> Output :void main() Value of a : 10{ Value of b : 11int a,b,x=10,y=10;a = x++;b = ++y;printf("Value of a : %d",a);printf("Value of b : %d",b);}

PRE­INCREMENT & POST INCREMENT

Relational

Operator

Meaning

> Greater than

>= Greater than or equal to

<= Less than or equal to

< Less than

== Is equal to

!= Is not equal to

Logical Operator

Name of the Operator

&& And Operator

| Or Operator

! Not Operator

Operators

Examples

Truth Tablea b Value of the

expression

a&& b

a || b

0 0 0 0

0 1 0 1

1 0 0 1

1 1 1 1

Shorthand Assignment operators

Simple assignment operator

Shorthand operator

a = a+1 a + =1

a = a-1 a - =1

a = a* (m+n) a * = m+n

a = a / (m+n) a / = m+n

a = a %b a %=b

Conditional Operators [ ?: ] : Ternary Operator Statement in C

expression 1 ? expression 2 : expression 3

where

expression1 is Condition

expression2 is Statement Followed if Condition is True

expression2 is Statement Followed if Condition is False

BITWISE OPERATORS

Operator Meaning

& Bitwise AND

| Bitwise OR

^ Bitwise exclusive OR

<< Shift left

>> Shift right

These operators allow manipulation of data at the bit level

Review – Bitwise Operations in Integers

• & – AND● Result is 1 if both

operand bits are 1

• | – OR● Result is 1 if either

operand bit is 1

• ^ – Exclusive OR● Result is 1 if operand

bits are different

• ~ – Complement● Each bit is reversed

• << – Shift left● Multiply by 2

• >> – Shift right● Divide by 2

Corresponding bits of both operands are combined by the usual logic operations.

Apply to all kinds of integer types:–Signed and unsignedchar, short, int, long, long long

Assume if A = 60; and B = 13; now in binary format they will be as follows:

A = 0011 1100

B = 0000 1101

---------------------

A&B = 0000 1100

A|B = 0011 1101

A^B = 0011 0001

~A = 1100 0011

The Bitwise operators supported by C language are listed in the following table. Assume variable A holds 60 and variable B holds 13, then:

Example for Bitwise Operations

Special operators

1. Comma operator ( ,)2. sizeof operator – sizeof( )3. Pointer operators – ( & and *)4. Member selection operators – ( . and ->)

● BODMAS RULE- Brackets of Division Multiplication Addition Subtraction

PRECEDENCE OF OPERATORS

Brackets will have the highest precedence and have to be evaluated first, then comes of , then comes division, multiplication,addition and finally subtraction.

The 2 distinct priority levels of arithmetic operators in c are-Highest priority : * / %Lowest priority : + -

Rules for evaluation of expression

1. First parenthesized sub expression from left to right are evaluated.2. If parentheses are nested, the evaluation begins with the innermost

sub expression3. The precedence rule is applied in determining the order of

application of operators in evaluating sub expressions

4. The associatively rule is applied when 2 or more operators of the same precedence level appear in a sub expression.

5. Arithmetic expressions are evaluated from left to right using the rules of precedence

6. When parentheses are used, the expressions within parentheses assume highest priority

Hierarchy of operators

Operator Description Associativity

( ), [ ] Function call, array element reference

Left to Right

+, -, ++, - -,!,~,*,&

Unary plus, minus, increment, decrement, logical negation, 1’s complement, pointer reference, address

Right to Left

*, / , % Multiplication, division, modulus

Left to Right

Expressions Combination of Operators and Operands

Example 2 * y + 5

Operands

Operators

Type Conversions

There are two kinds of type conversion. Automatic or implicit type conversion

and Explicit type conversion.

Type Conversion

Converting from one data type to another type is called type conversion.

There are two kinds of type conversions.Type Conversion

Implicit Explicit

Converting from one data type to another type is called type conversion.

There are two kinds of type conversions.Type Conversion

Implicit Explicit

Implicit: Same data type conversionSmaller data type into bigger

(memory size)Explicit: Different Data type

Type Casting(Float--- int (or) int ---- float)

Type Conversion

Thank You

You Gave me Your time

top related