×

Operators in C

An operator is a special symbol that tells the compiler to perform specific mathematical or logical operations. It is the combination of constants and variables through expressions. Example:
int c = a+b*5
Where, =, +,* are operators, a,b,c are the variables and  5 is the constants. There are various types of the operator in C Language.

Types of Operator
  • Arithmetic Operators
  • Relational Operators
  • Logical Operators
  • Bitwise Operators
  • Assignment Operators
  • Misc Operators
  • Increment/Decrement operators
Arithmetic Operators
Arithmetic operators are used to perform mathematical calculations like addition, subtraction, multiplication, division, and modulus.

 
Operator Example (int a=9, b=2) Result
+ A+B 11
- A-B 7
* A*B 18
/ A/B 4.5
% A%4 1

Example:
#include <stdio.h>
int main()
{
int a=9,b=2, add,sub,mul,div,mod;
add = a+b;
sub = a-b;
mul = a*b;
div = a/b;
mod = a%b;
printf("Addition of (a+b ) is : %d\n", add);
printf("Subtraction of (a-b) is : %d\n", sub);
printf("Multiplication of (a*b) is : %d\n", mul);
printf("Division of (a / b) is : %d\n", div);
printf("Modulus of (a%b) is : %d\n", mod);
}
Output:
Addition of (a+b ) is : 11
Subtraction of (a-b) is : 7
Multiplication of (a*b) is : 18
Division of (a / b) is : 4
Modulus of (a%b) is : 1
 Relational Operators Relational operators are used to compare the value of two variables.
Operator Description
> x > y (x is greater than y)
< x < y (x is less than y)
>= x >= y (x is greater than or equal to y)
<= x <= y (x is less than or equal to y)
== x == y (x is equal to y)
!= x != y (x is not equal to y)

Let us consider an example
#include <stdio.h>
int main()
{
int a=9,b=2;
if (a == b)
{
printf("a and b are equal");
}
else
{
printf("a and b are not equal");
}
}
Output
a and b are not equal
Logical Operators
Logical operators are used to perform logical operations on the given two variables. There are 3 logical operators in C LanguageAND(&&), OR ( || ) and NOT (!).
Operator Description
&& It returns true when both conditions are true.
|| It returns true while at least one condition is true.
! It reverses the state of the operand.

Let us consider an example
#include <stdio.h>
int main()
{
int a=8, b=3, c=10;
if(a>b && b<c){
printf("$$Operator : Both Condition are true \n");
}
if(a>b || b<c){
printf("||Operator : Only one condition is true \n");
}
return 0;
}
Output
$$Operator : Both Condition are true 
||Operator : Only one condition is true
 Assignment operators
Assignment operators are used to assign the values for the variable.
Operator Description
= sum = 10; 10 is assigned to variable sum
+= sum += 10; This is same as sum = sum + 10
-= sum -= 10; It is same as sum = sum – 10
*= sum *= 10; It is same as sum = sum * 10
/= sum /= 10; It  is same as sum = sum / 10
%= sum %= 10; It  is same as sum = sum % 10
&= sum&=10; It  is same as sum = sum & 10
^= sum ^= 10; It is same as sum = sum ^ 10

Let us consider an example
#include <stdio.h>
int main(){
int a=8,b=3,c;
c=a+b;
printf("a+b value is assign to c");
return 0;
}
Output
a+b value is assign to c
Operator Precedence in C The precedence of operators are used to specify the operators which will be evaluated first and next. Example:
int  result = 5+5*10-2;
The precedence and associatively of C operators is given below:
Category Operator Associativity
Postfix () [] -> . ++ - - Left to right
Unary + - ! ~ ++ - - (type)* & sizeof Right to left
Multiplicative * / % Left to right
Additive + - Left to right
Shift <<>> Left to right
Relational <<= >>= Left to right
Equality == != 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 || Left to right
Conditional ?: Right to left
Assignment = += -= *= /= %=>>= <<= &= ^= |= Right to left
Comma , Left to right

 Miscellaneous operators in C
There are few other important operators including sizeof and ?: supported by C Language.
Operator Description
sizeof() It returns the size of a variable.
& It returns the address of a variable.
* It assigns pointer to a variable.
? : It is a Conditional Expression.

Bitwise operators
Bitwise operator is used to perform bit by bit operations on two variables. There are following truth table for &, | and ^.
P q p & q p | q p ^ q
0 0 0 0 0
0 1 0 1 1
1 1 1 1 0
1 0 0 1 1
 
Operator Description
&   Binary AND Operator It copies a bit to the result if it exists in both operands.
|   Binary OR Operator It copies a bit if it exists in either operand.
^   Binary XOR Operator It copies the bit if it is set in one operand but not both.
~   Binary Ones Complement Operator It is unary and has the effect of 'flipping' bits.
<<  Binary Left Shift Operator The left operands value is moved left by the number of bits specified by the right operand.
>> Binary Right Shift Operator. The left operands value is moved right by the number of bits specified by the right operand.

