Lesson 06·C Programming

Operators & Precedence

Mathematical, logical, relational, and bitwise operators with precedence rules

What is an Operator?

An operator is a special symbol that tells the compiler to perform specific mathematical, relational, or logical manipulations on operands.

Categories of C Operators

The eight primary operator classifications available in C.
Operator CategoryOperators
Assignment=
Arithmetic+, -, *, /, %
Logical&&, ||, !
Relational<, >, <=, >=, ==, !=
Shorthand (Compound)+=, -=, *=, /=, %=
Unary++, --
Conditional (Ternary)? :
Bitwise&, |, ^, <<, >>, ~

Arithmetic Operators Example

Demonstration of basic arithmetic operations and increment/decrement:

C Example
#include <stdio.h>

int main() {
    int a = 21;
    int b = 10;
    int c;

    c = a + b;   // c is 31
    printf("a + b = %d\n", c);

    c = a - b;   // c is 11
    printf("a - b = %d\n", c);

    c = a * b;   // c is 210
    printf("a * b = %d\n", c);

    c = a / b;   // c is 2 (integer division)
    printf("a / b = %d\n", c);

    c = a % b;   // c is 1 (modulus remainder)
    printf("a %% b = %d\n", c);

    c = a++;     // c is 21 (post-increment, a becomes 22)
    printf("a++ = %d\n", c);

    c = a--;     // c is 22 (post-decrement, a becomes 21)
    printf("a-- = %d\n", c);

    return 0;
}
Program Output
a + b = 31
a - b = 11
a * b = 210
a / b = 2
a % b = 1
a++ = 21
a-- = 22

Bitwise Operators

Bitwise operators work directly on binary bits and perform bit-by-bit manipulation:

C Example
#include <stdio.h>

int main() {
    unsigned int a = 60;  /* 60 = 0011 1100 */
    unsigned int b = 13;  /* 13 = 0000 1101 */
    int c = 0;

    c = a & b;            /* 12 = 0000 1100 */
    printf("a & b = %d\n", c);

    c = a | b;            /* 61 = 0011 1101 */
    printf("a | b = %d\n", c);

    c = a ^ b;            /* 49 = 0011 0001 */
    printf("a ^ b = %d\n", c);

    c = ~a;               /* -61 = 1100 0011 */
    printf("~a    = %d\n", c);

    c = a << 2;           /* 240 = 1111 0000 */
    printf("a << 2 = %d\n", c);

    c = a >> 2;           /* 15 = 0000 1111 */
    printf("a >> 2 = %d\n", c);

    return 0;
}
Program Output
a & b = 12
a | b = 61
a ^ b = 49
~a    = -61
a << 2 = 240
a >> 2 = 15
Assume A = 60 and B = 13. In 8-bit binary format: A = 0011 1100 B = 0000 1101 Bitwise Calculations: A & B = 0000 1100 (Bitwise AND: 12) A | B = 0011 1101 (Bitwise OR: 61) A ^ B = 0011 0001 (Bitwise XOR: 49) ~A = 1100 0011 (Bitwise NOT: -61 in 2's complement)

Conditional Operator (Ternary ? :)

Syntax: condition ? expression1 : expression2;

C Example
#include <stdio.h>

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

    b = (a == 1) ? 20 : 30;
    printf("Value of b is %d\n", b);

    b = (a == 10) ? 20 : 30;
    printf("Value of b is %d\n", b);

    return 0;
}
Program Output
Value of b is 30
Value of b is 20

Precedence & Associativity Table

Operator precedence determines the grouping and evaluation order of terms in an expression. For example, in x = 7 + 3 * 2, x evaluates to 13 (not 20) because multiplication (*) has higher precedence than addition (+). Higher precedence operators appear at the top.

Full C language operator precedence hierarchy from highest to lowest priority.
CategoryOperatorAssociativity
Postfix() [] -> . ++ --Left to Right
Unary+ - ! ~ ++ -- (type) * & sizeofRight to Left
Multiplicative* / %Left to Right
Additive+ -Left to Right
Shift<< >>Left to Right
Relational< <= > >=Left to Right
Equality== !=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