Operators in C

What are Operators in C language?

In the C programming language, operators are symbols that perform operations on variables and values. Understanding the different types of operators present in C is essential to writing effective programs. In this blog post, we’ll cover the different types of operators, their functions, and provide examples of how to use them.

Arithmetic Operators in C

Arithmetic operators are used to perform mathematical calculations.

  1. Addition: +
  2. Subtraction: -
  3. Multiplication: *
  4. Division: /
  5. Modulus: %

Here’s an example of how to use arithmetic operators :

#include <stdio.h>

int main() {
    int a = 10, b = 20;
    printf("a + b = %d\n", a + b);
    printf("a - b = %d\n", a - b);
    printf("a * b = %d\n", a * b);
    printf("a / b = %d\n", a / b);
    printf("a %% b = %d\n", a % b);
    return 0;
}

In this example, we declare two variables, a and b, and then perform various arithmetic operations using the arithmetic operators. The output of the program is as follows:

a + b = 30
a - b = -10
a * b = 200
a / b = 0
a % b = 10

Relational Operators in C

Relational operators are used to compare two values and return a Boolean value (either true or false) depending on the result of the comparison.

  1. Equal to: ==
  2. Not equal to: !=
  3. Greater than: >
  4. Less than: <
  5. Greater than or equal to: >=
  6. Less than or equal to: <=

Here’s an example of how to use relational operators :

#include <stdio.h>

int main() {
    int a = 10, b = 20;
    if (a == b) 
     {
        printf("a is equal to b\n");
     } 
    else if (a != b) 
     {
        printf("a is not equal to b\n");
     }
    if (a > b) 
     {
        printf("a is greater than b\n");
     } 
    else if (a < b) 
     {
        printf("a is less than b\n");
     }
    if (a >= b) 
     {
        printf("a is greater than or equal to b\n");
     } 
    else if (a <= b) 
     {
        printf("a is less than or equal to b\n");
     }
    return 0;
}

In this example, we declare two variables, a and b, and then use relational operators to compare their values. The output of the program is as follows:

a is not equal to b
a is less than b
a is less than or equal to b

Logical Operators

Logical operators are used to combine two or more Boolean expressions and return a Boolean value.

  1. Logical AND: &&
  2. Logical OR: ||
  3. Logical NOT: !

Here’s an example of how to use logical operators :

#include <stdio.h>

int main() {
    int a = 10, b = 20, c = 30;
    if ((a > b) && (b < c)) 
     {
        printf("a is greater than b and b is less than c\n");
     }
    if ((a < b) || (b < c)) 
     {
        printf("either a is less than b or b is less than c\n");
     }
    if (!(a == b)) 
     {
        printf("a is not equal to b\n");
     }
    return 0;
}

In this example, we declare three variables, a, b, and c, and then use logical operators to combine Boolean expressions. The output of the program is as follows:

a is greater than b and b is less than c
either a is less than b or b is less than c
a is not equal to b

Assignment Operators

Assignment operators are used to assign a value to a variable.

  1. Simple assignment: =
  2. Addition assignment: +=
  3. Subtraction assignment: -=
  4. Multiplication assignment: *=
  5. Division assignment: /=
  6. Modulus assignment: %=

Here’s an example of how to use assignment operators :

#include <stdio.h>

int main() {
    int a = 10;
    a += 5;
    printf("a = %d\n", a);
    a -= 3;
    printf("a = %d\n", a);
    a *= 2;
    printf("a = %d\n", a);
    a /= 4;
    printf("a = %d\n", a);
    a %= 3;
    printf("a = %d\n", a);
    return 0;
}

In this example, we declare a variable a and then use various assignment operators to modify its value. The output of the program is as follows:

a = 15
a = 12
a = 24
a = 6
a = 0

Conclusion :

Operators are an essential part of the C programming language. They allow you to perform mathematical calculations, compare values, combine Boolean expressions, and assign values to variables. In this blog post, we’ve covered the different types of operators present in C, their functions, and provided examples of how to use them. By understanding how to use operators effectively, you can write programs that are more efficient, reliable, and easier to read and maintain.

Categories C

Leave a Comment