Operators

 Operators are fundamental to any programming language, and C is no exception. They enable various operations on operands, and in C, operators are categorized by their functionality and the number of operands they require.

1. Arithmetic Operators

   - Addition (`+`): Adds two operands. Example: `x + y`.

   - Subtraction (`-`): Subtracts the second operand from the first. Example: `x - y`.

   - Multiplication (`*`): Multiplies two operands. Example: `x * y`.

   - Division (`/`): Divides the first operand by the second. Example: `x / y`.

   - Modulus (`%`): Returns the remainder of division. Example: `x % y`.

   Example Code:

   using System;

   namespace Arithmetic

   {

       class Program

       {

           static void Main(string[] args)

           {

               int result;

               int x = 10, y = 5;

               result = x + y;

               Console.WriteLine("Addition: " + result);

               result = x - y;

               Console.WriteLine("Subtraction: " + result);

               result = x * y;

               Console.WriteLine("Multiplication: " + result);

               result = x / y;

               Console.WriteLine("Division: " + result);

               result = x % y;

               Console.WriteLine("Modulo: " + result);

           }

       }

   }

2. Unary Operators

   - Increment (`++`): Increases an integer value by one. Prefix (`++x`) increments before the value is used, and postfix (`x++`) increments after the value is used.

   - Decrement (`--`): Decreases an integer value by one. Prefix (`--x`) decrements before the value is used, and postfix (`x--`) decrements after the value is used.

   Example Code:

   using System;

   namespace UnaryOperators

   {

       class Program

       {

           static void Main(string[] args)

           {

               int a = 10, res;

              res = a++;

               Console.WriteLine($"a is {a} and res is {res}");

               res = a--;

               Console.WriteLine($"a is {a} and res is {res}");

                res = ++a;

               Console.WriteLine($"a is {a} and res is {res}");

               res = --a;

               Console.WriteLine($"a is {a} and res is {res}");

           }

       }

   }

 3. Relational Operators

   - Equal to (`==`): Checks if two operands are equal. Example: `x == y`.

   - Not equal to (`!=`): Checks if two operands are not equal. Example: `x != y`.

   - Greater than (`>`): Checks if the first operand is greater than the second. Example: `x > y`.

   - Less than (`<`): Checks if the first operand is less than the second. Example: `x < y`.

   - Greater than or equal to (`>=`): Checks if the first operand is greater than or equal to the second. Example: `x >= y`.

   - Less than or equal to (`<=`): Checks if the first operand is less than or equal to the second. Example: `x <= y`.

   Example Code:

    using System;

   namespace RelationalOperators

   {

       class Program

       {

           static void Main(string[] args)

           {

               bool result;

               int x = 5, y = 10;

              

               result = x == y;

               Console.WriteLine("Equal to: " + result);

              

               result = x > y;

               Console.WriteLine("Greater than: " + result);

              

               result = x < y;

               Console.WriteLine("Less than: " + result);

              

               result = x >= y;

               Console.WriteLine("Greater than or Equal to: " + result);

               result = x <= y;

               Console.WriteLine("Less than or Equal to: " + result);

              result = x != y;

               Console.WriteLine("Not Equal to: " + result);

           }

       }

   }

 

4. Logical Operators

   - Logical AND (`&&`): Returns true if both conditions are true. Example: `a && b`.

   - Logical OR (`||`): Returns true if at least one condition is true. Example: `a || b`.

   - Logical NOT (`!`): Returns true if the condition is false. Example: `!a`.

   Example Code:

   using System;

   namespace LogicalOperators

   {

       class Program

       {

           static void Main(string[] args)

           {

               bool a = true, b = false;

               bool result = a && b;

               Console.WriteLine("Logical AND: " + result);

               result = a || b;

               Console.WriteLine("Logical OR: " + result);

               result = !a;

               Console.WriteLine("Logical NOT: " + result);

           }

       }

   }

 

5. Bitwise Operators

   - Bitwise AND (`&`): Performs AND operation on each bit. Example: `x & y`.

   - Bitwise OR (`|`): Performs OR operation on each bit. Example: `x | y`.

   - Bitwise XOR (`^`): Performs XOR operation on each bit. Example: `x ^ y`.

   - Bitwise Complement (`~`): Inverts all bits. Example: `~x`.

   - Left Shift (`<<`): Shifts bits to the left. Example: `x << 2`.

   - Right Shift (`>>`): Shifts bits to the right. Example: `x >> 2`.

   Example Code:

   using System;

   namespace BitwiseOperators

   {

       class Program

       {

           static void Main(string[] args)

           {

               int x = 5, y = 10, result;

               result = x & y;

               Console.WriteLine("Bitwise AND: " + result);

               result = x | y;

               Console.WriteLine("Bitwise OR: " + result);

               result = x ^ y;

               Console.WriteLine("Bitwise XOR: " + result);

               result = ~x;

               Console.WriteLine("Bitwise Complement: " + result);

               result = x << 2;

               Console.WriteLine("Left Shift: " + result);

               result = x >> 2;

               Console.WriteLine("Right Shift: " + result);

           }

       }

   }

 

6. Assignment Operators

   - Simple Assignment (`=`): Assigns a value to a variable. Example: `x = 10`.

   - Add Assignment (`+=`): Adds and assigns. Example: `x += 5` (equivalent to `x = x + 5`).

   - Subtract Assignment (`-=`): Subtracts and assigns. Example: `x -= 5` (equivalent to `x = x - 5`).

   - Multiply Assignment (`*=`): Multiplies and assigns. Example: `x *= 5` (equivalent to `x = x * 5`).

   - Divide Assignment (`/=`): Divides and assigns. Example: `x /= 5` (equivalent to `x = x / 5`).

   - Modulus Assignment (`%=`):

Computes modulus and assigns. Example: `x %= 5` (equivalent to `x = x % 5`).

   - Left Shift Assignment (`<<=`): Left shifts and assigns. Example: `x <<= 2` (equivalent to `x = x << 2`).

   - Right Shift Assignment (`>>=`): Right shifts and assigns. Example: `x >>= 2` (equivalent to `x = x >> 2`).

   - Bitwise AND Assignment (`&=`): Bitwise AND and assigns. Example: `x &= 2` (equivalent to `x = x & 2`).

   - Bitwise XOR Assignment (`^=`): Bitwise XOR and assigns. Example: `x ^= 2` (equivalent to `x = x ^ 2`).

   - Bitwise OR Assignment (`|=`): Bitwise OR and assigns. Example: `x |= 2` (equivalent to `x = x | 2`).

   Example Code:

   using System;

   namespace AssignmentOperators

   {

       class Program

       {

           static void Main(string[] args)

           {

               int x = 15;

               x += 10;

               Console.WriteLine("Add Assignment: " + x);

               x = 20;

               x -= 5;

               Console.WriteLine("Subtract Assignment: " + x);

               x = 15;

               x *= 5;

               Console.WriteLine("Multiply Assignment: " + x);

               x = 25;

               x /= 5;

               Console.WriteLine("Division Assignment: " + x);

               x = 25;

               x %= 5;

    }

}

No comments:

Post a Comment