The Ultimate C Operators Guide: Precedence, Associativity and All 7 Types Explained

Table of Contents

What Are Operators in C?

In C an operator is a special symbol used to perform operations on data. C provides a rich set of operators grouped by functionality.

For example:

int a = 5 + 3;

 

1. Arithmetic Operators

These operators execute fundamental arithmetic computations. They are mainly used in calculators.

OperatorDescriptionExample
+Additiona + b
-Subtractiona - b
*Multiplicationa * b
/Divisiona / b
%Modulus (remainder)a % b
				
					#include <stdio.h>
int main() {
    int num1 = 15;
    int num2 = 4;
    int result;
    
    // 1. Addition 
    result = num1 + num2;
    printf("Addition = %d\n", result);            // 19

    // 2. Subtraction
    result = num1 - num2;
    printf("Subtraction = %d\n", result);         // 11

    // 3. Multiplication
    result = num1 * num2;
    printf("Multiplication = %d\n", result);      // 60

    // 4. Division
    result = num1 / num2;
    printf("Division = %d\n", result);            // 3

    // 5. Modulo
    result = num1 % num2;
    printf("Modulo = %d\n", result);              // 3
    return 0;   
}
				
			

The above code demonstrates all the simple operations for the Arithmetic Operators.

 

2. Relational Operators

These are used to compare two or more values and check their relationship.

OperatorMeaning
==Equal to
!=Not Equal
>Greater Than
<Smaller Than
>=Greater Than or Equal to
<=Smaller Than or Equal to
				
					#include <stdio.h>
int main() {
    int a = 20;
    int b = 15;
    printf("a = %d\n", a);
    printf("b = %d\n", b);

    // 1. Equal to (==)
    printf("(20 == 15): %d \n", a == b);          // (20 == 15): 0 


    // 2. Not equal to (!=)
    printf("(20 != 15): %d \n", a != b);         // (20 != 15): 1 


    // 3. Greater than (>)
    printf("(20 > 15): %d \n", a > b);           // (20 > 15): 1 


    // 5. Less than (<)
    printf("(20 < 15): %d\n", a < b);            // (20 < 15): 0


    // 6. Greater than or equal to (>=)
    printf("(20 >= 20): %d \n", a >= 20);        // (20 >= 20): 1 


    // 7. Less than or equal to (<=)
    printf("(15 <= 10): %d \n", b <= 10);        // (15 <= 10): 0 

    
    return 0;
}
				
			

3. Logical Operators

They are used to combine multiple conditions.

OperatorMeaningDescription
&&Logical ANDIt Returns true only if both conditions are true.
||Logical ORIt return true if any one condition is true.
!Logical NOTIt reverses a condition(true -> false, false -> true)
				
					#include <stdio.h>
int main() {
    int A = 1;  // True (non-zero)
    int B = 0;  // False (zero)
    int C = 5;  // True (non-zero)
    printf("A = %d (True)\n", A);
    printf("B = %d (False)\n", B);
    printf("C = %d (True)\n", C);

    // 2. Logical AND (True only if both operands are non-zero)
    printf("A && B (1 AND 0) = %d\n", A && B);      // False (0)
    printf("A && C (1 AND 5) = %d\n", A && C);      // True (1)

    // 3. Logical OR (||) (True if at least one operand is non-zero)
    printf("A || B (1 OR 0) = %d\n", A || B);      // True(1)
    printf("B || B (0 OR 0) = %d\n", B || B);      // False(0) 

    // 4. Logical NOT (!) (Reverses the logical state)
    printf("!A (NOT 1) = %d\n", !A);               // False(0)    
    printf("!B (NOT 0) = %d\n", !B);               // True(1) 

    return 0;
}
				
			

The above code demonstrates the operations of Logical Operators.

 

4. Assignment Operators

Assign values to variables. Operators used to assign a value to a variable. Assignment operators (+=, -= ) operate and assign the result in one step

OperatorUsageEquivalent to
= a = bAssignment
+= a += ba = a + b
-= a -= ba = a - b
*= a *= ba = a * b
/= a /= ba = a / b
%= a %= ba = a % b
				
					#include <stdio.h>
int main() {
    int x = 10;
    int y = 5;
    printf("x = %d\n", x);
    printf("y = %d\n", y);

    // 1. Assignment Operator (=)
    int a = 100;
    printf("1. a = %d\n", a);            // 100

    // 2. Add and assign (+=)
    x += y; // x = x + y (10 + 5)
    printf("2. x = %d\n", x);            // 15

    // 3. Subtract and assign (-=)
    x = 10; 
    x -= y; // x = x - y (10 - 5)
    printf("3. x = %d\n", x);            // 5

    // 4. Multiply and assign (*=)
    x = 10; 
    x *= y; // x = x * y (10 * 5)
    printf("4. x = %d\n", x);            // 50

    // 5. Divide and assign (/=)
    x = 10; 
    x /= y; // x = x / y (10 / 5)
    printf("5. x = %d\n", x);            // 2

    // 6. Modulo and assign (%=)
    x = 17; // Use a different number for a clear remainder
    x %= y; // x = x % y (17 % 5, remainder is 2)
    printf("6. x = %d\n", x);            // 2

    return 0;
}
				
			

