Operators in C

Learn via video course
FREE
View all courses
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
Topics Covered

Overview

C Supports a rich set of built-in Operators. Operators are symbols that are used to perform some operation or a set of operations on a variable or a set of variables. C has a set of operators to perform specific mathematical and logical computations on operands.

Introduction to Operators in C Language

operators in C are that symbols which work on operands. Operator in C language is used to perform specific mathematical or logical computations on the operands and it reduces a single value.

Operators in C language, are classified into several categories.

  1. Arithmetic Operators
  2. Relational Operators
  3. Shift Operators
  4. Logical Operators
  5. Bitwise Operators
  6. Ternary or Conditional Operators
  7. Assignment Operators
  8. Misc Operators
  9. Special Operators

Operators in C Language

Arithmetic Operators

An arithmetic operator is used to perform arithmetic/mathematical operations on operands. Some of the arithmetic operators are (+, -, *, /, %, ++, --)

OperatorName of OperatorWhat it doesHow it is used
+Unary PlusAdd two Operandsa+b
-Unary MinusSubtracts the second operand from the first.a-b
*MultiplicationMultiplies both operands.a*b
/DivisionDivides numerator by de-numerator.a/b
%Modulusreturn remainder, after an integer division.a%b
++Increment Operatorincreases the integer value by one.a++
- -Decrement Operatordecreases the integer value by one.a- -

Relational Operators

Relational operators help in making a relationship or comparison between two operands with which they are used. Hence, relational operators help us make decisions in the program and their end result is either true or false. Some of the relation operators are (==, !=, <, >, <=, >=)
Example:

The expression given above, we used an equality operator that means it checks the value of a and b if both values are the same then it will return true otherwise it will return false.

OperatorName of OperatorWhat it doesReturn value
==Equality Operatorchecks if a == bBoolean
!=Not equal tochecks if a != bBoolean
<Less thanchecks if a < bBoolean
>Greater thanchecks if a > bBoolean
<=Less than or equal tochecks if a<=bBoolean
>=Greater than or equal tochecks if a>=bBoolean

Shift Operators

The Shift Operators is used when we want to shift a binary bit either in the left direction or right direction.
Shift Operators are classified into two categories C Language:

  • Left Shift Operator: Left Shift Operator performs operations on the binary bits. The left shift operator is a type of binary operator so we need two operands to shift the position of the bits to the left side and add zeroes to the empty space on the right side after shifting the bits.

Syntax:

The output of the left shift operator, will be equivalent to multiplying varName with 2 ^ no_of_position (2 raised to power no_of_position)

Example:

  • Right Shift Operator: Right Shift Operator performs operations on the binary bits. The Right shift operator is a type of binary operator so we need two operands to shift the position of the bits to the right side and add zeroes to the empty space on the left side after shifting the bits.

Syntax:

The output of the right shift operator, will be equivalent to dividing varName with 2^no_of_position (2 raised to power no_of_position)

Example:

OperatorName of the OperatorWhat it doesHow it is used
<<Left Shift Operatorshifts the number of bits to the left sidea << 1
>>Right Shift Operatorshifts the number of bits to the right sidea >> 2

Logical Operators

The logical operators are used when we want to check or test more than one condition and make decisions. Some of the logical operators are(&&, ||, !).
Example:

The logical expression given above is true only if a > b is true and x == 100 is true. if either (or both) of them are false, the expression is false.

OperatorName of the operatorWhat it doesHow it is used/output
&&logical ANDreturns true if both side operands value is true otherwise returns falseBoolean
||logical ORreturns true if one of the operand's value is true or both of the operand's values is true otherwise returns falseBoolean
!logical Notreturns true if the condition in consideration is not satisfied Otherwise returns falseBoolean

Bitwise Operators

An Bitwise operator is used for the manipulation of data at the bit level. These operators are not applied for the float and double datatype.

Bitwise operator first converts the integer into its binary representation then performs its operation. Bitwise operators subsist of two digits, either 0 or 1. Some of the bitwise operators are (&, | , ^, ~)

Note: Shift Bitwise operators are used to shift the bits right to left. Some of the shift bitwise operators are(<<, >>)

We use the following truth table for the Bitwise Operators:

ABA & B (Bitwise AND)A | B (Bitwise OR)A ^ B (Bitwise XoR)
11110
01011
10011
00000

Example:

In the above example, We have two variables a and b.
a = 5 (In Decimal);
b = 6 (In Decimal);
So, a's binary representation is 0101(5) and b's binary representation is 0110(6)

OperatorName of OperatorWhat it doesHow it is used
&bitwise ANDbitwise AND operator do AND of every corresponding bits of both operands and output 1 (true) if both operands have 1 at that position otherwise 0(false).a & b
|bitwise ORbitwise OR operator do OR operation of every corresponding bits of both operands and output 0 (false) if both operands have 0 at that position otherwise 1(true).a | b
~bitwise complementperforms complement operation on an operand and bitwise complement changes 1 to 0 and 0 to 1~a
^bitwise exclusive ORreturns 1 if the corresponding bits of two operands are opposite else 0a^b
<<shift leftshifts the number of bits to the left sidea << 1
>>shift rightshifts the number of bits to the right sidea >> 1

