37
Operators and Operators and Expressions Expressions www.eshikshak.co.in www.eshikshak.co.in

Mesics lecture 4 c operators and experssions

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: Mesics lecture  4   c operators and experssions

Operators and ExpressionsOperators and ExpressionsOperators and ExpressionsOperators and Expressions

www.eshikshak.co.inwww.eshikshak.co.in

Page 2: Mesics lecture  4   c operators and experssions

IntroductionIntroductionIntroductionIntroduction

• An operator indicates an operation to be An operator indicates an operation to be performed on data that yields a new value.performed on data that yields a new value.

• An operand is a data items on which An operand is a data items on which operators perform operations.operators perform operations.

• C provides rich set of “Operators”C provides rich set of “Operators”– ArithmeticArithmetic– RelationalRelational– LogicalLogical– BitwiseBitwise

www.eshikshak.co.in

Page 3: Mesics lecture  4   c operators and experssions

IntroductionIntroductionIntroductionIntroduction

www.eshikshak.co.in

Page 4: Mesics lecture  4   c operators and experssions

Properties of OperatorsProperties of OperatorsProperties of OperatorsProperties of Operators

• PrecedencePrecedence– Precedence means priority.Precedence means priority.– When an expressions contains many operators, When an expressions contains many operators,

the operations are carried out according to the the operations are carried out according to the priority of the operators.priority of the operators.

– The higher priority operations are solved first.The higher priority operations are solved first.– ExampleExample

• 10 * 5 + 4 / 210 * 5 + 4 / 2• 10 + 5 – 8 * 2 / 210 + 5 – 8 * 2 / 2

www.eshikshak.co.in

Page 5: Mesics lecture  4   c operators and experssions

Properties of OperatorsProperties of OperatorsProperties of OperatorsProperties of Operators• AssociativityAssociativity

– Associativity means the direction of execution.Associativity means the direction of execution.– When an expression has operators with same precedence, the associativity When an expression has operators with same precedence, the associativity

property decides which operation to be carried out first.property decides which operation to be carried out first.a)a) Left to Right : The expression evaluation starts from the left to right directionLeft to Right : The expression evaluation starts from the left to right direction Example : 12 * 4 / 8 % 2Example : 12 * 4 / 8 % 2 48 / 8 % 248 / 8 % 2 6 % 26 % 2 00b) Right to Left : The expression evaluation starts from right to left directionb) Right to Left : The expression evaluation starts from right to left direction

Example : X = 8 + 5 % 2Example : X = 8 + 5 % 2 X = 8 + 1 X = 8 + 1

X = 9 X = 9

www.eshikshak.co.in

Page 6: Mesics lecture  4   c operators and experssions

Priority of Operators and their clubbingPriority of Operators and their clubbingPriority of Operators and their clubbingPriority of Operators and their clubbing

• Each and every operator in C having its Each and every operator in C having its priority and precedence fixed on the basis of priority and precedence fixed on the basis of these property expression is solved.these property expression is solved.

• Operators from the same group may have Operators from the same group may have different precedence and associativity.different precedence and associativity.

• If arithmetic expression contains more If arithmetic expression contains more operators, the execution will be performed operators, the execution will be performed according to their priorities.according to their priorities.

www.eshikshak.co.in

Page 7: Mesics lecture  4   c operators and experssions

Priority of Operators and their clubbingPriority of Operators and their clubbingPriority of Operators and their clubbingPriority of Operators and their clubbing

• When two operators of the same priority are When two operators of the same priority are found in the expression, the precedence is found in the expression, the precedence is given from the left most operators.given from the left most operators. x = 5 * 4 + 8 / 2 (8 / ( 2 * ( 2 * 2 ) ) )x = 5 * 4 + 8 / 2 (8 / ( 2 * ( 2 * 2 ) ) )

1 2

3

1

2

3

www.eshikshak.co.in

Page 8: Mesics lecture  4   c operators and experssions

Comma and Conditional OperatorComma and Conditional OperatorComma and Conditional OperatorComma and Conditional Operator• It is used to separate two It is used to separate two

or more expressions. or more expressions. • It has lowest precedence It has lowest precedence

among all the operatorsamong all the operators• It is not essential to It is not essential to

enclose the expressions enclose the expressions with comma operators with comma operators within the parenthesis.within the parenthesis.

