BEST TUTORIAL FOR PROGRAMMING        
BEST TUTORIAL FOR PROGRAMMING

C Operators

What is operator? : An operator is a symbol or a sign that is used to perform certain operation like '+' symbol operator (addition operator) which takes two operands and gives addition of both the operands. Operators are the key parts of any programming language. C language provides a rich set of built-in operators that help us to perform various mathematical and logical operations. Operators are mainly used in programming for calculating and manipulating data and variables.

For example : 14 + 12 = 26




Types of operators

C language provides various types of operators to perform various operations in C programs. We can classify C operators like this.

1 

Arithmetic operators in C

2 

Assignment operators in C

3 

Relational operators in C

4 

Logical operators in C

5 

Bitwise operators in C

6 

Conditional operator in C

7 

Unary operator in C

8 

Special Operators in C




Now we will see all of them one by one in brief.

Arithmetic operators in C

Arithmetic operators are used to perform arithmetic or mathematical operations in C programming like addition, subtraction, multiplication/, etc. These operators are also called binary operators because these operators take two operands to perform any arithmetic operation.



The following table shows all the arithmetic operators of C with examples. Here, we are assuming that variable X holds 10 and variable Y holds 20.

Operator Description Example
+ Adds two operands X + Y = 30
- Subtracts second operand from first operand Y - X = 10
* Multiplies two operands X * Y = 200
/ Divides numerator by denumerator Y / X = 2
% Remainder of division (Modulus operator) Y % X = 0



Assignment operators in C

Assignment operators are used to assign or store value into a variable. These operators take two operands as left side operand (variable) and right side operand (value).

For example : age = 18

Here, age is an integer variable and we are assigning 18 to this variable using equal '=' assignment operator.



Assume, we have two variables like this.

int x = 10;
int y = 20;


The following table shows all the assignment operators of C with examples.

Operator Description Example Result
= Assigns value of right side operand to left side operand x = y value of x is 20
+= First adds right side operand to the left side operand and at last assign the result to the left side operand x += y is same as x = x + y value of x is 30
-= First subtracts right side operand from the left side operand and at last assign the result to the left side operand y -= x is same as y = y - x value of y is 10
*= First multiplies left side operand with the right side operand and at last assign the result to the left side operand x *= y is same as x = x * y value of x is 200
/= First divides left side operand with the right side operand and at last assign the result to the left side operand y /= x is same as y = y / x value of y is 2
%= First calculates modulus (remainder) using left side operand and right side operand and at last assign the result to the left side operand y %= x is same as y = y % x value of y is 0



Relational operators in C

Relational operators are used to compare two operands or check relation between two operands. These relational operators return result as true or false (boolean value). Relational operators are also known comparison operators.



Assume, we have two variables like this.

int x = 10;
int y = 20;


The following table shows all the relational operators of C with examples.

Operator Description Example Result
== Checks that both operands are equal or not, if equal, then returns true otherwise false. x == y false
!= Checks that both operands are equal or not, if not, then returns true otherwise false. x != y true
> Checks that left side operand is greater than right side operand, if yes, then returns true otherwise false. x > y false
< Checks that left side operand is smaller than right side operand, if yes, then returns true otherwise false. x < y true
>= Checks that left side operand is greater than or equal to right side operand, if yes, then returns true otherwise false. x >= y false
<= Checks that left side operand is smaller than or equal to right side operand, if yes, then returns true otherwise false. x <= y true



Logical operators in C

Logical operators are used to check operands logically and combine two or more conditions. C provides three types logical operators like And (&&), OR (||) and NOT (!). These logical operators return result as true or false (boolean value).



Assume, we have two variables like this.

int x = 1;
int y = 0;


The following table shows all the logical operators of C with examples.

Operator Description Example Result
&& If both the operands are non-zero, then returns true otherwise false. (x && y) false
|| If any operand from both operands is non-zero, then returns true otherwise false. (x || y) true
! It is used to reverse the logical state. If logical state is true, then makes it false and if logical state is false, then makes it true. !(x && y) true



Bitwise operators in C

Bitwise operators are used to perform bit level operations on operands. We can also use these operators to perform bits shifting left to right and right to left but we can not use C bitwise operators with floating data types.



Assume, we have two variables like this.

int x = 7;
int y = 10;

