×

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

Example of Iteration in C

The iterations in the C language are the statements which are executed number of times until a certain condition is reached. These iterations are regarded as “Loops” in C language....

3 minutes read.

C Programming Tutorial

What is C Programming C is most popular and widely used computer programming language. It was developed by Dennis Ritchie in 1972 at the Bell Lab. It is used to develop system application software. There...

8 minutes read.

C Program to Find the Largest Number using Ternary Operator

In this tutorial, we will write some program to determine which of the provided integers is larger using the ternary operator, also known as the conditional operator in C. Ternary Operator: The ternary...

3 minutes read.

Hook() function in C

Use of hook () function in C Introduction:  The hook () is a function used in the C programming language. The basic concept of the hook() function is that it can remove the function...

3 minutes read.

Booleans in C

Introduction to Boolean With all the complexities of programming, it can take time to understand the basics. One concept, in particular, that confuses many beginners is the use of Booleans in...

6 minutes read.

Decimal to Hexadecimal in C

Decimal to Hexadecimal in C Let us first understand the definitions of decimal and hexadecimal. In algebra, a decimal number is defined as a number whose whole number part and a...

3 minutes read.

Union in C

Union is a user-defined data type that is used to hold the different types of elements like structure. In union, all members share the same memory location. Syntax: union [union name] { ...

1 minute read.

Int in C

The keyword int in C programming stands for integer and it is a data type which is used for variable declarations or declaration of functions of different types. Similar to...

4 minutes read.

Find reverse of an array in C using functions

Introduction: Here, we describe how to find the reverse array in C using functions. Suppose you have an array with n elements. I need to display the elements present in...

3 minutes read.

Cbrt() function in C

Introduction: The Cbrt is a function used in C programming language. The cbrt() function is a math function. Using the cbrt function, we can do the cube root of a function. This...

4 minutes read.

Pi() Function in C

Pi: Mathematic constants are not defined in the c or c++ programming languages. To use the Mathematical constants firstly, we have to define the _USE_MATH_DEFINES and then declare the cmath and...

3 minutes read.

Armstrong Number in C

The Armstrong number is defined as the sum of each of its digits to the power of the number base for the each given number with any given number base....

3 minutes read.

Types of Array in C

What is an Array? One value can be stored in a variable at once. How many variables will you need if you have 100 values? Well, the answer isn't 100. It...

14 minutes read.

How to use atoi() function in C

Introduction: The atoi() is a function used in C programming language. This function converts string characters to the integer value. The atoi() function converts the input string to the return type of...

4 minutes read.

How to create a node in C?

A node is a small concept taken from the graph theory, or a node is a fundamental unit of a data structure, like a tree data structure. Every graph contains...

3 minutes read.

Program to Find Mode of an Array in C

Mode is the highest occurring value or number in a given set of data elements. In a group of data values, a value that occurs most frequently is considered to...

3 minutes read.

Character Set in C

Introduction Every language has some basic elements which are used to represent information. The English language includes alphabets which together shape a sentence, after which the sentences are used to shape...

3 minutes read.

Flexible Array Members in a Structure in C

There is flexibility to declare array from c99 generation onwards that declaration of the collections can be made possible without even mentioning its dimension, which means that the displays are...

4 minutes read.

Assert() Function in C

Assert (): In C, the statements are executed with the exit statement. In C language declare, and it tests the condition parameters. If the statement executed will be false, it...

3 minutes read.

Prime Number code in C

Prime Number Programs in C language In this tutorial, we will learn how to check whether the given number is a prime number or not, and how to print all the...

4 minutes read.