Operators in C: Types of Operators

Operators in C: Types of Operators

25 Oct 2024
Beginner
26.5K Views
22 min read
Learn with an interactive course and practical hands-on labs

Free C Foundation Course: Learn C In 21 Days

Operators in C: An Overview

Operators in C constitute the building block of the C programming language. They act as potent instruments for data manipulation and control flow. For any C programmer, understanding how these operators function is essential. We'll explain the main operator classifications and their importance in this C Tutorial. To test your theoretical concepts, consider our C Programming Course.

What are Operators in C language?

Operators are special symbols used to perform various mathematical and logical operations on variables and symbols known as operands.

The application of operators varies depending on the context. So, it is important to understand how each type works to use them effectively when programming in C language.

Example

        #include <stdio.h>

int a=10;
int b=40;
int c= a+b;11:16

Here, “+” is an operator that performs the addition of two variables of data type int. The variables a and b are known as the operands.

Types of Operators in C

C has a wide range of built-in operators that can be classified into various types according to their functionality.

  1. Arithmetic Operators
  2. Relational Operators
  3. Logical Operators
  4. Bitwise Operators
  5. Assignment Operators
  6. Conditional Operator
  7. Miscellaneous Operator

types of operators in c programming language

We can further classify these operators based on the number of operands on which they operate. Now, let us go through all these operators in detail.

Read More - Top 50 Mostly Asked C Interview Questions and Answers

1. Arithmetic Operators

These operators perform basic mathematical operations like addition, subtraction, multiplication, and division. We will categorize the arithmetic operators in C into two categories based on the number of operands and then look at their functionality.

TypeOperatorName of OperatorFunctionality
Unary++IncrementIncreases the value by 1
- -DecrementDecreases the value by 1
+Unary plusNo change in the operand value
-Unary minuschanges the negative number to the positive and vice-versa
Binary+AdditionAdd two values
-SubtractionSubtracts one value from the other
*MultiplicationMultiplies two values
/DivisionDivides one by the other value
%ModulusFinds the remainder after division

Example of Arithmetic Operators in C


#include <stdio.h>
int main()
{
 //unary operators
 int x = 5, y=6, z=7, w=8;
 printf("%d\n", ++x); //increments the value of x
 printf("%d\n", --y); //decrements the value of x
 printf("%d\n", +z); // unary +
 printf("%d\n", -w); // unary - on a positive number
 printf("%d\n",-(-w)); // unary - on a negative number
 // binary operators
 int a = 10, b = 3;
 int sum = a + b;
 int difference = a - b;
 int product = a * b;
 int quotient = a / b;
 int remainder = a % b;
 printf("Sum: %d\n", sum);
 printf("Difference: %d\n", difference);
 printf("Product: %d\n", product);
 printf("Quotient: %d\n", quotient);
 printf("Remainder: %d\n", remainder);
 return 0;
}

In the above code in the C Compiler, we have performed all the arithmetic operations on unary as well as binary operands.

Output

6
5
7
-8
8
Sum: 13
Difference: 7
Product: 30
Quotient: 3
Remainder: 1

2. Relational Operators

Relational operators in C are also known as Comparison Operators. They compare the values of the two operands. The result of the comparison is either true or false. If the comparison is true, it returns 1; If the comparison results in false, it returns 0. These are known as boolean values.

OperatorName
==Equal to
>Greater than
<Less than
>=Greater than or equal to
<=Less than or equal to
!=Not equal to

Example of Relational Operators in C


#include <stdio.h>
int main()
{
 int x = 5;
 int y = 3;
 printf("%d\n", x == y); // returns 0 (false) because 5 is not equal to 3
 printf("%d\n", x != y); // returns 1 (true) because 5 is not equal to 3
 printf("%d\n", x > y); // returns 1 (true) because 5 is greater than 3
 printf("%d\n", x < y); // returns 0 (false) because 5 is greater than 3
 printf("%d\n", x >= y); // returns 1 (true) because five is greater than, or equal, to 3
 printf("%d\n", x <= y); // returns 0 (false) because 5 is neither less than or equal to 3
 return 0;
}

The above code performs all the comparison operations and returns the result in boolean values.

Output

0
1
1
0
1
0

3. Logical Operators

They are used to combine two or more conditions/constraints. It returns either 0 or 1 depending upon whether the expression results in true or false. If the result is true, it returns 1 else returns 0.

The logical operators in C are used in decision-making and looping statements.

We will categorize the logical operators into two categories based on the number of operands and then look at their functionality.

TypeOperatorNameFunctionality
Binary&&Logical ANDreturns 1(true) if both the expressions/values are true.
||Logical ORreturns 1(true) if one of the expressions/values evaluates to true.
Unary!=Logical NOTNegates the expression and returns 1 or 0.

Example of Logical Operators in C Online Compiler


