Operators are special symbols that perform operations on variables and values. In C, operators are categorized into several types based on the operations they perform. Understanding these operators is crucial for performing arithmetic calculations, making decisions, and manipulating data.

Types of Operators

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

  1. Arithmetic Operators

Arithmetic operators are used to perform basic mathematical operations.

Operator Description Example
+ Addition a + b
- Subtraction a - b
* Multiplication a * b
/ Division a / b
% Modulus (remainder) a % b

Example:

#include <stdio.h>

int main() {
    int a = 10, b = 3;
    printf("Addition: %d\n", a + b);        // Output: 13
    printf("Subtraction: %d\n", a - b);     // Output: 7
    printf("Multiplication: %d\n", a * b);  // Output: 30
    printf("Division: %d\n", a / b);        // Output: 3
    printf("Modulus: %d\n", a % b);         // Output: 1
    return 0;
}

  1. Relational Operators

Relational operators are used to compare two values. They return either true (1) or false (0).

Operator Description Example
== Equal to a == b
!= Not equal to a != b
> Greater than a > b
< Less than a < b
>= Greater than or equal a >= b
<= Less than or equal a <= b

Example:

#include <stdio.h>

int main() {
    int a = 10, b = 20;
    printf("a == b: %d\n", a == b);  // Output: 0 (false)
    printf("a != b: %d\n", a != b);  // Output: 1 (true)
    printf("a > b: %d\n", a > b);    // Output: 0 (false)
    printf("a < b: %d\n", a < b);    // Output: 1 (true)
    printf("a >= b: %d\n", a >= b);  // Output: 0 (false)
    printf("a <= b: %d\n", a <= b);  // Output: 1 (true)
    return 0;
}

  1. Logical Operators

Logical operators are used to combine multiple conditions.

Operator Description Example
&& Logical AND a && b
` `
! Logical NOT !a

Example:

#include <stdio.h>

int main() {
    int a = 1, b = 0;
    printf("a && b: %d\n", a && b);  // Output: 0 (false)
    printf("a || b: %d\n", a || b);  // Output: 1 (true)
    printf("!a: %d\n", !a);          // Output: 0 (false)
    printf("!b: %d\n", !b);          // Output: 1 (true)
    return 0;
}

  1. Bitwise Operators

Bitwise operators perform operations on the binary representations of integers.

Operator Description Example
& Bitwise AND a & b
` ` Bitwise OR
^ Bitwise XOR a ^ b
~ Bitwise NOT ~a
<< Left shift a << 1
>> Right shift a >> 1

Example:

#include <stdio.h>

int main() {
    int a = 5, b = 3;  // Binary: a = 0101, b = 0011
    printf("a & b: %d\n", a & b);  // Output: 1 (0001)
    printf("a | b: %d\n", a | b);  // Output: 7 (0111)
    printf("a ^ b: %d\n", a ^ b);  // Output: 6 (0110)
    printf("~a: %d\n", ~a);        // Output: -6 (in 2's complement)
    printf("a << 1: %d\n", a << 1); // Output: 10 (1010)
    printf("a >> 1: %d\n", a >> 1); // Output: 2 (0010)
    return 0;
}

  1. Assignment Operators

Assignment operators are used to assign values to variables.

Operator Description Example
= Assign a = b
+= Add and assign a += b
-= Subtract and assign a -= b
*= Multiply and assign a *= b
/= Divide and assign a /= b
%= Modulus and assign a %= b

Example:

#include <stdio.h>

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

  1. Miscellaneous Operators

Operator Description Example
sizeof Size of a data type sizeof(a)
& Address of variable &a
* Pointer to a variable *ptr
? : Ternary operator a ? b : c

Example:

#include <stdio.h>

int main() {
    int a = 10;
    int *ptr = &a;
    printf("Size of a: %lu\n", sizeof(a));  // Output: 4 (on most systems)
    printf("Address of a: %p\n", (void*)&a); // Output: Address of a
    printf("Value at ptr: %d\n", *ptr);    // Output: 10
    int b = (a > 5) ? 100 : 200;
    printf("Ternary operator result: %d\n", b); // Output: 100
    return 0;
}

Practical Exercises

Exercise 1: Arithmetic Operations

Write a program that takes two integers as input and performs all arithmetic operations on them.

Solution:

#include <stdio.h>

int main() {
    int a, b;
    printf("Enter two integers: ");
    scanf("%d %d", &a, &b);
    printf("Addition: %d\n", a + b);
    printf("Subtraction: %d\n", a - b);
    printf("Multiplication: %d\n", a * b);
    printf("Division: %d\n", a / b);
    printf("Modulus: %d\n", a % b);
    return 0;
}

Exercise 2: Relational and Logical Operations

Write a program that takes two integers as input and prints the result of relational and logical operations.

Solution:

#include <stdio.h>

int main() {
    int a, b;
    printf("Enter two integers: ");
    scanf("%d %d", &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);
    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);
    return 0;
}

Exercise 3: Bitwise Operations

Write a program that takes two integers as input and performs bitwise operations on them.

Solution:

#include <stdio.h>

int main() {
    int a, b;
    printf("Enter two integers: ");
    scanf("%d %d", &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 << 1: %d\n", a << 1);
    printf("a >> 1: %d\n", a >> 1);
    return 0;
}

Summary

In this section, we covered the various types of operators in C, including arithmetic, relational, logical, bitwise, assignment, and miscellaneous operators. We also provided practical examples and exercises to help reinforce the concepts. Understanding these operators is fundamental to writing effective and efficient C programs. In the next module, we will delve into control flow statements, which will allow us to make decisions and repeat actions in our programs.

© Copyright 2024. All rights reserved