The above demonstrates all the operations of the Assignment Operator.

5. Increment & Decrement Operators

Unary operators (++ and–) that increase or decrease a variable’s value by exactly one. 

TypeOperatorExample CodeWhat Happens
Pre-Increment++aint a = 5; int b = ++a;Value of a is increased first then assigned
a = 6, b = 6
Post-Incrementa++int a = 5; int b = a++;Value of a is assigned first then increased
a = 6, b = 5
Pre-Decrement--aint a = 5; int b = --a;Value of a is decreased first, then assigned
a = 4, b = 4
Post-Decrementa--int a = 5; int b = a--;Value of a is assigned first, then decreased
a = 4, b = 5
				
					#include <stdio.h>

int main() {
    int x = 10;
    int y = 10;
    int temp_x = 0;
    int temp_y = 0;
    printf("Initial x = %d, Initial y = %d\n", x, y);

    // 1. Postfix Increment (x++)
    temp_x = x++;  // 10 is assigned to temp_x
    printf("   temp_x = %d\n", temp_x); 
    printf("   x (Final Value) = %d\n", x); // after assignment value is increased by 1
    printf("\n");

    // 2. Prefix Increment (++y)
    temp_y = ++y;  // y is immediately increased by 1
    printf("   temp_y = %d\n", temp_y); 
    printf("   y (Final Value) = %d\n", y);   
    printf("\n");
    
    // Reset for decrement
    x = 10;
    y = 10;

    // 3. Postfix Decrement (x--)
    temp_x = x--;  //  10 is first assigned to temp_x
    printf("   temp_x (Value RETURNED) = %d\n", temp_x); 
    printf("   x (Final Value) = %d\n", x);  // after assignment value is decreased by 1
    printf("\n");

    // 4. Prefix Decrement (--y)
    temp_y = --y;  // y is decreased by 1 and then assigned to temp_y
    printf("   temp_y (Value RETURNED) = %d\n", temp_y); 
    printf("   y (Final Value) = %d\n", y);
    return 0;
}

// Initial x = 10, Initial y = 10
//   temp_x = 10
//   x (Final Value) = 11

//   temp_y = 11
//   y (Final Value) = 11

//   temp_x (Value RETURNED) = 10
//   x (Final Value) = 9

//   temp_y (Value RETURNED) = 9
//   y (Final Value) = 9

				
			

6. Bitwise Operators

Operators that perform manipulation and calculations at the individual bit level of integer types. 

OperatorNameDescriptionExampleResult Explanation
&Bitwise ANDCompares each bit of two numbers result bit is 1 only if both bits. are 15 & 30101 &
0011 = 0001 (1)
!Bitwise ORThe bitwise OR operator compares each bit of two numbers. A bit becomes 1 if either of the bits is 1.5 ! 30101 (5)
0011 (3) = 0111 (7)
^Bitwise XORResult bit is 1 only if bits are different5 ^ 30101 ^
0011 = 0110 (6)
~Bitwise NOTFlips all bits (1 → 0, 0 → 1). Produces the 2’s complement negative of the number~5~00000101 = 11111010 (-6 in 8-bit)
<<Left ShiftShifts bits to the left; fills with 0 from the right.5 << 100000101 << 1 = 00001010 (10)
>>Right ShiftShifts bits to the right; discards rightmost bits. Fills left with sign bit (for signed numbers)5 >> 100000101 >> 1 = 00000010 (2)
				
					#include <stdio.h>

int main() {
    int a = 12; // Binary: 0000 1100
    int b = 25; // Binary: 0001 1001
    int result;

    printf("A = %d\n", a);
    printf("B = %d\n", b);

    // 1. Bitwise AND (&)
    // 0000 1100 & 0001 1001 = 0000 1000 (8)
    result = a & b;
    printf("1. A & B (AND) = %d\n", result);

    // 2. Bitwise OR (|)
    // 0000 1100 | 0001 1001 = 0001 1101 (29)
    result = a | b;
    printf("2. A | B (OR) = %d\n", result);

    // 3. Bitwise XOR (^)
    result = a ^ b;
    printf("3. A ^ B (XOR)  = %d\n", result);

    // 4. Bitwise NOT (~)
    // Note: Result is a large negative number in a standard 32-bit signed integer.
    // ~12 (0...00001100) -> 1...11110011 (-13)
    result = ~a;
    printf("4. ~A (NOT) = %d\n", result);

    // 5. Left Shift (<<) - Multiplication by power of 2
    // 12 << 2: 0000 1100 << 2 = 0011 0000 (48)
    result = a << 2;
    printf("5. A << 2  = %d\n", result);

    // 6. Right Shift (>>) 
    // 25 >> 1: 0001 1001 >> 1 = 0000 1100 (12)
    result = b >> 1;
    printf("6. B >> 1 = %d\n", result);

    return 0;
}
				
			