• Following statements are Following statements are validvalid a = 2, b = 4, c = a + b;a = 2, b = 4, c = a + b; ( a=2, b = 4, c = a + b );( a=2, b = 4, c = a + b );

void main()void main() {{

clrscr();clrscr(); printf(“%d %d”, 2+3, 3-2); printf(“%d %d”, 2+3, 3-2); }}

OUTPUT :OUTPUT :5 15 1

www.eshikshak.co.in

Page 9: Mesics lecture  4   c operators and experssions

Conditional Operator (?:)Conditional Operator (?:)Conditional Operator (?:)Conditional Operator (?:)

• This operator contains condition followed by This operator contains condition followed by two statements and values.two statements and values.

• It is also called as ternary operator.It is also called as ternary operator.• Syntax :Syntax :

Condition ? (expression1) : (expression2);Condition ? (expression1) : (expression2);

• If the If the conditioncondition is true, than is true, than expression1expression1 is is executed, executed, otherwiseotherwise expression2expression2 is executedis executed

www.eshikshak.co.in

Page 10: Mesics lecture  4   c operators and experssions

Conditional Operator (?:)Conditional Operator (?:)Conditional Operator (?:)Conditional Operator (?:)

• Example :Example :void main()void main(){{

clrscr();clrscr();printf(“Maximum : %d”, 5>3 ? 5 : 3)printf(“Maximum : %d”, 5>3 ? 5 : 3)

} } OUTPUT :OUTPUT :

Maximum : 5Maximum : 5

www.eshikshak.co.in

Page 11: Mesics lecture  4   c operators and experssions

Conditional Operator (?:)Conditional Operator (?:)Conditional Operator (?:)Conditional Operator (?:)

• Example :Example :void main()void main(){{

clrscr();clrscr();printf(“Maximum : %d”, 5>3 ? 5 : 3)printf(“Maximum : %d”, 5>3 ? 5 : 3)

} } OUTPUT :OUTPUT :

Maximum : 5Maximum : 5

www.eshikshak.co.in

Page 12: Mesics lecture  4   c operators and experssions

Conditional Operator (?:)Conditional Operator (?:)Conditional Operator (?:)Conditional Operator (?:)

• Example :Example :void main()void main(){{

clrscr();clrscr();5 > 3 ? printf(“True”) : printf(“False”);5 > 3 ? printf(“True”) : printf(“False”);printf(“%d is ”, 5>3 ? : 3)printf(“%d is ”, 5>3 ? : 3)

} } OUTPUT :OUTPUT :

Maximum : 5Maximum : 5

www.eshikshak.co.in

Page 13: Mesics lecture  4   c operators and experssions

Arithmetic OperatorArithmetic OperatorArithmetic OperatorArithmetic Operator

• Two types of arithmetic operatorsTwo types of arithmetic operators– Binary OperatorBinary Operator– Unary OperatorUnary Operator

Operators

Unary Binary Ternary

www.eshikshak.co.in

Page 14: Mesics lecture  4   c operators and experssions

Arithmetic OperatorArithmetic OperatorArithmetic OperatorArithmetic Operator

• Binary OperatorBinary Operator– An operator which requires two operator is know An operator which requires two operator is know

as binary operatoras binary operator– List of Binary OperatorsList of Binary Operators

Arithmetic Operator Operator Explanation Examples

+ Addition 2 + 2 = 4

- Subtraction 5 – 3 = 2

* Multiplication 2 * 5 = 10

/ Division 10 / 2 = 5

% Modular Division 11 % 3 = 2 (Remainder 2)

www.eshikshak.co.in

Page 15: Mesics lecture  4   c operators and experssions

Arithmetic OperatorArithmetic OperatorArithmetic OperatorArithmetic Operator

• Unary OperatorUnary Operator– The operator which requires only one operand is The operator which requires only one operand is

called unary operatorcalled unary operator– List of unary operator areList of unary operator are

Operator Description or Action

- Minus

++ Increment

-- Decrement

& Address Operator

Sizeof Gives the size of the operator

www.eshikshak.co.in

Page 16: Mesics lecture  4   c operators and experssions

Unary Operator - MinusUnary Operator - MinusUnary Operator - MinusUnary Operator - Minus

• Unary minus is used for indicating or changing Unary minus is used for indicating or changing the algebraic sign of a valuethe algebraic sign of a value

