Operators in C

Operators are play very important role in any programming languages. The symbols which are used to perform logical and mathematical operations in a C program are called C operators.

Types of Operators

There are 8 types of operators in c language list is given below.

  1. Arithmetic operators
  2. Assignment operators
  3. Relational operators
  4. Logical operators
  5. Bit wise operators
  6. Conditional operators (ternary operators)
  7. Increment/decrement operators
  8. Special operators

1. Arithmetic operators

C Arithmetic operators are used to perform mathematical/arithmetic operations. For Examples: addition, multiplication, subtraction, division and modulus in C programs.

There are two types of Arithmetic operators

  1. Unary Arithmetic operators: Operators that operates or works with a single operand are unary operators.
  2. Binary Arithmetic operators: Operators that operates or works with two operands are binary operators.

Suppose variable M = 10 and N = 5

OperatorsDescriptionExample
+(Addition)Add two operandsM+N = 15
-(Subtraction)Subtract two operandsM-N = 5
*(Multiplication)Multiply both operandsM*N = 50
/(Division)Divide numerator by denominatorM/N = 2
%(Modulus)Modulus Operator and remainder of after an integer divisionM%N = 0
++(Increment)Increment operator: increases integer value by oneN++ = 6
--(Decrement)Decrement operator: decreases integer value by oneN-- = 4

2. Assignment operators

C assignment operators are used to assigning a value to a variable. For Examples: equal to (=) to use assign value to a variable in C programs.

Operators DescriptionExample
= assigns values from right side operands to left side operandM=N
+= adds right operand to the left operand and assign the result to leftM+=Nb is same as M=M+N
-= subtracts right operand from the left operand and assign the result to left operandM-=N is same as M=M-N
*= mutiply left operand with the right operand and assign the result to left operandM*=N is same as M=M*N
/= divides left operand with the right operand and assign the result to left operandM/=N is same as M=M/N
%= calculate modulus using two operands and assign the result to left operandM%=N is same as M=M%N

3. Relational operators

A relational operator is a programming language construct or operator that comparison or defines some kind of relation between two operands.If the relation between two operands are true, it returns 1; if the relation is false, it returns 0.

Suppose M=10 and N=5

Operator Description Examples
== Equal to M == N returns 0
> Greater than M > N returns 1
< Less than M < N>
!= Not equal to M != N returns 1
>= Greater than or equal to M >= N returns 1
<= Less than or equal to M <= N return 0

4. Logical operators

C logical operators are used to perform logical operations on the given expressions. The output of the operation of a logical operator is a boolean value either 1(true) or 0(false). Logical operators are commonly used in decision making in C programming.

There are 3 types of logical operators in c language list is given below.

  1. AND (&&)
  2. OR (||)
  3. NOT (!)
Operators Description Examples
&& Logial AND. True only if all operands are true (A && B) is false.
|| Logical OR. True only if either one operand is true (A || B) is true.
! Logical NOT. True only if the operand is 0 !(A && B) is true.

5. Bitwise operators

Bitwise operators are used in C programming to perform bit-level operations.

OperatorsDescription
&Bitwise AND
|Bitwise OR
^Bitwise XOR
~Bitwise complement
<<Bitwise left shift
>>Bitwise right shift

6. Conditional(Ternary) operators

Conditional operator is also known ternary operator used to evaluate an expression based on some condition. It is a replacement of short if else statement.

conditionalExpression ? expression1 : expression2

7. Increment/decrement operators

Increment and Decrement operator are used to increment or decrement value by 1.

There are two variants of increment/decrement operator.

  1. Prefix
  2. Postfix
SyntaxDescriptionExample
++Pre increment++a
++Post incrementa++
--Pre decrement--a
--Post decrementa--

8. Special operators

Apart from the above operators there are some special operators available in C or C++ used to perform some specific task. Some of them are discussed here:

sizeof operator: sizeof is a much used in the C programming language. 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. Basically, sizeof operator is used to compute the size of the variable. To learn about sizeof operator in details you may visit this link.

Comma Operator: The comma operator (represented by the token ,) is a binary operator that evaluates its first operand and discards the result, it then evaluates the second operand and returns this value (and type). The comma operator has the lowest precedence of any C operator. Comma acts as both operator and separator. To learn about comma in details visit this link.