#include <stdio.h>
int main()
{
 int a = 1, b = 0;
if (a && b)
 {
 printf("Both a and b are true (non-zero)\n");
 }
 else
 {
 printf("At least one of a or b is false (zero)\n");
 }
 if (a || b)
 {
 printf("At least one of a or b is true (non-zero)\n");
 }
 else
 {
 printf("Both a and b are false (zero)\n");
 }
 if (!b) //value of b becomes 1
 {
 printf("b is false (zero)\n");
 }
 else
 {
 printf("b is true (non-zero)\n");
 }
 return 0;
}

The above code performs all three logical operations on a and b i.e. 1 and 0 respectively.

Output

At least one of a or b is false (zero)
At least one of a or b is true (non-zero)
b is false (zero)

4. Bitwise Operators

These operators work on individual bits. The operands are first converted into bits i.e. 0 or 1, and then the calculation is performed on them. We will categorize the bitwise operators into two categories based on the number of operands and then look at their functionality.

TypeOperatorName
Unary~One's complement or Bitwise Complement
<<Left Shift
>>Right Shift
Binary&Bitwise AND
|Bitwise OR
^Bitwise Exclusive OR or XOR

We will look at Bitwise Operators in detail in the section Bitwise Operators in C

5. Assignment Operators

These are used to assign values to the variables. The most fundamental assignment operator in C is “=”.

Example of Assignment Operators in C

#include <stdio.h>int main() {
 int x;
 x = 10; // Assigning the value 10 to x
 printf("The value of x is: %d\n", x);
 return 0;}

In this example, we have assigned the value 10 to the variable x using the assignment operator (=).

Output

The value of x is: 10

The following table shows some variants of the assignment operator, “=”.

OperatorExampleSame as
=x=yx=y
+=x+=yx=x+y
-=x-=yx=x-y
*=x*=yx=x*y
/=x/=yx=x/y
%=x%=yx=x%y
&=x&=yx=x&y
|=x|=yx=x|y
^=x^=yx=x^y
>>=x>>=yx=x>>y
<<=x<<=yx=x<<y

6. Conditional Operator

This is also called a ternary operator. It works on three operands. The ternary operator is used to execute a set of statements when the test expression is true and another set of statements when the test expression evaluates to false.

Syntax


testexpression? expression1 : expression 2;

Here, the testexpression results in a boolean value i.e. 0(true) or 1(false). If the testexpression evaluates to:

  • True: expression1 before the colon(:) executes
  • False: expression2 after the colon(:) executes

Example of Conditional Operator in C


#include <stdio.h>
 int main(){
 int a = 5;
 int b = 10;
 int max = (a > b) ? a : b; 
 printf("The maximum value is: %d\n", max);
 return 0;
}

Output

The maximum value is: 10

We will look at ternary operators in detail in the section Ternary Operator in C.

7. Miscellaneous Operator

Commas are used to divide expressions in a statement and are a part of C's miscellaneous operator (,). The value of the rightmost expression is returned after evaluating each expression in order from left to right.

Example


#include <stdio.h>int main() {
 int a = 5, b = 10, c;
 c = (a++, b++, a + b);
 printf("c = %d\n", c);
 return 0;}

In this example in the C Editor, we have shown how to combine several operations into a single statement using the comma operator. Then, it calculates the sum of a and b and assigns it to c after increasing a and b with the notations a++ and b++, respectively.

Output

c = 15

Precedence of Operators in C

Operator precedence and associativity help us to determine which operators will be given priority when there are multiple operators in the expression. It is the grouping of terms in an expression and decides how an expression should be evaluated.

Category Operator Associativity
Postfix() [] ->. ++ - -Left to right
Unary+ - ! ~ ++ - - (type)* & sizeofRight 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
Summary

C language's fundamental building block is its operators, which enable a wide range of operations. The basic kinds of operators in C, including arithmetic, relational, logical, bitwise, and assignment operators, have been outlined in this article along with examples from real-world applications for each. For easy reference, we've included a reference table to help you quickly understand operator precedence, which is essential for writing effective and error-free code. For more information, please consider our C Certification Course.

FAQs

The four categories of operators in C are the arithmetic, relational, logical, and assignment operators.

In C, the "&=" operator is a compound assignment operator used to conduct a bitwise AND operation between two values and assign the result to the left operand.

In C, operators are used to manipulate and control data by performing various operations on it, such as calculations, comparisons, logical operations, and assignments.

The order in which operators are evaluated in expressions in C is determined by their operator precedence. It helps prevent confusion and make sure that expressions are accurately evaluated while adhering to mathematical norms.

With arithmetic operations adhering to conventional mathematical norms and logical and relational operations producing true (1) or false (0) outputs, operations in C are carried out based on operator precedence and associativity.
Share Article
About Author
Sakshi Dhameja (Author and Mentor)

She is passionate about different technologies like JavaScript, React, HTML, CSS, Node.js etc. and likes to share knowledge with the developer community. She holds strong learning skills in keeping herself updated with the changing technologies in her area as well as other technologies like Core Java, Python and Cloud.

Accept cookies & close this