• ExampleExampleint x = -50;int x = -50;int y = -x;int y = -x;

• There is no unary plus (+) in C. Even though a There is no unary plus (+) in C. Even though a value assigned with plus sign is valid.value assigned with plus sign is valid.

www.eshikshak.co.in

Page 17: Mesics lecture  4   c operators and experssions

Increment (++) and Decrement (--) Increment (++) and Decrement (--) OperatorOperator

Increment (++) and Decrement (--) Increment (++) and Decrement (--) OperatorOperator

• The ++ operator adds value one to its The ++ operator adds value one to its operand.operand.

• X = X + 1 can be written as X++;X = X + 1 can be written as X++;• There are two types ++ increment operator There are two types ++ increment operator

base on the position are used with the base on the position are used with the operand.operand.

www.eshikshak.co.in

Page 18: Mesics lecture  4   c operators and experssions

Pre-increment (i.e. ++x)Pre-increment (i.e. ++x)Pre-increment (i.e. ++x)Pre-increment (i.e. ++x)

int x =5, y;int x =5, y;y = ++x;y = ++x;printf(“x = %d\n y = %d”, ++x, y);printf(“x = %d\n y = %d”, ++x, y);

OUTPUT :OUTPUT :x = 7x = 7y = 6y = 6

y = ++x;

x = x + 1;

y = x;

www.eshikshak.co.in

Page 19: Mesics lecture  4   c operators and experssions

Post-increment (i.e. ++x)Post-increment (i.e. ++x)Post-increment (i.e. ++x)Post-increment (i.e. ++x)

int x =5, y;int x =5, y;y = x++;y = x++;printf(“x = %d\n y = %d”, x++, y);printf(“x = %d\n y = %d”, x++, y);

OUTPUT :OUTPUT :x = 7x = 7y = 6y = 6

y = x++;

y = x;

x = x + 1;

www.eshikshak.co.in

Page 20: Mesics lecture  4   c operators and experssions

Pre-decrement (i.e. --x)Pre-decrement (i.e. --x)Pre-decrement (i.e. --x)Pre-decrement (i.e. --x)

int x =5, y;int x =5, y;y = --x;y = --x;printf(“x = %d\n y = %d”, --x, y);printf(“x = %d\n y = %d”, --x, y);

OUTPUT :OUTPUT :x = 3x = 3y = 4y = 4

y = --x;

x = x - 1;

y = x;

www.eshikshak.co.in

Page 21: Mesics lecture  4   c operators and experssions

Post-decrement (i.e. ++x)Post-decrement (i.e. ++x)Post-decrement (i.e. ++x)Post-decrement (i.e. ++x)

int x =5, y;int x =5, y;y = x--;y = x--;printf(“x = %d\n y = %d”, x--, y);printf(“x = %d\n y = %d”, x--, y);

OUTPUT :OUTPUT :x = 7x = 7y = 6y = 6

y = x--;

y = x;

x = x - 1;

www.eshikshak.co.in

Page 22: Mesics lecture  4   c operators and experssions

sizeof operator sizeof operator sizeof operator sizeof operator

• The The sizeofsizeof operator operator gives the bytes gives the bytes occupied by a variable.occupied by a variable.

• i.e. the size in terms of i.e. the size in terms of bytes required in bytes required in memory to store the memory to store the value.value.

• The number of bytes The number of bytes occupied varies from occupied varies from variable to variable variable to variable depending upon its depending upon its data type.data type.

• The The sizeofsizeof operator operator gives the bytes gives the bytes occupied by a variable.occupied by a variable.

• i.e. the size in terms of i.e. the size in terms of bytes required in bytes required in memory to store the memory to store the value.value.

• The number of bytes The number of bytes occupied varies from occupied varies from variable to variable variable to variable depending upon its depending upon its data type.data type.

void main()void main(){{

int x = 12;int x = 12; float y = 2;float y = 2;

printf(“size of x : %d”, sizeof(x));printf(“size of x : %d”, sizeof(x)); printf(“\nsize of y :%d”, sizeof(y));printf(“\nsize of y :%d”, sizeof(y));} }

OUTPUT :OUTPUT :sizeof x : 2sizeof x : 2sizeof y : 4sizeof y : 4