Increment / decrement operator Increment Operator: The increment operator is used to increase the value of the variable by one.

Syntax:
Increment operator: ++ var_name; or var_name++;
Example:
Increment operator: ++i; i++;
Decrement Operator:
Decrement operator is used to decrease the value of the variable by one.

Syntax:
Decrement operator: -- var_name; or var_name--;
Example:
Decrement operator: --i;  i--;
Difference between Increment & Decrement operators
There are various differences between pre/Post increment & decrement operators in C.

 
Operator Operator/Description
Pre increment operator (++i) The value of i is incremented before assigning it to the variable i.
Post increment operator (i++) The value of i is incremented after assigning it to the variable i.
Pre decrement operator (- -i) The value of i is decremented before assigning it to the variable i.
Post decrement operator (i--) The value of i is decremented after assigning it to variable i.

Related Topics

Entry Control Loop in C

Initially, an entry control loop verifies the termination condition at the entry point. After that, its control passes to the main body of the while or for loop if the...

3 minutes read.

How to Calculate Time Complexity in C?

What is time complexity? An algorithm's time complexity measures how long it takes to complete a task in relation to the size of the input. It should be noted that the...

5 minutes read.

Pre-increment and Post-increment in C

The rich set of operators is supported by the c language. In c there are several operators used. The mathematical operations in the programming language are done with the operators...

4 minutes read.

Pascal Triangle in C

The pascal triangle in c is an array of binomial coefficients in triangular form. Here the nth row contains the binomial coefficient of ncr.In a pascal triangle, every number is...

2 minutes read.

Commenting in C

Commenting in C Commenting in the C language is used to give out the information about the lines of the code which are included. It is one of the things that...

3 minutes read.

Binary Search in C with Best and Worst Time Complexity

What is Binary Search? Binary search is an algorithm that is used to find an element in a sorted array efficiently. It has a time complexity of O(log n). It means...

3 minutes read.

Tower of Hanoi in C

What is Tower of Hanoi? The Tower of Hanoi is a gaming problem that was created in 1883 by a French mathematician named Édouard Lucas. The Tower of Hanoi temple in...

4 minutes read.

Else If Ladder in C

What is Else If Ladder: When there are multiple options and a user has to decide from the options, we use Else If Ladder. Basically, it is an extension of if...

3 minutes read.

Use of Structure in C

We can usually use arrays in C programming to store elements of the same data type. We can use strings to store multiple elements of a character data type. Still,...

4 minutes read.

Structure Pointer in C

Structure Pointer in C In the C programming language, a structure pointer is defined as a pointer that points to the memory block address that stores a structure. Like C standard...

4 minutes read.

Difference between while and do-while loop in C

Introduction Both 'While' and 'Do-While' loops in C mostly have a similar concept, and the code of both loops runs for mainly similar purposes. Both loops process the program/code in the...

3 minutes read.

Heap Sort in C

In this tutorial, we will learn about heap sorting in C language, but before going to Heap sort, we have to know the concept of Complete Binary Tree. What is Complete...

8 minutes read.

Sizeof in C

A pointer in C is defined as a variable that stores the information of the address of another variable. A pointer can be incremented or decremented, which means we can...

4 minutes read.

Nested if-else statement in C

If we use an if-else statement within another if statement in a C program, it is called a nested if-else statement in C. It helps to check the condition inside...

3 minutes read.

gets() function in C

gets() is a pre-define or built-in function present in the stdio.h header file. stdio stands for standard input and output. gets() function is used to read a stream and store...

4 minutes read.

While-Loop in C

Syntax of While Loop The syntax that has been used for the while loop in C programming language is: while (termination condition) {   // the body of the loop  } Working of While-Loop Initially, we...

3 minutes read.

C Program Swap Numbers in cyclic order Using Call by Reference

The three integers that the user entered in cyclical sequence are swapped in this tutorial using call by reference. C Program: #include <stdio.h> void cyclic_Swap ( int *X, int *Y, int *Z ); int...

2 minutes read.

2f in C language

Float data type in c: Double-precision floating-point numbers with up to 17 significant digits are stored in the FLOAT data type. FLOAT is equivalent to C's double data type and IEEE...

3 minutes read.

Conio.h in C

Header files are the source files with the extension of .h. In c language header files are referred to as the helping files and they contain definitions of various functions...

4 minutes read.

Enumeration (Enum) in C

Enumeration (Enum) in C: In the C programming language, the enum is referred to as an enumerated type. Enum is a user defined data type consisting of integer values and...

3 minutes read.