Operator in C
Operator in C
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
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:
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
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
Examples:
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
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
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
Signed Result -2
Unsigned Result 4294967294d
“=”: 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)
“-=”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)
“*=”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)
“/=”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.
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
Syntax:
The conditional operator is of the form
variable = Expression1 ? Expression2 : Expression3
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:
Output:
1
4
4
8
Note: sizeof() may give different output according to machine, we have run our
program on 32 bit gcc compiler.
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