Operators N Expressions in C EngineersTutor 1
Operators N Expressions in C EngineersTutor 1
C++ provides operators for combining arithmetic, relational, logical, bitwise, and conditional expressions. Note that
every operator must return some value. For example, + operator returns sum of two numbers, * operator return
multiplication of two numbers etc.
Operator is a symbol that performs some operation. Simple operations may be addition, subtraction,
multiplication, divison etc. So, operator is a symbol, which tells the compiler to do some operation or action.
For example,
2 + 3 = 5. Here the data 2 an 3 are known as operands. + is an operator, which perform summation of given
numbers 2 and 3. The data 5 is the result of operation.
In above expression, the operator + operates on TWO operands. So, + is called binary operator. BI means 2.
Rememeber bicycle has two wheels. Binacular has 2 eyes.
5 * 10 = 50. Here the data 5 an 10 are known as operands. * is an operator, which perform multiplication of
given numbers 5 and 10. The data 50 is the result of operation.
z = x - y. Here the variables x an y are known as operands. - is an operator, which perform subtracton of given
numbers x and y. The result of operation is stored in the variable, z.
Type of operators
• Arithmetic operators
• Relational operators
• Logical operators
• Assignment operators
• Pointer operators
• Special operators
• Bitwise operators
• Unary operators
• Binary operators
• Ternary operators
Unary operators: Some operators operate on single operand. They are called unary operators. Some examples are
given below, which will be discussed later.
Note:
For example, the symbol * can be used for two purposes. It can be used as multiplication operator. And also used as
pointer operator. This is known as operator overloading. The process of making an operator to exhibit different
behaviours in different instances (times) is known as operator overloading. Real life example of overloading
is the use of word: “square”. The word “square” has two meanings. When we say square of 5 = 25, the meaning
is to find square of a number. Other meaning of a square is a geometric figure: SQUARE.
Binary operators: Some operators operate on two operands. They are called binary operators. Some examples are
given below, which will be discussed later.
The conditional operators ? and : are called ternary operators since they take 3 arguments. Note that ternary operator
takes 3 operands for its implementation.
Arithmetic operators: +, -, *, /, %. These are binary operators as they operate on 2 operands at a time. Note that each of
these operators can work with int, float or char.
Modulo division operator (%): This operator returns remainder after integer division. It is important to note that during
modulo division, the sign of the result is always the sign of first operand (i.e., numerator). So, if you are interested to
get remainder after division operation, we should use %.
Logical Operators
Logical operators are used to combine two or more conditions in the program. There are 3 logical operators
namely: NOT, AND, OR. Note that these operators yield BOOLEAN result. BOOLEAN means either true or
False.
Logical AND
False && False False
False && True False
True && False False
True && True True
Logical OR table:
Logical OR
False || False False
False || True True
True || False True
True || True True
• !20 means ‘not 20’ ‘not True’ False. So, finally it returns FALSE
!0 = 1
!25 = 0
Q. The marks obtained by a student in 5 different subjects are input through the keyboard. The student gets
a division as per the following rules:
Relational operators are used to compare two quantities for big/small/equal etc. note that these quantities can
be variables/constants/expressions. These operators compare two operands, so these are binary operators.
Characters can also be compared as they are represented internally as ASCII codes (integers).
Relational operator returns either TRUE or FALSE. In C++, non-zero (+ve or –ve) value represents TRUE
and zero represents FALSE. There are 6 relational operators, which are described in below table.
For example, the statement x = = y; tests whether left-hand side (LHS) value and right-hand side (RHS)
value are equal or not.
Compares LHS with RHS. If they are equal, the result is true.
If they are unequal, the result is false
Note:
Compiler is ignorant to distinguish = and = = operators. So, this logical error should be taken care by
user.
Used to assign right side value to the left side variable. For e.g. x =5; the value 5 is stored in variable x.
Increment/decrement Operators
The increment (++) and decrement (--) operators provide a convenient way of, respectively, adding and
subtracting 1 from a numeric variable. These are summarized in the following table. The examples assume
the following variable definition:
int a = 5;
int b = 6
Prefix & postfix operators have same effect if they are used in an isolated C++ statements. a++ and ++a are
the same, if they are used in isolated statements.
In other words, the statements ++x or x++; is equivalent of the following statement: x = x + 1;
And --y or y--; is equivalent of the following statement: y = y -1.
Note: The increment and decrement operators can be used only with variables, not with constants.
7|Page Youtube.com/EngineersTutor www.EngineersTutor.com
7
Hence a++, x++ are legal. 6++, 10++ are invalid.
EXPRESSION OUTPUTS
x=5 y=x++ x=6 y=5
x=5 y=++x x=6 y=6
a=8 b=--a a=7 b=7
a=8 b=a-- a=7 b=8
x=5 , y=8 z=x+y-- x=5 y=7 z=13
x=5 y=8 z=x+--y x=5 y=7 z=12
k = i++ + --j
initial value of i is used, because i++ is a post-increment. Value of
j is decremented first, because –j is a pre-decrement operation. So,
i = 4 j = 6 and k =9 get printed.
Prefix and postfix operators have different effects when used in association with some other operators in a C
statement.
C++ offers increment (++) and decrement (- -) operators. ++ operator increments the value of the variable by
1. -- decrements the value of the variable by 1. These two are unary operators as they operate on only one
operand.
Bitwise operators
These operators can operate upon ints and chars but not on floats and doubles.
Bitwise AND: c=a & b: If the corresponding bits are both 1 then the resultant bit is 1 , otherwise 0.
Bitwise OR: c=a | b: If the corresponding bits are both 0 then the resultant bit is 0 , otherwise 1.
Exclusive OR: c=a ^ b: If the corresponding bits are both same then the resultant bit is 0 , otherwise 1.
1’s Compliment: c=~a: 0 bit is converted to 1 and vice versa
Left Shift: c=a<<b: In the left shift operator(<<), b number of 0’s are added on the right side.
Right Shift: c=a>>b: In the right shift operator(>>), the right most bit is removed and b number of 0’s are
added on the left side for right shift by 1.
Comma Operator
The comma operator is used between multiple expressions inside a set of parentheses. These expressions are
then evaluated from left to right and the entire expression assumes the value of the last one evaluated.
For e.g. we can form an expression by separating two sub expressions with a comma. The result is as follows:
• Both expressions are evaluated, with the left expression being evaluated first.
• The entire expression evaluates to the value of the right most expression.
Address Operators
The address-of operator (&) returns the address of a given variable. For e.g. ptr = &x; &x is pronounced as ampersand
x. &x return address of the variable x. The statement y = &x; stores address of variable x into ptr.
The indirection operator (*) returns the value of the memory location (variable) that it points to.
For e.g. z = *ptr; *ptr is pronounced as value at ptr memory location (variable). The statement z = *ptr; stores value at
memory location ptr into variable z.
sizeof Operator
10 | P a g e Y o u t u b e . c o m / E n g i n e e r s T u t o r www.EngineersTutor.com
10
It returns the number of bytes of the given expression or operand or a data type.
11 | P a g e Y o u t u b e . c o m / E n g i n e e r s T u t o r www.EngineersTutor.com
11