Home >>C Tutorial >Operators in C

Operators in C

Operators in C

The operators in C language are basically a symbol that conveys the compiler to perform logical or specific mathematical functions. The C language is known to be rich in built-in operators.

Here are the following operators in the C language:

  • Arithmetic Operators
  • Logical Operators
  • Assignment Operators
  • Bitwise Operators
  • Relational Operators
  • Misc Operators

1. Bitwise Operators

The Bitwise operators in the C language, works on the bits and execute bit-by-bit operation. Please find the truth tables for &, |, and ^ below:

k j k|(OR)j k&(AND)j k ^(XOR) j
0 0 0 0 0
0 1 1 0 1
1 1 1 1 0
1 0 1 0 1

Let's suppose A = 60 and B = 13 in binary format, the values will be as follows:

A = 0011 1100
B = 0000 1101
A&B = 0000 1100
A|B = 0011 1101
A^B = 0011 0001
~A = 1100 0011

Here are the list of the Bitwise operators that are supported by the C language. Let’s suppose variable 'A' holds 60 and variable 'B' holds 13, then:

Operator Description Example
& Binary AND Operator basically copies a bit to the result if it exists in both operands. (A & B) = 12, i.e., 0000 1100
| Binary OR Operator basically copies a bit if it exists in either operand. (A | B) = 61, i.e., 0011 1101
^ Binary XOR Operator basically copies the bit if it is set in one operand but not both. (A ^ B) = 49, i.e., 0011 0001
~ Binary One's Complement Operator is generally unary and has the 'flipping' bits effects. (~A ) = ~(60), i.e,. -0111101
<< This is the Binary Left Shift Operator. The left operands value is generally moved left by the number of bits that are specified by the right operand moves the value of left operand. A << 2 = 240 i.e., 1111 0000
>> This is the Binary Right Shift Operator. The left operands value is generally moved right by the number of bits that are specified by the right operand. A >> 2 = 15 i.e., 0000 1111

2. Arithmetic Operators

All the arithmetic operators that are supported by the C language are displayed in the following table. Let's suppose variable A holds 10 and variable B holds 20 then:

Operator Description Example
+ Used to add two operands. A + B = 30
- Used to subtract second operand from the first. A − B = -10
* Used to multiply both operands. A * B = 200
/ Used to divide numerator by de-numerator. B / A = 2
% It is a modulus operator and remainder of after integer division. B % A = 0
++ Increment operator generally increases the integer value by one. A++ = 11
-- Decrement operator generally decreases the integer value by one. A-- = 9

3. Logical Operators

Here is the table showing all the logical operators that are supported by the C language. Let's suppose that the variable A holds 1 and variable B holds 0:

Operator Description Example
&& It is called Logical AND operator. The condition becomes true, if both the operands are non-zero. (A && B) is false
|| It is called Logical OR Operator. The condition becomes true, if any of the two operands is non-zero. (A || B) is true
! It is called Logical NOT Operator and generally used to reverse the logical state of its operand. Suppose a condition is true then Logical NOT operator will make it false !(A && B) is true

4. Relational Operators

Here is the table depicting all the relational operators that are supported by the C Language. Let's suppose that the variable A holds 10 and variable B holds 20 then:

Operator Description Example
== This operator checks the values of two operands are equal or not and If the values of two operands are not equal, then the condition becomes true. (A == B) is not true.
!= This operator Checks if the values of two operands are equal or not. The condition becomes true if the values are not equal. (A != B) is true.
> This operator checks if the value of left operand is greater than the value of right operand. If the values of left operator is greater then the condition becomes true. (A > B) is not true.
< This operator checks if the value of left operand is less than the value of right operand. If the value of l (A < B) is true.
>= This operator checks if the value of left operand is greater than or equal to the value of right operand. If the value of left operator is greater then the condition becomes true. (A >= B) is not true.
<= This operator checks if the value of left operand is less than or equal to the value of right operand. If the value of left operator is less then the condition becomes true. (A <= B) is true

5. Assignment Operators

Here is the lists of the assignment operators that are supported by the C language along with a description and example:

Operator Description Example
= This is a simple assignment operator. This operator assigns values from the right side operands to the left side operands. M = K + L will get assigned the exact value of K + L to M
+= This is known as Add AND assignment operator. As the name suggests it adds the right operand to the left operand and also assigns the result to the left operand. M += K will be equal to M = M + K
-= This is basically a subtract AND assignment operator. As the name suggests it subtracts the right operand from the left operand and also ==assigns the result to the left operand. M -= K will be equal to M = M – K
*= This is basically a multiply AND assignment operator. This operator is generally used to multiply the right operand with the left operand and assigns the result to the left operand. M *= K will be equal to M = M * K
/= This is a divide AND assignment operator. It is used to divide the left operand with the right operand and also assigns the result to the left operand. M /= K will be equal to M = M / K
%= This is known as modulus AND assignment operator. It calculates modulus using two operands and assigns the result to the left operand. M %= K will be equal to M = M % K
<<== This is known as Left shift AND assignment operator. M <<= 2 will mirror the value of M = M << 2
>>== This is known as Right shift AND assignment operator. M >>= 2 will mirror the value of M = M >> 2
&= This is known as Bitwise AND assignment operator. M &= 2 will mirror the value of M = M & 2
^= This is known as Bitwise exclusive OR and assignment operator. M ^= 2 will mirror the value of M = M ^ 2
|= This is known as Bitwise inclusive OR and assignment operator. M |= 2 will mirror the value of M = M | 2

6. Misc. Operators

Apart from the above mentioned operators in the C language, there are still a few other more important operators including sizeof and ? : that are supported by the C programming Language:

Operator Description Example
sizeof() This operator generally returns the size of a variable. sizeof(a), where a is integer, will return 4.
& This operator generally returns the address of a variable. &a; returns the actual address of the variable.
* This operator is a pointer to a variable. *a;
? : This operator is a conditional Expression. If Condition is true ? then value X : otherwise value Y

Operator Precedence in the C Language

The operator precedence in the C language generally determines the grouping of terms in an expression and it is responsible for deciding the evaluation of an expression. There are certain operators in C languages that have a higher precedence than others here is an example: the multiplication operator has a higher precedence than the addition operator in C.

Let's take an example for a better understanding a = 7 + 3 * 2; here, a is assigned 13, not 20 because operator * has a higher precedence than + operator hence, it gets multiplied first with 3*2 and then it gets added into 7.

In the following table, operators that have the highest precedence, appear at the top of the table and the operators with the lowest precedence appear at the bottom of the table. Within an expression, the operators with higher precedence will be evaluated first.


Popular Tutorials