Operators in C

Definition

In C programming, an operator is a symbol that represents an action to be taken with the operands (data values) in an expression. Operators in C can perform various operations such as arithmetic calculations, logical comparisons, assignment, bitwise operations, and more. They are fundamental building blocks used to manipulate data and control the flow of a program. Operators in C are classified into several categories based on their functionality, including arithmetic, relational, logical, assignment, bitwise, and conditional operators. Understanding and correctly using operators are crucial skills for writing efficient and effective C programs.

Different types of operators in C

Operators in C are categorized into different types based on their functionality:

Arithmetic Operators:

  • Addition (+): Adds two operands together to produce a sum.
  • Subtraction (-): Subtracts the second operand from the first operand to get the difference.
  • Multiplication (*): Multiplies two operands to get the product.
  • Division (/): Divides the first operand by the second operand, resulting in a quotient.
  • Modulus (%): Computes the remainder of the division of the first operand by the second operand. These operators are fundamental for performing mathematical calculations in C programs.
int a = 10, b = 3;
int sum = a + b;          // sum = 13
int difference = a - b;    // difference = 7
int product = a * b;       // product = 30
int quotient = a / b;      // quotient = 3
int remainder = a % b;     // remainder = 1

Relational Operators:

  • Greater than (>): Returns true if the left operand is greater than the right operand.
  • Less than (<): Returns true if the left operand is less than the right operand.
  • Greater than or equal to (>=): Returns true if the left operand is greater than or equal to the right operand.
  • Less than or equal to (<=): Returns true if the left operand is less than or equal to the right operand.
  • Equal to (==): Returns true if the operands are equal.
  • Not equal to (!=): Returns true if the operands are not equal. These operators are commonly used for making decisions based on conditions in control structures like if statements and loops.
int a = 10, b = 5;
if (a > b)      // true
if (a < b)      // false
if (a >= b)     // true
if (a <= b)     // false
if (a == b)     // false
if (a != b)     // true

Logical Operators:

  • Logical AND (&&): Returns true if both operands are true.
  • Logical OR (||): Returns true if either of the operands is true.
  • Logical NOT (!): Reverses the logical state of its operand. These operators are used to combine multiple conditions and control the flow of execution in C programs.
int a = 1, b = 0;
if (a && b)     // false
if (a || b)     // true
if (!a)         // false

Assignment Operators:

  • =: Simple assignment operator. Assigns the value on the right to the variable on the left.
  • +=: Adds the value on the right to the variable on the left and assigns the result to the variable on the left.
  • -=: Subtracts the value on the right from the variable on the left and assigns the result to the variable on the left.
  • Similarly, there are *=, /=, and %= operators for other arithmetic operations combined with assignment. Assignment operators are used to update the values of variables based on calculations or other conditions.
int a = 10, b = 5, result;
result += a;   // result = result + a;
result -= b;   // result = result - b;

Increment and Decrement Operators:

  • ++: Increment operator. Increases the value of the operand by 1.
  • : Decrement operator. Decreases the value of the operand by 1. These operators are often used in loops and other situations where you need to update the value of a variable by a fixed amount.
int a = 5;
a++;    // a becomes 6
a--;    // a becomes 5 again

Bitwise Operators:

  • &: Bitwise AND
  • |: Bitwise OR
  • ^: Bitwise XOR
  • ~: Bitwise NOT
  • <<: Left shift
  • >>: Right shift Bitwise operators perform operations at the bit level, manipulating individual bits of integer operands.
#include <stdio.h>

int main() {
    unsigned int a = 5; // binary: 0000 0101
    unsigned int b = 3; // binary: 0000 0011
    unsigned int result;

    // Bitwise AND (&)
    result = a & b; // 0000 0001
    printf("Result of bitwise AND: %u\n", result);

    // Bitwise OR (|)
    result = a | b; // 0000 0111
    printf("Result of bitwise OR: %u\n", result);

    // Bitwise XOR (^)
    result = a ^ b; // 0000 0110
    printf("Result of bitwise XOR: %u\n", result);

    // Bitwise NOT (~)
    result = ~a; // 1111 1010 (2's complement of 5)
    printf("Result of bitwise NOT for 'a': %u\n", result);

    // Left Shift (<<)
    result = a << 1; // 0000 1010 (left shift by 1)
    printf("Result of left shift for 'a': %u\n", result);

    // Right Shift (>>)
    result = a >> 1; // 0000 0010 (right shift by 1)
    printf("Result of right shift for 'a': %u\n", result);

    return 0;
}

Output:

Result of bitwise AND: 1
Result of bitwise OR: 7
Result of bitwise XOR: 6
Result of bitwise NOT for 'a': 4294967290
Result of left shift for 'a': 10
Result of right shift for 'a': 2

Conditional Operator (Ternary Operator):

  • ? :: Ternary operator. It evaluates a condition and returns one of two values depending on whether the condition is true or false. This operator provides a concise way of writing conditional expressions.
int a = 10, b = 5, max;
max = (a > b) ? a : b; // max = 10

Understanding and mastering these operators is essential for writing efficient and correct C programs. They provide the necessary tools for performing various computations, making decisions, and controlling program flow.

Precedence and Associativity of operators in C

  • Precedence: Operators with higher precedence are evaluated before operators with lower precedence.
  • Associativity: Specifies the order in which operators of the same precedence are evaluated. Left to right associativity means operators are evaluated from left to right, while right to left associativity means operators are evaluated from right to left.

Below is a table detailing the precedence and associativity of operators in C, listed from highest precedence to lowest precedence. Operators with the same precedence are evaluated from left to right unless otherwise specified.

Operator NameSign(s)AssociativityPrecedence
Function Call/Indexing/Member Access() [] -> .Left to rightHighest
Increment/Decrement++ —Right to left
Unary Operators– + ! ~ & * sizeofRight to left
Multiplication/Division/Modulus* / %Left to right
Addition/Subtraction+ –Left to right
Bitwise Shift<< >>Left to right
Relational Operators< <= > >=Left to right
Equality Operators== !=Left to right
Bitwise AND&Left to right
Bitwise XOR^Left to right
Bitwise OR|Left to right
Logical AND&&Left to right
Logical OR||Left to right
Ternary Conditional?:Right to left
Assignment/Compound Assignment= += -= *= /= %= &= ^= |= <<= >>=Right to left
Comma,Left to rightLowest
Precedence and Associativity of operators

This table provides the names of operators along with their corresponding signs, followed by associativity and precedence.

For example:

int result = 1 + 2 * 3;  // result = 7 (multiplication (*) has higher precedence than addition (+))
int a = 5;
int b = 6;
int c = (a > b) ? a : b; // c will be assigned the greater value between a and b

Conclusion

In conclusion, operators in C are symbols used to perform various operations on operands, facilitating tasks such as arithmetic computations, logical evaluations, and bitwise manipulations. These operators have different precedence and associativity, dictating the order of evaluation within expressions. Mastery of operators is essential for writing efficient and concise C code, enabling programmers to effectively manipulate data and control program flow. Understanding operator behavior ensures correct expression evaluation and enhances code readability. With their versatility and power, operators serve as fundamental building blocks in C programming, empowering developers to create complex algorithms and applications.