[go: up one dir, main page]

0% found this document useful (0 votes)
9K views20 pages

Operator in C

Operators are essential elements in C/C++ that perform computations on operands. There are several types of operators including arithmetic, relational, logical, bitwise, and assignment. Arithmetic operators perform math operations, relational compare values, logical combine conditions, bitwise work at the bit level, and assignment assign values. Each operator type has specific functions like adding, comparing, AND/OR, shifting bits, and assigning a value to a variable.

Uploaded by

shakil ahmed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9K views20 pages

Operator in C

Operators are essential elements in C/C++ that perform computations on operands. There are several types of operators including arithmetic, relational, logical, bitwise, and assignment. Arithmetic operators perform math operations, relational compare values, logical combine conditions, bitwise work at the bit level, and assignment assign values. Each operator type has specific functions like adding, comparing, AND/OR, shifting bits, and assigning a value to a variable.

Uploaded by

shakil ahmed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

Operartor In C/C++

Operators are the foundation of any programming language. Thus the


functionality of C/C++ programming language is incomplete without the use of
operators. We can define operators as symbols that help us to perform specific
mathematical and logical computations on operands. In other words, we can say
that an operator operates the operands.

C/C++ has many built-in operator types and they are classified as follows:

1. Arithmetic Operators
2. Relational Operators
3. Logical Operators
4. Bitwise Operators
5. Assignment Operators
6. Other Operators

1. Arithmetic Operators: These are used to perform arithmetic/mathematical


operations on operands. The binary operators falling in this category are:
 Addition: The ‘+’ operator adds two operands. For example, x+y.
 Subtraction: The ‘-‘ operator subtracts two operands. For example, x-y.
 Multiplication: The ‘*’ operator multiplies two operands. For example, x*y.
 Division: The ‘/’ operator divides the first operand by the second. For
example, x/y.
 Modulus: The ‘%’ operator returns the remainder when first operand is
divided by the second. For example, x%y.
Output:

a is 10 and b is: 4
a+b is: 14
a-b is: 6
a*b is: 40
a/b is: 2
a%b is: 2

The ones falling into the category of unary arithmetic operators are:

 Increment: The ‘++’ operator is used to increment the value of an integer.


When placed before the variable name (also called pre-increment operator),
its value is incremented instantly. For example, ++x.
And when it is placed after the variable name (also called post-increment
operator), its value is preserved temporarily until the execution of this
statement and it gets updated before the execution of the next statement.
For example, x++.

 Decrement: The ‘ – – ‘ operator is used to decrement the value of an integer.


When placed before the variable name (also called pre-decrement operator),
its value is decremented instantly. For example, – – x.
And when it is placed after the variable name (also called post-decrement
operator), its value is preserved temporarily until the execution of this
statement and it gets updated before the execution of the next statement.
For example, x – –.
Output:

a is 11 and res is 10
a is 10 and res is 11
a is 11 and res is 11
a is 10 and res is 10

2. Relational Operators: Relational operators are used for comparison of two


values to understand the type of relationship a pair of number shares. For
example, less than, greater than, equal to etc. Let’s see them one by one
1. Equal to operator: Represented as ‘==’, the equal to operator checks
whether the two given operands are equal or not. If so, it returns true.
Otherwise it returns false. For example, 5==5 will return true.
2. Not equal to operator: Represented as ‘!=’, the not equal to operator checks
whether the two given operands are equal or not. If not, it returns true.
Otherwise it returns false. It is the exact boolean complement of
the ‘==’ operator. For example, 5!=5 will return false.
3. Greater than operator: Represented as ‘>’, the greater than operator checks
whether the first operand is greater than the second operand or not. If so, it
returns true. Otherwise it returns false. For example, 6>5 will return true.
4. Less than operator: Represented as ‘<‘, the less than operator checks
whether the first operand is lesser than the second operand. If so, it returns
true. Otherwise it returns false. For example, 6<5 will return false.
5. Greater than or equal to operator: Represented as ‘>=’, the greater than or
equal to operator checks whether the first operand is greater than or equal
to the second operand. If so, it returns true else it returns false. For
example, 5>=5 will return true.
6. Less than or equal to operator: Represented as ‘<=’, the less than or equal
tooperator checks whether the first operand is less than or equal to the
second operand. If so, it returns true else false. For example, 5<=5 will also
return true.