7. (Ternary) Operator

A unique operator that evaluates a condition and returns one of two expressions based on whether the condition is true or false. 

				
					#include <stdio.h>
int main() {
    int score = 75;
    int passing_grade = 60;
    char *result_message;
    printf("Student Score: %d\n", score);
    printf("Passing Grade: %d\n", passing_grade);

    result_message = (score >= passing_grade) ? "Pass" : "Fail";

    printf("The result is: %s\n", result_message);

    return 0;
}

				
			

Precedence and Associativity

Operator precedence determines the grouping of terms in an expression and dictates which operations are performed first. C uses operator precedence to determine the order of evaluation: operators with higher precedence are processed before those with lower precedence. When operators share the same level of precedence, their associativity.(left-to-right or right-to-left) dictates the sequence of execution..

CategoryOperatorAssociativity
Parenthesis and brackets() [] -> . ++ --Left to Right
Unary++ -- + - ! ~ * & sizeofRight to Left
Multiplication / Division* / %Left to Right
Addition / Subtraction+ -Left to Right
Bitwise Shift<< >>Left to Right
Relational< <= > >=Left to Right
Equity != ==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
Conditional? :Right to Left
Assignment+=, -=, =, *=, /=, %=, << =, >>=, &=, ^=, |=Right to Left
Comma,Left to Right

Consider an example

10 + 5 * 2 – 6 / 3 + 8 % 5

Step-by-Step Solution:

Step 1 → Multiplication, Division, Modulus (Same precedence, Left to Right)

  • 5 * 2 = 10

  • 6 / 3 = 2

  • 8 % 5 = 3

So now the expression becomes

10 + 10 – 2 + 3

Step 2 → Addition and Subtraction (Left to Right)

  • 10 + 10 = 20

  • 20 – 2 = 18

  • 18 + 3 = 21

Final answer is 21

Example 2: (10 + 2) * 3 – 4 / (2 + 2) + 5 * (3 – 1) – –x

Consider x = 5

Step 1 → Solve Parentheses first

  • (10 + 2) = 12

  • (2 + 2) = 4

  • (3 – 1) = 2

Now the expression becomes

12 * 3 – 4 / 4 + 5 * 2 – –x

Step 2 → Unary Operator ( –x)

  • Initial x = 5

  • –x  becomes 4 value used = 4

Now the expression is:

12 * 3 – 4 / 4  + 5 * 2 – 4
 

Step 3 → Multiplication and Division (Left to Right)

  • 12 * 3 = 36

  • 4 / 4 = 1

  • 5 * 2 = 10

The expression becomes

36 – 1 + 10 – 4

Step 4 → Addition and Subtraction (Left to Right)

  • 36 – 1 = 35

  • 35 + 10 = 45

  • 45 – 4 = 41

result = 41

Frequently Asked Questions (FAQs)

1) What is a bitwise operator in C?

Bitwise operators in C are used to perform operations on individual bits of integers.
These operators allow for bit-level manipulation such as & (AND), | (OR), ^ (XOR), ~ (NOT), << (left shift) and >> (right shift). They are commonly used in embedded systems, low-level programming, compression, encryption and optimization.

Example:

int a = 5; // 0101
int b = 3; // 0011
int result = a & b; // result = 1

 

2) What is the modulus operator in C?

It returns the remainder when one number is divided by another.

Example:

int a = 10;
int b = 3;
printf(“%d”, a % b); // Output: 1

It is often used in loops, condition checks, even/odd detection, and cyclic processes.

 

3) What is a ternary operator?

The ternary operator (?:) is a shorthand for an if-else statement. This describes the conditional (Ternary) Operator which evaluates a specified Boolean condition and returns one of two possible expressions based on the outcome (true or false).
Syntax:

(condition) ? value_if_true : value_if_false;

Example:

int max = (a > b) ? a : b;

 

4) What is the difference between == and = in C?

= is used to assign values.

== is the equality operator (used to compare two values).

Example:

a = 5; // assignment
if(a == 5) // comparison Mixing them up causes logical errors in programs.

 

5) What is operator precedence in C?

Operator precedence decides which operator gets evaluated first in an expression.
For example, in:

int x = 10 + 5 * 2;

Because the multiplication operator (*) has a higher precedence than the addition operator (+), multiplication is always performed first.
Result: x = 20.

Associativity decides evaluation order when two operators have the same precedence.