1. C Programming for Beginners: Writing Your First Hello World Program in C in 2023 2. Mastering Datatypes in C Programming: A Comprehensive Guide for Beginners in 2023 3. Complete Guide to % Format Specifiers in C for Beginners in 2023 4. A Comprehensive Guide to Operators in C Programming: Learn Everything About Operators in C in 2023 5. Master C Operator Precedence and Associativity in 2023: Definition, Examples, and Code 6. Learn Increment and Decrement Operators in C: A Beginners Guide with Examples - Codzify 7. Mastering Bitwise Operators in C: A Comprehensive Guide with Examples - Codzify 8. Ternary Operators in C Programming: A Beginners Guide (2023) 9. Switch Statements in C 2023: Complete Guide for Beginners 10. Learn Functions in C Programming - A Complete Tutorial for Beginners in 2023 11. Call by Value and Call by Reference in C Programming - A Complete Tutorial for Beginners in 2023 12. Storage Classes in C Programming: A Comprehensive Guide for 2023 13. Arrays in C Programming: A Comprehensive Guide for 2023 14. Basics of 2D Array in C Programming in 2023: Definition, Initialization, and Accessing Elements 15. Beginners Guide to Strings in C Programming with Codes and Examples 16. Strings Functions in C Language - strlen, strcpy,strcmp,strcat functions 17. A Comprehensive Guide to Structures in C Programming in 2023 18. Learn Unions in C Programming with Codes, Examples, and Explanations in 2023 19. Dynamic Memory Allocation in C: Understanding malloc, calloc, realloc, and free Functions 20. Introduction to C Programming 21. Hello world program in C 22. String functions in C - strncat, strcpy, strncpy, strchr, strrchr, strstr functions 23. Program for Fibonacci number in C - Codzify.com 24. What is palindrome number in C? 25. Write a C program to find a factorial of a given number 26. Write a C program to check whether a given number is prime or not 27. Write a C program to check whether a given number is an Armstrong number or not. 28. Write a C Program to transpose a matrix 29. C Programming Tutorial: Understanding Comments and Identifiers with Code Examples 30. Master String Handling Functions in C in 2023 - Codzify Topics

A Comprehensive Guide to Operators in C Programming: Learn Everything About Operators in C in 2023

Article by: Manish Methani

Last Updated: November 7, 2021 at 2:04pm IST
7 min 19 sec

Operators are an essential part of any programming language, and C programming language is no exception. Operators are symbols that represent a specific operation that can be performed on one or more operands. In C, operators are classified into different types based on their functionality. In this tutorial, we will explore everything about operators in the C programming language with examples.

Types of Operators in C

Arithmetic Operators

Arithmetic operators are used for mathematical calculations. The following are the arithmetic operators in C:

  • Addition (+)
  • Subtraction (-)
  • Multiplication (*)
  • Division (/)
  • Modulo (%)

Here is an example of using arithmetic operators in C:

#include <stdio.h>
int main()
{
    int a = 9,b = 4, c;
    
    c = a+b;
    printf("a+b = %d",c);
    c = a-b;
    printf("a-b = %d",c);
    c = a*b;
    printf("a*b = %d",c);
    c = a/b;
    printf("a/b = %d",c);
    c = a%b;
    printf("Remainder when a divided by b = %d",c);
    
    return 0;
}

Output:

a+b = 13
a-b = 5
a*b = 36
a/b = 2
Remainder when a divided by b = 1

Relational Operators

Relational operators are used to comparing two values. The following are the relational operators in C:

  • Greater than (>)
  • Less than (<)
  • Greater than or equal to (>=)
  • Less than or equal to (<=)
  • Equal to (==)
  • Not equal to (!=)

Here is an example of using relational operators in C:

#include <stdio.h>
int main() {
   int a = 10, b = 4;
   printf("a > b is %d", a > b);
   printf("a < b is %d", a < b);
   printf("a >= b is %d", a >= b);
   printf("a <= b is %d", a <= b);
   printf("a == b is %d", a == b);
   printf("a != b is %d", a != b);
   return 0;
}

Output:

a > b is 1
a < b is 0
a >= b is 1
a <= b is 0
a == b is 0
a != b is 1

Logical Operators

Logical operators are used to combining two or more conditions. The following are the logical operators in C:

  • Logical AND (&&)
  • Logical OR (||)
  • Logical NOT (!)

Here is an example of using logical operators in C:

#include <stdio.h>

int main()
{
        int marks = 65;
        if(marks > 75  && marks < 99)
        {
        printf("He is topper");
        }
        else if(marks < 75 && marks >= 65)
        {
          printf("He cracked the exam");
        }
        else 
        {
            printf("He failed");
        }
    return 0;
}

Output:

He cracked the exam

Bitwise Operators

Bitwise operators are used to perform bitwise operations on two integer operands. The following are the bitwise operators in C:

  • Bitwise AND (&)
  • Bitwise OR (|)
  • Bitwise XOR (^)
  • Bitwise NOT (~)
  • Left shift (<<)
  • Right shift (>>)

There are many Bitwise operators in C as defined below:

& (bitwise AND):- Performs the AND Operation bit by bit. The result of AND is 1 only if both bits are 1.

| (bitwise OR):- Performs the OR Operation bit by bit. The result of OR is 1 any of the two bits is 1.

^ (bitwise XOR):- Performs the XOR Operation bit by bit. The result of XOR is 1 if the two bits are different.

<< (left shift):- Takes two numbers, left shifts the bits of the first operand, the second operand decides the number of places to shift.

>> (right shift):- Takes two numbers, right shifts the bits of the first operand, the second operand decides the number of places to shift.

~ (bitwise NOT):- Takes one number and inverts all bits of it.

Here is an example of using bitwise operators in C:

#include <stdio.h>
int main() {
   int a = 5, b = 9, result;
   result = a & b;
   printf("Bitwise AND: %d
", result);
   result = a | b;
}

Output:

Bitwise AND: 1

 

Assignment Operators in C

An assignment operator is used for assigning a value to a variable.

Operator Example Same as
= a = b a = b
+= a += b a = a+b
-= a -= b a = a-b
*= a *= b a = a*b
/= a /= b a = a/b
%= a %= b a = a%b

C program to illustrate the concept of Assignment Operators

#include <stdio.h>
int main()
{
        int a;
        a+= 10;
        printf("a = %d",a);
        
        a -= 2;
        printf("a = %d",a);
        
        a *= 2;
        printf("a = %d",a);

        a /= 2;
        printf("a = %d",a);
        
        a %=2; 
        printf("a = %d",a);
    return 0;
}

Output

a = 10
a = 8
a = 16
a = 8
a = 0

Hierarchy of Operations

There are various scenarios where the compiler has to know which operation should perform first. What do you think this expression will be evaluated according to the compiler?

2 * x - 3 * y 

Now,

There are two ways to solve this equation

1)  (2x)-(3y)
2)  2(x-3y)

We engineers know how to solve these problems because we know multiplication is solved first then minus operation should be applied. But how does the compiler know how to solve this? So Hierarchy of Operations helps the compiler to understand this kind of equations.

The following table list the operators according to priorities:-

Priority Operators Description
1st */% multiplication, division, modular division
2nd +- addition, subtraction
3rd = assignment

 

What this table signifies?

* will be always solved first then the division (/) operator. + will be solved before the - operator but * will be solved first then the + operator.

Example

a = 2*3/4+4/4

As * has max priority than "/" So it will be evaluated first. So,

6/4+4/4

Now "/" has max priority than "+" so

1 + 4/4

Now "/" has max priority than "+" so

1 + 1

Result is 2

Explore Tech Guide:

Codzify Logo

Terms and Conditions    Cookie Policy   Refund Policy   Adsense Disclaimer

Contact: teamcodzify@gmail.com