Ternary or Conditional Operators

the ternary or conditional operators is used to construct the conditional expression. A conditional operator pair "?:"
Syntax:

Here exp1, exp2, exp3 are expressions.
The Operator ?: works as follows: exp1 is evaluated first. If it is true, then the expression exp2 is evaluated and becomes the value of the expression. If exp1 is false, then exp3 is evaluated and its value becomes the value of the expression.

Example:

In the above example, we have two variables a and b. x, will be assigned the value of b because a>b is false.

Misc Operators

Misc Operator is type of Operator in C language. It is also called Miscellaneous Operator. Some of the Misc operators are (sizeof() ?: , & * )

Example:

OperatorName of OperatorWhat it doesHow it is used
sizeof()sizeofIt returns the size of variableif variable a is a interger variable the sizeof(a) will return 4
?:conditional or ternary operatorif the condition is true then it returns the value of x else value of ycondition?x
casttype castit converts one datatype to another datatypeint(5.260) would return 5
,comma operatorUsed to link the related expressions togethera = (1,2,3) would return 3
&Address Operatorreturns the address of the variable.&a
*pointer operatorpointer to a variable*a

Assignment Operators

An assignment operator is used to assign values to the operands. Some of the assignment operators are (=, +=, -=, *=, /=, %=)
Eample:

In the above example, we are assigning b's value to variable a.

OperatorName of OperatorWhat it doesHow it is used
=assignmentassign value of variable b to variable aa = b
+=plus assigna = a+b (adds values of a to b and assign this value to a)a += b
-=minus assigna = a-b (subtracts values of b from a and assign this value to a)a -= b
*=times assigna = a*b (Multiplies a with b and assign the value to a)a *= b
/=div assigna = a/b (divides a by b and assigns the value to a)a /= b
%=Mod assigna = a%b (divides a by b and assigns the value of the remainder to a)a %= b

Special Operators

C supports some special operators some of the special operators are (comma operator, address operator, size of operator, pointer operator)

Example:

OperatorName of OperatorWhat it doesHow it is used
,CommaUsed to link the related expressions togethervalue = (x=10, y=5)
&Address Operatorreturns the address of the variable.&a
sizeof()sizeofreturns the size of a variablem = sizeof(a)

Let's Understand the sizeof() operator with the help of program

Output:

Note: sizeof() may give different output according to the compiler.

Precedence(or priority) and Associativity of Operators in C

Precedence determines which operator is performed first in an expression if there are more than one operator of different precedence(lower precedence means higher priority). Associativity determines in which direction we should start computing the operators having the same precedence. The table shows the precedence and their associativity among operators.

TokenOperatorPrecedenceAssociativity
()function call1left-to-right
[]array element1left-to-right
++postfix increament1left-to-right
- -postfix decreament1left-to-right
++prefix increament2right-to-left
- -prefix decreament2right-to-left
+unary plus2right-to-left
-unary minus2right-to-left
!Logical negation2right-to-left
~one's complement2right-to-left
*indirection2right-to-left
&address2right-to-left
sizeofsize(in bytes)2right-to-left
(type)type cast2right-to-left
*multiplication3left-to-right
/division3left-to-right
%modulus3left-to-right
+addition4left-to-right
-subtraction4left-to-right
<<left shift5left-to-right
>>right shift5left-to-right
<less than6left-to-right
<=less than or equal to6left-to-right
>greater than6left-to-right
>=greater than or equal to6left-to-right
==equality7left-to-right
!=inequality7left-to-right
&bitwise AND8left-to-right
^bitwise XOR9left-to-right
|bitwise OR10left-to-right
&&Logical AND11left-to-right
||Logical OR12left-to-right
?:conditional expression13right-to-left
= *= /= %= += -= &= ^= |= <<= >>=assignment operators14right-to-left
,comma operator15left-to-right

Let's understand the precedence(or priority) operator with the help of programming example.

Example 1:

Output:

explanation: priority or precedence for the values assigned to any variable is given from left to right.

Example 2:

Output:

explanation: priority or precendence for the values indside brackets () assigned to any variable is given from right to left.

Conclusion

  • We perform operations on variables or operands by using operators in C
  • An expression is the combination of operands and operators that reduces a single value.
  • Arithmetic operators in C are used for mathmatical operations and include (+, -, *, /, %, ++, --).
  • Relational operators in C are used to compare 2 variables and include (==, !=, <, >, <=, >=).
  • Shift operators in C are used to shift bits of a number and include (<<, >>).
  • To check more than one condition we use logical operators and they include (&&, ||, !).
  • Bitwise operator are used to manipulate bits of a number and include (&, | , ^, ~).
  • Ternary operator (?) are used as a short-hand to write if-else condition.
  • Miscellaneous operator include (sizeof() ?: , & * ).
  • To assign values we use assignment operators in C and they include (=, +=, -=, *=, /=, %=).
  • We also understood the priority of operators in C.

See Also: