Operators in C Programming: Understanding the Different Types of Operators Used in C

Operators

Table of Contents

What is an operator and its types?

In C programming, operators are symbols or words that we use to do different things with data values. They let us work with data and do calculations with variables. There are different types of operators that are grouped based on what they do. For example, we have operators for doing math (arithmetic operators), comparing values (relational operators), checking conditions (logical operators), and changing bit patterns (bitwise operators).

Let’s say you have two variables in C programming, “a” and “b”, and you want to add them together and store the result in a third variable “c”. In this case, you would use the “+” operator, which is an arithmetic operator, to perform the addition operation.

The syntax would look something like this:

int a = 5;
int b = 7;
int c = a + b;

In this example, the value of “c” would be 12, since 5 + 7 = 12. The “+” operator was used to perform the addition operation on the variables “a” and “b”.

What are the different types of operators in C?

Types of Operator in C programming

  • Arithmetic Operators
  • Relational Operators
  • Logical Operators
  • Bitwise Operators
  • Assignment Operators
  • Increment and Decrement Operators

Arithmetic Operators

Arithmetic Operators: Arithmetic operators are mathematical symbols used to execute basic mathematical operations such as addition, subtraction, multiplication, division, and modulus. These operators are an essential component of any programming language, including C programming.

The following table demonstrates the various arithmetic operators utilized in C programming:

OperatorDescription
+Addition
Subtraction
*Multiplication
/Division
%Modulus (remainder)

Example Of Arithmetic Operators

#include <stdio.h>

int main() {
int a = 5, b = 2, result;

result = a + b;
printf(“Addition: %d\n”, result);

result = a – b;
printf(“Subtraction: %d\n”, result);

result = a * b;
printf(“Multiplication: %d\n”, result);

result = a / b;
printf(“Division: %d\n”, result);

result = a % b;
printf(“Modulus: %d\n”, result);

return 0;
}

Output

Addition: 7
Subtraction: 3
Multiplication: 10
Division: 2
Modulus: 1

Logical Operators

Logical Operators: In C programming, logical operators are utilized to assess a condition and produce a value of true or false. These operators are fundamental in constructing decision-making structures within a program.

The following table displays the unique logical operators utilized in C programming:

OperatorDescription
&&Logical AND (both conditions are true)
||Logical OR (at least one condition is true)
!Logical NOT (inverts the condition)

Example Of Logical Operators

#include <stdio.h>
#include <stdbool.h>

int main() {
bool a = true, b = false;

if (a && b) {
printf(“Logical AND: true\n”);
} else {
printf(“Logical AND: false\n”);
}

if (a || b) {
printf(“Logical OR: true\n”);
} else {
printf(“Logical OR: false\n”);
}

if (!a) {
printf(“Logical NOT: true\n”);
} else {
printf(“Logical NOT: false\n”);
}

return 0;
}

Output

Logical AND: false
Logical OR: true
Logical NOT: false

Relational operators

 

Relational operators: Within the realm of C programming, relational operators are invaluable for comparing two distinct values, with the resultant outcome being a Boolean value. Essentially, the comparison yields either a true or false outcome, which is dependent upon the comparison itself. These operators are crucial when constructing decision-based structures within a program, allowing developers to create a variety of powerful and dynamic software applications.

The following is the syntax for the six relational operators used in C programming:

OperatorDescription
==Checks if two values are equal
!=Checks if two values are not equal
>Checks if the first value is greater than second
<Checks if the first value is less than second
>=Checks if the first value is greater than or equal to the second
<=Checks if the first value is less than or equal to the second

Example Of Relational operators

#include <stdio.h>

int main() {
    int a = 10;
    int b = 5;

    if (a == b) {
        printf("a is equal to b");
    }

    if (a != b) {
        printf("a is not equal to b");
    }

    if (a > b) {
        printf("a is greater than b");
    }

    if (a < b) {
        printf("a is less than b");
    }

    if (a >= b) {
        printf("a is greater than or equal to b");
    }

    if (a <= b) {
        printf("a is less than or equal to b");
    }

    return 0;
}

Bitwise operators:

Bitise operators In the world of C programming, bitwise operators are often utilized to perform a variety of bit-level operations on a given variable. These operators function by manipulating the bits contained within a variable to produce the desired result. As a result, these operators are often employed in situations where fine-tuned control over the individual bits within a variable is necessary. With their ability to handle complex data manipulation tasks, bitwise operators remain a critical tool for developers seeking to maximize the performance of their C-based software applications.

OperatorDescription
&Bitwise AND
|Bitwise OR
^Bitwise XOR
~Bitwise NOT
<<Left shift
>>Right shift

Example Of Bitwise operators:

#include <stdio.h>

int main() {
int a = 5, b = 3, result;

result = a & b;
printf(“Bitwise AND: %d\n”, result);

result = a \| b;
printf(“Bitwise OR: %d\n”, result);

result = a ^ b;
printf(“Bitwise XOR: %d\n”, result);

result = ~a;
printf(“Bitwise NOT: %d\n”, result);

result = a << 1;
printf(“Left shift: %d\n”, result);

result = a >> 1;
printf(“Right shift: %d\n”, result);

return 0;
}

TRENDIG TALKER .COM

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *