Chapter 4: Operators and Expressions

Introduction to Operators

Operators are special symbols in C that perform operations on variables and values. They are the foundation of expressions and help in performing various computations.

Types of Operators

C has several types of operators, including:

  • Arithmetic Operators
  • Relational Operators
  • Logical Operators
  • Bitwise Operators
  • Assignment Operators
  • Miscellaneous Operators

Arithmetic Operators

Arithmetic operators are used to perform mathematical operations.

Basic Arithmetic Operators

  • Addition (+): Adds two operands.int sum = 5 + 3; // sum = 8
  • Subtraction (-): Subtracts the second operand from the first.int difference = 5 - 3; // difference = 2
  • Multiplication (*): Multiplies two operands.int product = 5 * 3; // product = 15
  • Division (/): Divides the first operand by the second.int quotient = 6 / 3; // quotient = 2
  • Modulus (%): Returns the remainder of the division.int remainder = 5 % 3; // remainder = 2

Unary Operators

  • Increment (++): Increases an integer value by one.int x = 5; x++; // x is now 6
  • Decrement (–): Decreases an integer value by one.int x = 5; x--; // x is now 4

Relational Operators

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

Basic Relational Operators

  • Equal to (==): Checks if two operands are equal.int result = (5 == 3); // result = 0 (false)
  • Not equal to (!=): Checks if two operands are not equal.int result = (5 != 3); // result = 1 (true)
  • Greater than (>): Checks if the first operand is greater than the second.int result = (5 > 3); // result = 1 (true)
  • Less than (<): Checks if the first operand is less than the second.int result = (5 < 3); // result = 0 (false)
  • Greater than or equal to (>=): Checks if the first operand is greater than or equal to the second.int result = (5 >= 3); // result = 1 (true)
  • Less than or equal to (<=): Checks if the first operand is less than or equal to the second.int result = (5 <= 3); // result = 0 (false)

Logical Operators

Logical operators are used to combine two or more conditions.

Basic Logical Operators

  • Logical AND (&&): Returns true if both operands are true.int result = (5 > 3 && 8 > 5); // result = 1 (true)
  • Logical OR (||): Returns true if at least one operand is true.int result = (5 > 3 || 8 < 5); // result = 1 (true)
  • Logical NOT (!): Reverses the logical state of its operand.int result = !(5 > 3); // result = 0 (false)

Bitwise Operators

Bitwise operators perform operations on the binary representations of numbers.

Basic Bitwise Operators

  • AND (&): Sets each bit to 1 if both bits are 1.int result = 5 & 3; // result = 1 (0001 in binary)
  • OR (|): Sets each bit to 1 if one of the bits is 1.int result = 5 | 3; // result = 7 (0111 in binary)
  • XOR (^): Sets each bit to 1 if only one of the bits is 1.int result = 5 ^ 3; // result = 6 (0110 in binary)
  • NOT (~): Inverts all the bits.int result = ~5; // result = -6 (inverts bits)
  • Left Shift (<<): Shifts bits to the left.int result = 5 << 1; // result = 10 (1010 in binary)
  • Right Shift (>>): Shifts bits to the right.int result = 5 >> 1; // result = 2 (0010 in binary)

Assignment Operators

Assignment operators are used to assign values to variables.

Basic Assignment Operators

  • Simple Assignment (=): Assigns the right operand to the left operand.int x = 5;
  • Add and Assign (+=): Adds the right operand to the left operand and assigns the result to the left operand.int x = 5; x += 3; // x is now 8
  • Subtract and Assign (-=): Subtracts the right operand from the left operand and assigns the result to the left operand.int x = 5; x -= 3; // x is now 2
  • Multiply and Assign (*=): Multiplies the left operand by the right operand and assigns the result to the left operand.int x = 5; x *= 3; // x is now 15
  • Divide and Assign (/=): Divides the left operand by the right operand and assigns the result to the left operand.int x = 6; x /= 3; // x is now 2
  • Modulus and Assign (%=): Takes modulus using two operands and assigns the result to the left operand.int x = 5; x %= 3; // x is now 2

Miscellaneous Operators

Sizeof Operator

The sizeof operator returns the size of a variable or data type in bytes.

int size = sizeof(int); // size = 4 (typically)

Conditional (Ternary) Operator

The conditional operator is a shorthand for the if-else statement.

int a = 10;
int b = 20;
int max = (a > b) ? a : b; // max = 20

Comma Operator

The comma operator allows two or more expressions to be evaluated in a single statement.

int a, b;
a = (b = 3, b + 2); // a = 5

Expressions

Expressions combine variables, constants, and operators to produce a new value.

Example of Expressions

int a = 5;
int b = 10;
int result = a + b * (b - a); // result = 55

Evaluation Order

C follows operator precedence and associativity rules to evaluate expressions.

Operator Precedence

Operator precedence determines the order in which operators are evaluated.

  • Highest Precedence: (), [], ->, . (Function call, array subscript, structure member access)
  • Lowest Precedence: =, +=, -=, *=, /=, %= (Assignment operators)

Operator Associativity

Associativity determines the order of evaluation for operators of the same precedence level.

  • Left-to-right: +, -, *, /, %, &, |, ^, ==, !=, <=, >=, <, > (Binary operators)
  • Right-to-left: =, +=, -=, *=, /=, %= (Assignment operators)

Example: Precedence and Associativity

int x = 5 + 3 * 2; // x = 11 (Multiplication before addition)
int y = (5 + 3) * 2; // y = 16 (Parentheses alter precedence)

Practical Examples in C

Here are some practical examples of using operators and expressions in C.

Example 1: Simple Arithmetic Operations

#include <stdio.h>

int main() {
int a = 10;
int b = 20;
int sum = a + b;
int difference = b - a;
int product = a * b;
int quotient = b / a;
int remainder = b % a;

printf("Sum: %d\n", sum);
printf("Difference: %d\n", difference);
printf("Product: %d\n", product);
printf("Quotient: %d\n", quotient);
printf("Remainder: %d\n", remainder);

return 0;
}

Example 2: Using Relational and Logical Operators

#include <stdio.h>

int main() {
int a = 10;
int b = 20;

if (a < b && b > 15) {
printf("Both conditions are true.\n");
}

if (a > b || b > 15) {
printf("At least one condition is true.\n");
}

if (!(a == b)) {
printf("a is not equal to b.\n");
}

return 0;
}

Example 3: Using Bitwise Operators

#include <stdio.h>

int main() {
int a = 5; // 0101 in binary
int b = 3; // 0011 in binary

int and = a & b; // 0001 in binary, 1 in decimal
int or = a | b; // 0111 in binary, 7 in decimal
int xor = a ^ b; // 0110 in binary, 6 in decimal
int not = ~a; // 1010 in binary, -6 in decimal
int leftShift = a << 1; // 1010 in binary, 10 in decimal
int rightShift = a >> 1; // 0010 in binary, 2 in decimal

printf("AND: %d\n", and);
printf("OR: %d\n", or);
printf("XOR: %d\n", xor);
printf("NOT: %d\n", not);
printf("Left Shift: %d\n", leftShift);
printf("Right Shift: %d\n", rightShift);

return 0;
}

Conclusion

Operators and expressions are essential for performing calculations and making decisions in C programs. By understanding how to use these operators effectively, you can create more complex and powerful programs.

Comments

Leave a Reply

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