Operators in C

Operators in C

Operators in C are special symbols that perform operations on variables and values. In the C programming language, operators in C are used to manipulate data, make decisions, and control program flow. Without operators in C, it would be impossible to perform calculations or implement logic in programs.

Every C program uses operators in C to evaluate expressions, compare values, and execute conditions. Understanding operators in C is essential for writing efficient and meaningful code.

Types of Operators in C

Operators in C are classified into different categories based on their functionality. Some commonly used operators in C include:

  • Arithmetic operators
  • Relational operators
  • Logical operators in C
  • Bitwise operator in C
  • Assignment operator in C

Each type of operators in C serves a specific purpose.

Arithmetic operators in C

The arithmetic operators are used to perform arithmetic/mathematical operations on operands. There are 9 arithmetic operators in C language:

#include <stdio.h>

int main() {

    int a = 25, b = 5;

    // using operators and printing results
    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);
    printf("+a = %d\n", +a);
    printf("-a = %d\n", -a);
    printf("a++ = %d\n", a++);
    printf("a-- = %d\n", a--);

    return 0;
}

OUTPUT:

a + b = 30 a – b = 20 a * b = 125 a / b = 5 a % b = 0 +a = 25 -a = -25 a++ = 25 a– = 26

Relational operators in C

The relational in C are used for the comparison of the two operands. All these operators are binary operators that return true or false values as the result of comparison.

#include <stdio.h>

int main() {
    int a = 25, b = 5;

    // using operators and printing results
    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);
    printf("a != b : %d\n", a != b);

    return 0;
}

OUTPUT:

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

Logical Operators in C

Logical operators in C are used to combine multiple conditions and return either true or false. These operators in C are commonly used in decision-making statements like if, while, and for.

Common logical operators in C:

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

Example:

#include <stdio.h>
int main() {
    int a = 10, b = 5;

    if (a > 5 && b < 10) {
        printf("Condition is true");
    }

    return 0;
}

OUTPUT:

Condition is true

Bitwise operators in C

The Bitwise operators are used to perform bit-level operations on the operands. The operators are first converted to bit-level and then the calculation is performed on the operands.

Common bitwise operator in C includes:

  • & (AND)
  • | (OR)
  • ^ (XOR)
  • << (Left Shift)
  • >> (Right Shift)

Bitwise operator in C improves performance when dealing with binary data.

#include <stdio.h>

int main() {
    int a = 25, b = 5;

    // using operators and printing results
    printf("a & b: %d\n", a & b);
    printf("a | b: %d\n", a | b);
    printf("a ^ b: %d\n", a ^ b);
    printf("~a: %d\n", ~a);
    printf("a >> b: %d\n", a >> b);
    printf("a << b: %d\n", a << b);

    return 0;
}

OUTPUT:

a & b: 1 a | b: 29 a ^ b: 28 ~a: -26 a >> b: 0 a << b: 800

Assignment operators in C

An assignment operator in C is used to assign a value to a variable. It stores the result of an expression on the right side into the variable on the left side. Assignment operators are commonly used in almost every C program for initializing and updating variables.

Basic Assignment Operator (=)

The basic assignment operator = assigns a value to a variable.

int a;
a = 10;

OUTPUT:

Here, the value 10 is assigned to the variable a.

Compound Assignment Operators

C also provides compound assignment operators that combine an arithmetic operation with assignment. These operators make the code shorter and more readable.

Operator Description Example
+= Add and assign a += 5 (a = a + 5)
-= Subtract and assign a -= 3 (a = a – 3)
*= Multiply and assign a *= 2 (a = a * 2)
/=

Leave a Comment

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