36
Operators in C/C++ By: Er. Aman Kumar

8. operators

Embed Size (px)

Citation preview

Operators in C/C++

By: Er. Aman Kumar

Operators

• Operators are the special symbols that work on operand. Like in a + b, a and b are operands and + is special symbol (operator) which is working or operating on them.

way2ITech

Types of Operators

way2ITech

Assignment Operator

• Used to assign value to variable. = is assignment operator.

• NOTE: Value at right side is assigned to left side variable.

int a = 10;

int b = 55;

b = a;

Value of b will become 10 and value of a will remain as such.

way2ITech

Lvalues and Rvalues

• lvalue : An expression that is an lvalue may appear as either the left-hand or right-hand side of an assignment.

way2ITech

• rvalue : An expression that is an rvalue may appear on the right- but not left-hand side of an assignment.

• Variables are lvalues and so may appear on the left-hand side of an assignment. Numeric literals are rvalues and so may not be assigned and can not appear on the left-hand side.

• Following is a valid statement:

int g = 20;

• But following is not a valid statement and would generate compile-time error:

• 10 = 20;

way2ITech

Arithmetic operators

way2ITech

way2ITech

Priority of operators

• If 2 operators with same priority come?, then we will follow left to right rule. Means from left, which operator comes first, will be executed.

way2ITech

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

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

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

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

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

• i = 2 + 8 - 2 + 0 operation: +

• i = 10 - 2 + 0 operation: +

• i = 8 + 0 operation: -

• i = 8 operation: + way2ITech

Int i = 3 / 2 * 4 + 3 / 8 + 3

• i = 3 / 2 * 4 + 3 / 8 + 3

• i = 1 * 4 + 3 / 8 + 3 operation: /

• i = 4 + 3 / 8 + 3 operation: *

• i = 4 + 0 + 3 operation: /

• i = 4 + 3 operation: +

• i = 7 operation: +

way2ITech

Arithmetical Calculations on Characters

• Arithmetic Calculations on characters can be done using ASCII codes

way2ITech

way2ITech

Cyclic order in Integers

way2ITech

WAP to enter small case character and Output upper case character

way2ITech

Compound Arithmetic Assignment

way2ITech

Unary Operators

• These are called as Unary operators as there is one operator and one operand only. It is two types:

way2ITech

Rule to solve

• 1. Solve Pre

• 2. Solve Equation (ignore all pre and post and solve as normal equation)

• 3. Solve Post

way2ITech

Comparison Operators

• They are used to compare value of 2 variables

• These operators deal in true or false. They return 0 if false and 1 (non zero number) for true.

way2ITech

• NOTE:

There is difference in x = y and x == y.

x=y assigns value of variable y to variable x.

way2ITech

Logical Operators

• They are used to combine two or multiple conditions.

• 1. And &&

• 2. OR ||

• 3. NOT !

way2ITech

&& and || are useful in the following programming situations:

I. When it is to be tested whether a value falls within a particular range or not.

II. When after testing several conditions the outcome is only one of the two answers (This problem is often called yes/no problem or combine multiple conditions).

• These also work in Boolean Values (True/False). way2ITech

• Following table shows all the logical operators. Assume variable A holds 1 and variable B holds 0, then:

way2ITech

Bitwise Operator

• This work in bits but takes input and gives Output in decimal form only.

way2ITech

The truth tables for &, |, and ^ are as follows:

way2ITech

way2ITech

Arithmetic shift • The two basic types are the

• arithmetic left shift (<<)

• arithmetic right shift (>>)

• For binary numbers it is a bitwise operation that shifts all of the bits of its operand; every bit in the operand is simply moved a given number of bit positions, and the vacant bit-positions are filled in.

• 23<<1 means shift bits of 23 by 1. Sly, 23>>1 means shift bits of 23 by 1.

way2ITech

way2ITech

Bitwise Complement

• Here we add 1 and then change the sign

• ~N = - (N + 1)

way2ITech

sizeof() operator

• This is used to get size in bytes for datatype, variable, structures, and union. It returns the size in bytes.

• sizeof(int) -> 2 (in blue screen TC)

• sizeof(char) -> 1

• sizeof(float) -> 4

way2ITech

way2ITech

Ternary Operator or Conditional Operator ? :

Syntax

(condition) ? expression 1 : expression 2

way2ITech

way2ITech

way2ITech

way2ITech