Bits value of these variables are x = 00000111 and y = 00001010



The following table shows all the bitwise operators of C with examples.

Operator Description Example Result
& (Binary AND) Takes two operands as two bits sets and does AND (&) on every bit of both bits sets. If both the bits sets have 1 (non-zero) on same position of bits, then result will be 1, otherwise 0. (x & y) 2 (00000010)
| (Binary OR) Takes two operands as two bits sets and does OR (|) on every bit of both bits sets. If any bits set from both the bits sets has 1 (non-zero) on same position of bits, then result will be 1, otherwise 0. (x | y) 15 (00001111)
^ (Binary XOR) Takes two operands as two bits sets and does XOR (^) on every bit of both bits sets. If both the bits sets have different bits on same position of bits, then result will be 1, otherwise 0. (x ^ y) 13 (00001101)
~ (Binary Complement) Takes one operand as bits set and does NOT (~) on every bit of bits sets. It inverts all bits of the bits set like 1 to 0 and 0 to 1. (~x) -8 (11111000)
<< (Binary Left Shift) Takes two operands, first as bits set and second as a number which decides the number of places to shift left bits of the first operand. x << 2 28 (00011100)
>> (Binary Right Shift) Takes two operands, first as bits set and second as a number which decides the number of places to shift right bits of the first operand. y >> 1 5 (00000101)



Conditional operator in C

Conditional operator takes three operands that is why it is called ternary operator in C. A conditional operator in C gives result according condition. We can consider it as a short form of if-else control statement of C language. In Coming sections, we will learn about C if-else control statements in detail.



Syntax of ternary operator in C is given below :
expression1 ? expression2 : expression3;

  • expression1 is a condition and its evaluation can be true or false.

  • If evaluation of expression1 is true, then expression2 will be executed.

  • If evaluation of expression1 is false, then expression3 will be executed.


Examples of ternary operator in C are given below :
( 20 > 10 ) ? 20 : 10

Output = 20

Or

( 20 < 10 ) ? 20 : 10

Output = 10



Unary operator in C

An operator that need only a single operand to perform operations is called unary operator in C. The mostly used unary operators in C language are increment and decrement operators.



Assume, we have a variable like this.

int x = 10;


The following table shows all the unary operators of C with examples.

Operator Description Example Result
++ Increases the current value of numeric variable by adding 1. ++x or x++ 11
-- Decreases the current value of numeric variable by subtracting 1. --x or x-- 9



Special Operators in C

C provides some special operators for performing specific tasks in C programming.



Assume, we have a variable like this.

int x = 10;


The following table shows all the special operators of C with examples.

Operator Description Example Result
sizeof() Returns the size of a variable. sizeof(x) 2
& Returns the actual memory address of a variable. &x 14121993
* Returns value of C pointer variable. *a 10
, Combines two or more variables in a single statement. x , y , z ; int x, y, z;



C operators precedence and associativity

The precedence of operators determine that which operator will be evaluated first and which operator will be the next in an expression. Each C operator has a predefined precedence.

The associativity of operators determine direction of an expression to be evaluated, it can be left to right or right to left.



For Example :

Assume, we have this expression ' 16 * ( 2 + 13 ) / 5 ' for evaluating.

16 * ( 2 + 13 ) / 5

First, we will evaluate Parentheses ' ( 2 + 13 ) ' values.

16 * 15 / 5

After that, we will evaluate ' 15 / 5 ' values.

16 * 3

At last, we will evaluate ' 16 * 3 ' values.

48

48 is final result.



The following table shows the precedence and associativity of C operators.

Category Operator Associativity
Postfix () [] -> . ++ -- Left to right
Unary operators + - ! ~ ++ -- * & (type) sizeof Right to left
Arithmetic operators * / % Left to right
Additive operators + - Left to right
Shift operators << >> Left to right
Relational operators < <= > >= Left to right
Equality operators == != Left to right
Bitwise AND & Left to right
Bitwise XOR ^ Left to right
Bitwise OR | Left to right
Logical AND && Left to right
Logical OR || Right to right
Conditional operator ?: Left to left
Assignment operators = += -= *= /= %= &= ^= |= <<= >>= Right to left
Comma operator , Left to right



INFO : In coming sections, we will learn more about C operators with help of C programs.


Next Topic : Constants in C