Examples:
Output:
a is greater than b
a is greater than or equal to b
a is greater than or equal to b
a is greater than b
a and b are not equal
a is not equal to b

3. Logical Operators: They are used to combine two or more


conditions/constraints or to complement the evaluation of the original
condition under consideration. They are described below:
1. Logical AND operator: The ‘&&’ operator returns true when both the
conditions under consideration are satisfied. Otherwise it returns false. For
example, a && b returns true when both a and b are true (i.e. non-zero).
2. Logical OR operator: The ‘||’ operator returns true even if one (or both) of
the conditions under consideration is satisfied. Otherwise it returns false. For
example, a || b returns true if one of a or b or both are true (i.e. non-zero).
Of course, it returns true when both a and b are true.
3. Logical NOT operator: The ‘!’ operator returns true the condition in
consideration is not satisfied. Otherwise it returns false. For
example, !a returns true if a is false, i.e. when a=0.

Examples:
Output:

AND condition not satisfied


a is greater than b OR c is equal to d
a is not zero

Short-Circuiting in Logical Operators:


 In case of logical AND, the second operand is not evaluated if first operand is
false. For example, program 1 below doesn’t print “GeeksQuiz” as the first
operand of logical AND itself is false.
Output:

No Output

But below program prints “GeeksQuiz” as first operand of logical AND is true.

Output:

GeeksQuiz

 In case of logical OR, the second operand is not evaluated if first operand is
true. For example, program 1 below doesn’t print “GeeksQuiz” as the first
operand of logical OR itself is true.

Output:

No Output
But below program prints “GeeksQuiz” as first operand of logical OR is false.

Output:

GeeksQuiz

4. Bitwise Operators: In C, the following 6 operators are bitwise operators


(work at bit-level)

1. The & (bitwise AND) in C or C++ takes two numbers as operands and does
AND on every bit of two numbers. The result of AND is 1 only if both bits are
1.
2. The | (bitwise OR) in C or C++ takes two numbers as operands and does OR
on every bit of two numbers. The result of OR is 1 if any of the two bits is 1.
3. The ^ (bitwise XOR) in C or C++ takes two numbers as operands and does
XOR on every bit of two numbers. The result of XOR is 1 if the two bits are
different.
4. The << (left shift) in C or C++ takes two numbers, left shifts the bits of the
first operand, the second operand decides the number of places to shift.
5. The >> (right shift) in C or C++ takes two numbers, right shifts the bits of the
first operand, the second operand decides the number of places to shift.
6. The ~ (bitwise NOT) in C or C++ takes one number and inverts all bits of it
Example:

Output:

a = 5, b = 9
a&b = 1
a|b = 13
a^b = 12
~a = 250
b<<1 = 18
b>>1 = 4
Interesting facts about bitwise operators
1. The left shift and right shift operators should not be used for negative
numbers. If any of the operands is a negative number, it results in undefined
behaviour. For example results of both -1 << 1 and 1 << -1 is undefined. Also,
if the number is shifted more than the size of integer, the behaviour is
undefined. For example, 1 << 33 is undefined if integers are stored using 32
bits. See this for more details..
2. The bitwise operators should not be used in place of logical operators. The
result of logical operators (&&, || and !) is either 0 or 1, but bitwise
operators return an integer value. Also, the logical operators consider any
non-zero operand as 1. For example, consider the following program, the
results of & and && are different for same operands.

Output:

False True

3. The left-shift and right-shift operators are equivalent to multiplication and


division by 2 respectively. As mentioned in point 1, it works only if numbers
are positive.
Output:

x << 1 = 38
x >> 1 = 9

4. The & operator can be used to quickly check if a number is odd or even. The
value of expression (x & 1) would be non-zero only if x is odd, otherwise the
value would be zero.

Output:

Odd

5. The ~ operator should be used carefully. The result of ~ operator on a small


number can be a big number if the result is stored in an unsigned variable.
And the result may be a negative number if the result is stored in a signed
variable (assuming that the negative numbers are stored in 2’s complement
form where the leftmost bit is the sign bit)
Output:

Signed Result -2
Unsigned Result 4294967294d

5. Assignment Operators: Assignment operators are used to assigning value


to a variable. The left side operand of the assignment operator is a variable
and right side operand of the assignment operator is a value. The value on
the right side must be of the same data-type of the variable on the left side
otherwise the compiler will raise an error.
Different types of assignment operators are shown below:

 “=”: This is the simplest assignment operator. This operator is used to assign
the value on the right to the variable on the left.
For example:
 a = 10;

 b = 20;

 ch = 'y';
 “+=”: This operator is combination of ‘+’ and ‘=’ operators. This operator first
adds the current value of the variable on left to the value on the right and
then assigns the result to the variable on the left.
Example:
 (a += b) can be written as (a = a + b)

If initially value stored in a is 5. Then (a += 6) = 11.

 “-=”This operator is combination of ‘-‘ and ‘=’ operators. This operator first
subtracts the current value of the variable on left from the value on the right
and then assigns the result to the variable on the left.
Example:
(a -= b) can be written as (a = a - b)

If initially value stored in a is 8. Then (a -= 6) = 2.

 “*=”This operator is combination of ‘*’ and ‘=’ operators. This operator first
multiplies the current value of the variable on left to the value on the right
and then assigns the result to the variable on the left.
Example:
 (a *= b) can be written as (a = a * b)

If initially value stored in a is 5. Then (a *= 6) = 30.

 “/=”This operator is combination of ‘/’ and ‘=’ operators. This operator first
divides the current value of the variable on left by the value on the right and
then assigns the result to the variable on the left.
Example:
 (a /= b) can be written as (a = a / b)
If initially value stored in a is 6. Then (a /= 2) = 3.

Below example illustrates the various Assignment Operators:

Output:

Value of a is 10
Value of a is 20
Value of a is 10
Value of a is 100
Value of a is 10

6. Other Operators: There are 2 types of operator-


1. Conditional Operator / Ternary Operator
2. Size of Operator

Conditional or Ternary Operator (?:) : The conditional operator is kind of similar


to the if-else statement as it does follow the same algorithm as of if-else
statement but the conditional operator takes less space and helps to write the if-
else statements in the shortest way possible.

Syntax:
The conditional operator is of the form
variable = Expression1 ? Expression2 : Expression3

It can be visualized into if-else statement as:

if(Expression1)
{
variable = Expression2;
}
else
{
variable = Expression3;
}
Since the Conditional Operator ‘?:’ takes three operands to work, hence they are
also called ternary operators.
Example: Program to Store the greatest of the two Number.
Output:

Largest number between 5 and 10 is 10.

sizeof operator: Sizeof is a much used operator in the C or C++. It is a compile


time unary operator which can be used to compute the size of its operand. The
result of sizeof is of unsigned integral type which is usually denoted by size_t.
sizeof can be applied to any data-type, including primitive types such as integer
and floating-point types, pointer types, or compound datatypes such as Structure,
union etc.
Usage
sizeof() operator is used in different way according to the operand type.
1. When operand is a Data Type.
When sizeof() is used with the data types such as int, float, char… etc it simply
returns the amount of memory is allocated to that data types.
Let’s see example:

Output:

1
4
4
8

Note: sizeof() may give different output according to machine, we have run our
program on 32 bit gcc compiler.

2. When operand is an expression.


When sizeof() is used with the expression, it returns size of the expression. Let see

Example:

Output:

8
As we know from first case size of int and double is 4 and 8 respectively, a is int
variable while d is a double variable. The final result will be a double, Hence the
output of our program is 8 bytes.

Need of Sizeof
1. To find out number of elements in a array.
Sizeof can be used to calculate number of elements of the array automatically. Let
see Example :

Output:

Number of elements: 11

You might also like