void main()void main(){{

int x = 12;int x = 12; float y = 2;float y = 2;

printf(“size of x : %d”, sizeof(x));printf(“size of x : %d”, sizeof(x)); printf(“\nsize of y :%d”, sizeof(y));printf(“\nsize of y :%d”, sizeof(y));} }

OUTPUT :OUTPUT :sizeof x : 2sizeof x : 2sizeof y : 4sizeof y : 4

www.eshikshak.co.in

Page 23: Mesics lecture  4   c operators and experssions

‘‘&’ Operator&’ Operator‘‘&’ Operator&’ Operator

• The ‘&’ returns the address of the variable in a The ‘&’ returns the address of the variable in a memory.memory.

int x = int x = 1515

printf(“%d”,x);printf(“%d”,x);

printf(“\n%u”,&x);printf(“\n%u”,&x);

15ValueValue

XXVariableVariable

2040AddressAddress

www.eshikshak.co.in

Page 24: Mesics lecture  4   c operators and experssions

Relational OperatorRelational OperatorRelational OperatorRelational Operator

• These operators are used to distinguish These operators are used to distinguish two values depending on their relations.two values depending on their relations.

• These operators provide the relationship These operators provide the relationship between two expressions.between two expressions.

• If the relation is true it returns a value 1 If the relation is true it returns a value 1 otherwise 0 for false.otherwise 0 for false.

www.eshikshak.co.in

Page 25: Mesics lecture  4   c operators and experssions

Relational OperatorRelational OperatorRelational OperatorRelational Operator

Operator Description or Action Example Return Value

> Greater than 5 > 4 1

< Less than 10 < 9 0

<= Less than or equal to 10 <= 10 1

>= Greater than or equal to 11 >= 5 1

== Equal to 2 == 3 0

!= Not Equal to 3 != 3 0

www.eshikshak.co.in

Page 26: Mesics lecture  4   c operators and experssions

Assignment OperatorAssignment OperatorAssignment OperatorAssignment Operator• Assigning a value to a variable is very sample. Assigning a value to a variable is very sample.

int x = 5int x = 5

int x = 10;int x = 10;printf(“x = %d\n”,x += 5); // x = x + 5; printf(“x = %d\n”,x += 5); // x = x + 5; O/PO/P x = 15 x = 15

Assignment Operator= *= /= %=

+= -= <<= >>=

>>>= &= ^= !=

www.eshikshak.co.in

Page 27: Mesics lecture  4   c operators and experssions

Logical OperatorsLogical OperatorsLogical OperatorsLogical Operators

• The logical operator between the two The logical operator between the two expressions is tested with logical operators.expressions is tested with logical operators.

• Using these operators, two expressions can be Using these operators, two expressions can be joined.joined.

• After checking the conditions, it provides After checking the conditions, it provides logical logical true(1) true(1) or or false(0) false(0) status.status.

• The operands could be constants, variables The operands could be constants, variables and expressions.and expressions.

www.eshikshak.co.in

Page 28: Mesics lecture  4   c operators and experssions

Logical OperatorsLogical OperatorsLogical OperatorsLogical OperatorsOperator Description or Action Example Return Value

&& Logical AND 5 > 3 && 5 < 10 1

|| Logical OR 8 > 5 || 8 < 2 1

! Logical NOT 8 != 8 0

i.i. The logical AND (&&) operator provides true result The logical AND (&&) operator provides true result when both expressions are true otherwise 0.when both expressions are true otherwise 0.

ii.ii. The logical OR (||) operator true result when one of The logical OR (||) operator true result when one of the expressions is true otherwise 0.the expressions is true otherwise 0.

iii.iii.The logical NOT (!) provides 0 if the condition is true The logical NOT (!) provides 0 if the condition is true otherwise 1.otherwise 1.

www.eshikshak.co.in

Page 29: Mesics lecture  4   c operators and experssions

Bitwise OperatorBitwise OperatorBitwise OperatorBitwise Operator

• C supports a set of bitwise operators for bit C supports a set of bitwise operators for bit manipulationmanipulation

OperatorsOperators MeaningMeaning

>>>> Right ShiftRight Shift

<<<< Left ShiftLeft Shift

^̂ Bitwise XOR (exclusive OR)Bitwise XOR (exclusive OR)

~~ One’s ComplementOne’s Complement

&& Bitwise ANDBitwise AND

|| Bitwise |Bitwise |

www.eshikshak.co.in

Page 30: Mesics lecture  4   c operators and experssions

