Operators in C languages


Operators are the symbols that are used to perform certain operations on data. C programming language provides a variety of operators. Different types of operators in C are as follows:

  • Relational operators
  • Increment and decrement operators
  • Compound assignment operators
  • Assignment operators
  • Logical operators
  • Arithmetic operators
  • Bitwise operators
  • Conditional operator
  • Special operators


Arithmetic operator:

Arithmetic operator is a symbol that performs mathematical operation on data. C language provides many arithmetic operators. Following is a list of all operators used in C:

Operation Symbol Description
Addition + Adds two values
Subtraction Subtracts one value from other
Multiplication * Multiplies two values
Division / Divides one values by other
Modulus % Gives the remainder of division of two integers


Relational operator:

The relational operators are used to specify conditions in programs. A relational operator compares two values. It produces result as true or false. The relational operators are sometimes called comparison operators as they test that are either true or false.

C programming language provides the following six basic relational operators:

Operator Description
> Greater than operator returns false if the value on right side of > is less than the value on the left side. Otherwise returns true.
< Less than operator returns true if the value on left side of < is less than the value on right side. Otherwise returns false.
== Equal to operator returns true if the values on both side of == are equal. Otherwise returns false.
>= Greater than or equal to returns false if value on the right side of >= is less than the value on the left side. Otherwise returns true.
<= Less than or equal to returns true if value on the left side of <= is less than the value on the right side. Otherwise returns false.
!= The not equal to operator returns true if the value on the left side of != is not equal to the value on the right side. Otherwise returns false.


Logical operator:

The logical operators are used to evaluate compound conditions. There are three logical operators in C programming language:

  1. AND operator (&&)
  2. OR operator (||)
  3. NOT operator (!)

AND operator:

The symbol for AND operator is (&&). It is used to evaluate two conditions. It produces true result if both conditions are true. It produces false result if any condition is false.

Condition 1 Operator Condition 2 Result
False && False False
False && True False
True && False False
True && True True

OR operator:

The symbol used for OR operator is (| |). It is used to evaluate two conditions. It gives true result if either condition is true. It gives false if either condition is false.

Condition 1 Operator Condition 2 Result
False && False False
False && True True
True && False True
True && True True

Not operator (!):

The symbol for not operator is (!). It is used to reverse the result of a condition.

Operator Condition Result
! True False
! False True


Assignment operator:

The assignment (=) is used in assignment statement to assign a value or computational result to a variable.

The name of the variable is written on the left side of the operator and the value is written on the right side of the operator. A statement that assigns a value to a variable is called assignment statement.

SYNTAX:

Variable = expression;

EXAMPLES:

A = 100;

C = A + B;


Increment and Decrement operator:

INCREMENT OPERATOR:

The increment operator is used to increment the value by 1. It is denoted by the symbol ++. It is a unary operator and works with single variable. Increment operator can be used in two forms.

  • Prefix form:

The increment operator is written before the variable as ++y.

  • Postfix form:

The increment operator is written after the variable as y++.

DECREMENT OPERATOR:

The decrement operator is used to decrease the value by 1. It is denoted by the symbol –. It is a unary operator and works with single variable. Decrement operator can be used in two forms.

  • Prefix form:

The decrement operator is written before the variable as –y.

  • Postfix form:

The decrement operator is written after the variable as y–.


Compound assignment operator:

C language provides compound assignment operators that combine assignment operator with arithmetic operators. Compound assignment operators are used to perform mathematical operations more easily.

SYNTAX:

Variable op = expression;

EXAMPLE:

N += 10 which is equivalent to N = N + 10;


Bitwise operators:

There are some bitwise operators that are supported by C programming language. These operators can be applied to integer data type, long, short, char and byte and not on float and double data types. The bitwise operator performs the bit by bit (bit level) operation and it works on bits. The following are the bitwise operators used in C programming language:


Bitwise & operator:

Suppose that you have two variables ‘a’ and ‘b’:

a = 0011 1100

b = 0000 1101

Then expression (a & b) will be equal to 0000 1100. The bitwise AND operator results a 1 if the bits in both of the operands are 1, and 0 if the bits in the operands are different or 0.


Bitwise OR operator:

Suppose that you have two variables ‘a’ and ‘b’:

a = 0011 1100

b = 0000 1101

Then the expression (a | b) will be equal to 0011 1101. The bitwise OR operator results a 1 if one of the bits in the operands is 1 and 0if both of the bits in the operand is 0.


Bitwise XOR operator:

Suppose that you have two variables ‘a’ and ‘b’:

a = 0011 1100

b = 0000 1101

Then the expression (a ^ b) will be equal to 0011 0001. The bitwise XOR operator results 1 if the bits in the two operands are different and a 0 if the bits in the two operands are same.


Left shift:

The left shift is used to move the value of the left operand to number of bits specified by the right operand to the left side.

Suppose that you have two variables ‘a’ and ‘b’:

a = 0011 1100

b = 2

Then the expression (a << b) will be equal to 1111 0000.


Right shift operator:

The right shift operator is used to move the value of the left operand to number of bits specified by the right operand to the right side.

Suppose that you have two variables ‘a’ and ‘b’:

a = 0011 1100

b = 2

Then the expression (a >> b) will be equal to 1111.


Special Operators:

In C programming language the following are some special operators:

Operator Description
sizeof This operator is used to return the size of a variable.
& The ‘&’ operator is used to return the address of a variable.
* It is used to refer a pointer to a variable.


Example of special operators in C:

sizeof:  It is used as sizeof(y) and y is the variable whose size is to be returned.

&: It is used as &x to return the address of the variable x.

*: It is used as *y to refer pointer to variable y.


Conditional operator:

Conditional operator is referred to as a decision making structure. It can be used in place of simple if else structure. It is also called ternary operator as it uses three operands.

SYNTAX:

The syntax of conditional operator is as follows:

(condition) ? true case statement: false case statement;

Condition: is specified as relational or logical expression. The condition is evaluated as true or false.

True case: it is executed if expression evaluates to true.

False case: it is executed if expression evaluates to false.

EXAMPLE:

Consider the following example in which we have used conditional operator applied on the variable A.

X = (A>50) ? 1 : 0;

Will assign 1 to X if the condition A>50 is true. It will assign 0 to X if the condition is false. The above statement can be written using if else statement as follows:

If (A>50)

X=1;

else

X=0;