C Tutorial



OPERATORS IN C


Operators in C

In C programming, operators are special symbols that perform operations on variables and values. Operators allow us to manipulate data and perform calculations.

🖱️ Types of Operators in C

📘 Key Operator Types:

  • Arithmetic Operators
  • Relational Operators
  • Logical Operators
  • Bitwise Operators
  • Assignment Operators
  • Increment/Decrement Operators
  • Conditional (Ternary) Operator
  • Sizeof Operator

➗ Arithmetic Operators

These operators perform basic mathematical operations on numeric values.

int a = 10, b = 5;
int sum = a + b;    // Addition
int diff = a - b;   // Subtraction
int prod = a * b;   // Multiplication
int div = a / b;    // Division
int mod = a % b;    // Modulo (Remainder)
  

🔍 Relational Operators

Relational operators are used to compare two values and return a boolean result (True or False).

int a = 10, b = 5;
a == b    // Equal to
a != b    // Not equal to
a > b    // Greater than
a < b    // Less than
a >= b   // Greater than or equal to
a <= b   // Less than or equal to
  

🔐 Logical Operators

Logical operators are used to perform logical operations on boolean values.

int a = 10, b = 5;
(a > b) && (b > 0)    // AND
(a > b) || (b > 0)    // OR
!(a > b)                // NOT
  

🧮 Assignment Operators

Assignment operators are used to assign values to variables.

int a = 10;
a += 5    // a = a + 5
a -= 5    // a = a - 5
a *= 5    // a = a * 5
a /= 5    // a = a / 5
a %= 5    // a = a % 5
  

🔄 Increment/Decrement Operators

These operators are used to increase or decrease the value of a variable by 1.

int a = 10;
a++;     // Increment (a = a + 1)
a--;     // Decrement (a = a - 1)
  

🔢 Bitwise Operators

Bitwise operators perform operations on individual bits of integer values.

int a = 5, b = 3;
a && b    // AND
a | b         // OR
a ^ b         // XOR
~a            // NOT
a << 1     // Left Shift
a >> 1     // Right Shift
  

📊 Conditional (Ternary) Operator

The ternary operator is a shorthand way to express an if-else statement.

int a = 10, b = 5;
int result = (a > b) ? a : b;  // If a > b, result = a; else result = b;
  

📐 Sizeof Operator

The sizeof operator is used to determine the size of a data type or variable in bytes.

int a = 10;
printf("Size of int: %lu bytes", sizeof(a));  // Output the size of a
  
💡 Note: Operators are crucial for performing calculations, comparisons, and logical evaluations in C.

📝 Try It Yourself

  1. Write a program using arithmetic operators to find the area of a rectangle.
  2. Use the conditional operator to check if a number is even or odd.

❌ Common Mistakes

  • 🚫 Misusing the assignment operator (=) instead of the equality operator (==).
  • 🚫 Forgetting to use parentheses in complex expressions, leading to unexpected results.

🌟 Enjoyed Learning with Us?

Help others discover Technorank Learning by sharing your honest experience.
Your support inspires us to keep building!

Leave a Google Review