void main() void main() {{

int x, y;int x, y; clrscr();clrscr(); print(“Read The Integer from keyword (x) ;print(“Read The Integer from keyword (x) ; scanf(“%d”, &x); // input value for x = 8scanf(“%d”, &x); // input value for x = 8

x>>2;x>>2; y=x;y=x;

printf(“The Right shifted data is = %d”, y);printf(“The Right shifted data is = %d”, y);

}}

Shifting of two bits means the inputted number is to be divided by 2s where s is the number of shifts i.e. in short y = n/2s

Where n = number and s = the number of position to be shift.

For example as per the program

Y = 8 / 22 = 2

Shifting of two bits means the inputted number is to be divided by 2s where s is the number of shifts i.e. in short y = n/2s

Where n = number and s = the number of position to be shift.

For example as per the program

Y = 8 / 22 = 2

www.eshikshak.co.in

Page 31: Mesics lecture  4   c operators and experssions

void main() void main() {{

int x, y;int x, y; clrscr();clrscr(); print(“Read The Integer from keyword (x) ;print(“Read The Integer from keyword (x) ; scanf(“%d”, &x); // input value for x = 2scanf(“%d”, &x); // input value for x = 2

x<<=3;x<<=3; y=x;y=x;

printf(“The Right shifted data is = %d”, y);printf(“The Right shifted data is = %d”, y);

}}

Shifting of three bits left means the number is multiplied by 8; in short y = n * 2s where n = number s = the number of position to be shifted

As per the program

Y = 2 * 23

Shifting of three bits left means the number is multiplied by 8; in short y = n * 2s where n = number s = the number of position to be shifted

As per the program

Y = 2 * 23

www.eshikshak.co.in

Page 32: Mesics lecture  4   c operators and experssions

void main()void main(){{ int a, b, c;int a, b, c; clrscr();clrscr(); printf(“Read the integers from keyboard ( a & b ) :”);printf(“Read the integers from keyboard ( a & b ) :”); scanf(“%d %d”, &a, &b);scanf(“%d %d”, &a, &b); c = a & b;c = a & b; printf(“The answer after ANDing is (C) = %d”, c);printf(“The answer after ANDing is (C) = %d”, c);}}

OUTPUT :OUTPUT :Read the Integers from keyboard (a & b) : 8 4Read the Integers from keyboard (a & b) : 8 4The Answer after ANDing is (C) = 0The Answer after ANDing is (C) = 0

www.eshikshak.co.in

Page 33: Mesics lecture  4   c operators and experssions

Table of exclusive ANDTable of exclusive ANDTable of exclusive ANDTable of exclusive AND

X Y Outputs

0 0 0

0 1 0

1 0 0

1 1 1

www.eshikshak.co.in

Page 34: Mesics lecture  4   c operators and experssions

Binary equivalent of 8Binary equivalent of 8

Binary equivalent of 4Binary equivalent of 4

After executionAfter executionC = 0 C = 0 Binary equivalent of 0Binary equivalent of 0

www.eshikshak.co.in

Page 35: Mesics lecture  4   c operators and experssions

void main()void main(){{ int a, b, c;int a, b, c; clrscr();clrscr(); printf(“Read the integers from keyboard ( a & b ) :”);printf(“Read the integers from keyboard ( a & b ) :”); scanf(“%d %d”, &a, &b);scanf(“%d %d”, &a, &b); c = a | b;c = a | b; printf(“The answer after ORing is (C) = %d”, c);printf(“The answer after ORing is (C) = %d”, c);}}

OUTPUT :OUTPUT :Read the Integers from keyboard (a & b) : 8 4Read the Integers from keyboard (a & b) : 8 4The Answer after ORing is (C) = 12The Answer after ORing is (C) = 12

www.eshikshak.co.in

Page 36: Mesics lecture  4   c operators and experssions

Table of exclusive ORTable of exclusive ORTable of exclusive ORTable of exclusive OR

X Y Outputs

0 0 0

0 1 1

1 0 1

1 1 0

www.eshikshak.co.in

Page 37: Mesics lecture  4   c operators and experssions

Binary equivalent of 8Binary equivalent of 8

Binary equivalent of 4Binary equivalent of 4

After executionAfter executionC = 12C = 12Binary equivalent of 0Binary equivalent of 0

www.eshikshak.co.in