Detailed Explanation of C Language Operators

C language provides a rich set of operators to perform various operations, including arithmetic operations, logical operations, bitwise operations, comparisons, and assignments.

Below are the common categories and descriptions of operators in C language:

1. Arithmetic Operators

Used for basic mathematical operations:

  • <span>+</span>Addition (e.g., <span>a + b</span>)
  • <span>-</span>Subtraction (e.g., <span>a - b</span>)
  • <span>*</span>Multiplication (e.g., <span>a * b</span>)
  • <span>/</span>Division (integer division truncates the decimal part, e.g., <span>5 / 2 = 2</span>)
  • <span>%</span>Modulus (remainder, only for integers, e.g., <span>5 % 2 = 1</span>)

2. Assignment Operators

Used to assign values to variables:

  • <span>=</span>Basic assignment (e.g., <span>a = 5</span>)
  • Compound assignment:<span>+=</span>, <span>-=</span>, <span>*=</span>, <span>/=</span>, <span>%=</span> (e.g., <span>a += 3</span> is equivalent to <span>a = a + 3</span>)

3. Increment and Decrement Operators

  • <span>++</span>Increment (increases the variable value by 1)
  • <span>--</span>Decrement (decreases the variable value by 1)

The difference between prefix form (<span>++a</span>) and postfix form (<span>a++</span>):

  • Prefix: modifies the variable value first, then uses the variable
  • Postfix: uses the variable value first, then modifies the variable
int a = 5, b;b = ++a;  // a becomes 6, b equals 6b = a++;  // b equals 6, a becomes 7

4. Relational Operators

Used to compare two values:

  • <span>==</span>Equal (note the distinction from assignment<span>=</span>)
  • <span>!=</span>Not equal
  • <span>></span>Greater than
  • <span><</span>Less than
  • <span>>=</span>Greater than or equal to
  • <span><=</span>Less than or equal to

Returns 1 (true) or 0 (false).

5. Logical Operators

Used for logical operations:

  • <span>&&</span>Logical AND (true only if both sides are true)
  • <span>||</span>Logical OR (true if at least one side is true)
  • <span>!</span>Logical NOT (negation)

Short-circuit behavior:

  • <span>&&</span>If the left side is false, the right side is not evaluated
  • <span>||</span>If the left side is true, the right side is not evaluated

6. Bitwise Operators

Used to operate on binary bits:

  • <span>&</span>Bitwise AND
  • <span>|</span>Bitwise OR
  • <span>^</span>Bitwise XOR (same is 0, different is 1)
  • <span>~</span>Bitwise NOT
  • <span><<</span>Left shift (shifts binary bits to the left)
  • <span>>></span>Right shift (shifts binary bits to the right)

Demo

int a = 5;    // binary: 0101int b = 3;    // binary: 0011int c = a & b; // 0001 (1)int d = a | b; // 0111 (7)int e = a ^ b; // 0110 (6)int f = a << 1; // 1010 (10)

7. Conditional Operator (Ternary Operator)

Form:<span>expression1 ? expression2 : expression3</span><span>If expression 1 is true, return the value of expression 2; otherwise, return the value of expression 3.</span><span>demo</span>

int max = (a > b) ? a : b; // get the maximum of a and b

8. Comma Operator

<span>Separates multiple expressions, executed from left to right, returning the value of the last expression.</span>

int a = (3 + 5, 5 * 2, 10 - 3); // a's value is 7

9. Pointer Operators

  • <span>*</span>Dereference operator (accesses the value pointed to by the pointer)
  • <span>&</span>Address-of operator (gets the address of a variable)

<span>demo</span>

int a = 10;int *p = &a; // p points to the address of aprintf("%d", *p); // outputs 10, accesses the value pointed to by p

10. Member Access Operators

<span>Used to access members of structures or unions:</span>

  • <span>.</span>Direct access (for structure variables)
  • <span>-></span>Access through pointer (for structure pointers)
struct Person {    char name[20];    int age;};struct Person p1 = {"Alice", 20};struct Person *p = &p1printf("%s", p1.name); // access using .printf("%d", p->age);  // access using ->

11. Precedence and Associativity

<span>The precedence of operators determines the order of operations, and in cases of equal precedence, associativity determines the order (left to right or right to left).</span><span>For example: multiplication takes precedence over addition, and the assignment operator is right associative.</span>

int a = 3 + 5 * 2; // first calculates 5*2, result is 13int b = c = 5;     // right associative, equivalent to b = (c = 5)

Leave a Comment