Mastering C Operators: Comprehensive Guide with Examples and Detailed Explanations
C Programming Reference
Operator Precedence Table
Operator | Description | Associativity |
---|---|---|
() [] -> . |
Function call, array subscripting, member access | Left to Right |
++ -- + - ~ ! |
Increment, decrement, unary plus, minus, bitwise NOT, logical NOT | Right to Left |
(type) sizeof * & |
Type cast, size of, dereference, address-of | Right to Left |
* / % |
Multiplication, division, modulo | Left to Right |
+ - |
Addition, subtraction | Left to Right |
<< >> |
Bitwise shift left, right | Left to Right |
< <= > >= |
Relational operators | Left to Right |
== != |
Equality and inequality | 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 operator | Right to Left |
= += -= *= /= %= |
Assignment operators | Right to Left |
, |
Comma operator | Left to Right |
ASCII Codes for C
Character | ASCII Code | Character | ASCII Code |
---|---|---|---|
Space | 32 | `a` | 97 |
`!` | 33 | `b` | 98 |
`”` | 34 | `c` | 99 |
`#` | 35 | `d` | 100 |
`$` | 36 | `e` | 101 |
`%` | 37 | `f` | 102 |
`&` | 38 | `g` | 103 |
`’` | 39 | `h` | 104 |
`(` | 40 | `i` | 105 |
`)` | 41 | `j` | 106 |
`*` | 42 | `k` | 107 |
`+` | 43 | `l` | 108 |
`0` | 48 | `m` | 109 |
`1` | 49 | `n` | 110 |
`2` | 50 | `o` | 111 |
`3` | 51 | `p` | 112 |
`4` | 52 | `q` | 113 |
`5` | 53 | `r` | 114 |
`6` | 54 | `s` | 115 |
`7` | 55 | `t` | 116 |
`8` | 56 | `u` | 117 |
`9` | 57 | `v` | 118 |
`:` | 58 | `w` | 119 |
`;` | 59 | `x` | 120 |
`<` | 60 | `y` | 121 |
`=` | 61 | `z` | 122 |
Operators 1: Basic Arithmetic Operators in C
Program
main()
{
int x;
x = -3 + 4 * 5 - 6;
printf("%d\n", x); // Operators 1.1
x = 3 + 4 % 5 – 6;
printf(“%d\n”, x); // Operators 1.2
x = -3 * 4 % -6 / 5;
printf(“%d\n”, x); // Operators 1.3
x = (7 + 6) % 5 / 2;
printf(“%d\n”, x); // Operators 1.4
}
Output
- Operators 1.1: 11
- Operators 1.2: 1
- Operators 1.3: 0
- Operators 1.4: 1
Arithmetic Operators Table
Operator | Yields | Restrictions |
---|---|---|
x + y |
Sum of x and y |
If either operand is a pointer, the other must be integral |
x - y |
Difference of x and y |
If either operand is a pointer, the other must be integral or same base type |
x * y |
Product of x and y |
x and y must not be pointers |
x / y |
Quotient of x divided by y |
x and y must not be pointers |
x % y |
Remainder of dividing x by y |
x and y must not be double, float, or pointers |
-x |
Arithmetic negation of x |
x must not be a pointer |
Incremental Operators
Operator | Yields | Restrictions |
---|---|---|
x++ (x-- ) |
Value of x , then incremented or decremented |
x must be a reference to a numeric value or pointer |
++x (--x ) |
Value of x incremented or decremented, then used |
x must be a reference to a numeric value or pointer |
Assignment Operators
Operator | Yields | Restrictions |
---|---|---|
x = y |
y cast to the type of x , assigns value to x |
x and y may be any type except array |
x op= y |
Performs operation op on x and y , assigns to x |
x and y may be any type except array or structure |
Operators in C
Operators 1: Basic Arithmetic Operators
Program
main()
{
int x;
x = -3 + 4 * 5 - 6;
printf("%d\n", x); // Operators 1.1
x = 3 + 4 % 5 – 6;
printf(“%d\n”, x); // Operators 1.2
x = -3 * 4 % -6 / 5;
printf(“%d\n”, x); // Operators 1.3
x = (7 + 6) % 5 / 2;
printf(“%d\n”, x); // Operators 1.4
}
Output
- Operators 1.1: 11
- Operators 1.2: 1
- Operators 1.3: 0
- Operators 1.4: 1
Operators 2: Assignment Operators
Program
#define PRINTX printf("%d\n", x)
main()
{
int x = 2, y, z;
x *= 3 + 2;
PRINTX; // Operators 2.1
x *= y = z = 4;
PRINTX; // Operators 2.2
x = y == z;
PRINTX; // Operators 2.3
x == (y = z);
PRINTX; // Operators 2.4
}
Output
- Operators 2.1: 10
- Operators 2.2: 40
- Operators 2.3: 1
- Operators 2.4: 1
Arithmetic Operators Table
Operator | Yields | Restrictions |
---|---|---|
x + y |
Sum of x and y |
If either operand is a pointer, the other must be integral |
x - y |
Difference of x and y |
If either operand is a pointer, the other must be integral or same base type |
x * y |
Product of x and y |
x and y must not be pointers |
x / y |
Quotient of x divided by y |
x and y must not be pointers |
x % y |
Remainder of dividing x by y |
x and y must not be double, float, or pointers |
-x |
Arithmetic negation of x |
x must not be a pointer |
Incremental Operators
Operator | Yields | Restrictions |
---|---|---|
x++ (x-- ) |
Value of x , then incremented or decremented |
x must be a reference to a numeric value or pointer |
++x (--x ) |
Value of x incremented or decremented, then used |
x must be a reference to a numeric value or pointer |
Assignment Operators
Operator | Yields | Restrictions |
---|---|---|
x = y |
y cast to the type of x , assigns value to x |
x and y may be any type except array |
x op= y |
Performs operation op on x and y , assigns to x |
x and y may be any type except array or structure |
Operators in C
Operators 3: Logical Operators
Program
#define PRINT(int) printf("%d\n", int)
main()
{
int x, y, z;
x = 2;
y = 1;
z = 0;
x = x && y || z;
PRINT(x); // Operators 3.1
PRINT(x || !y && z); // Operators 3.2
x = y = 1;
z = x++ – 1;
PRINT(x);
PRINT(z); // Operators 3.3
z += -x++ + ++y;
PRINT(x);
PRINT(z); // Operators 3.4
z = x / ++x;
PRINT(z); // Operators 3.5
}
Output
- Operators 3.1: 1
- Operators 3.2: 1
- Operators 3.3: 2
- Operators 3.4: 0
- Operators 3.5: Undefined Behavior (division by incremented variable)
Explanation
Operators 3.1: Logical AND (&&
) and OR (||
) are evaluated. x && y
evaluates to true
, and true || z
results in true
. Thus, x = 1
.
Operators 3.2: Logical OR (||
), NOT (!
), and AND (&&
) are evaluated. x || !y && z
results in 1
.
Operators 3.3: x = y = 1
sets both x
and y
to 1
. z = x++ - 1
uses post-increment, so z = 1 - 1 = 0
. Results: x = 2
, z = 0
.
Operators 3.4: z += -x++ + ++y
: Post-increment x
and pre-increment y
. z += -2 + 2 = 0
. Results: x = 3
, z = 0
.
Operators 3.5: z = x / ++x
is undefined behavior as x
is incremented during evaluation.
Operators in C
Operators 4: Bitwise Operators
Program
#define PRINT(int) printf("int = %d\n", int)
main()
{
int x, y, z;
x = 03; y = 02; z = 01;
PRINT(x | y & z); // Operators 4.1
PRINT(x | y & ~z); // Operators 4.2
PRINT(x ^ y & -z); // Operators 4.3
PRINT(x & y && z); // Operators 4.4
x = 1; y = -1;
PRINT(!x | x); // Operators 4.5
PRINT(~x | x); // Operators 4.6
PRINT(x ^ x); // Operators 4.7
x <<= 3;
PRINT(x); // Operators 4.8
y <<= 3; PRINT(y); // Operators 4.9 y >>= 3;
PRINT(y); // Operators 4.10
}
Output
- Operators 4.1: 3
- Operators 4.2: 3
- Operators 4.3: 1
- Operators 4.4: 1
- Operators 4.5: 1
- Operators 4.6: -1
- Operators 4.7: 0
- Operators 4.8: 8
- Operators 4.9: -8
- Operators 4.10: -1
Explanation
Operators 4.1: x | y & z
: Bitwise AND has higher precedence, so y & z
is evaluated first, resulting in 03 | 01 = 3
.
Operators 4.2: x | y & ~z
: The bitwise NOT ~z
flips the bits of z
, and then y & ~z
is evaluated, resulting in 3
.
Operators 4.3: x ^ y & -z
: Bitwise XOR evaluates to 1
.
Operators 4.4: x & y && z
: Logical AND (&&
) has lower precedence, so bitwise AND is evaluated first, resulting in 1
.
Operators 4.5: !x | x
: Logical NOT of x
results in 1
.
Operators 4.6: ~x | x
: Bitwise NOT results in -1
.
Operators 4.7: x ^ x
: XOR of any number with itself is 0
.
Operators 4.8: x <<= 3
: Left shift multiplies x
by 2^3
, resulting in 8
.
Operators 4.9: y <<= 3
: Left shift multiplies y
by 2^3
, resulting in -8
.
Operators 4.10: y >>= 3
: Arithmetic right shift divides y
by 2^3
, preserving the sign bit, resulting in -1
.
Operators in C
Operators 5: Conditional Operators
Program
#define PRINT(int) printf("int = %d\n", int)
main()
{
int x = 1, y = 1, z = 1;
x += y += z;
PRINT(x < y ? y : x); // Operators 5.1
PRINT(x < y ? x++ : y++);
PRINT(x);
PRINT(y); // Operators 5.2
PRINT(z += x < y ? x++ : y++); PRINT(y); PRINT(z); // Operators 5.3 x = 3; y = z = 4; PRINT((z >= y >= x) ? 1 : 0); // Operators 5.4
PRINT(z >= y && y >= x); // Operators 5.5
}
Output
- Operators 5.1: 3
- Operators 5.2: 2, followed by x = 3, y = 3
- Operators 5.3: 4, followed by y = 4, z = 4
- Operators 5.4: 0
- Operators 5.5: 1
Explanation
Operators 5.1: x += y += z
: Compound assignments evaluate right-to-left. Result: x = 3
. The conditional expression x < y ? y : x
evaluates to 3
.
Operators 5.2: Conditional expression x < y ? x++ : y++
evaluates to 2
. Post-increment updates x = 3
, y = 3
.
Operators 5.3: Compound assignment z += x < y ? x++ : y++
adds 4
to z
. Updated values: y = 4
, z = 4
.
Operators 5.4: Relational operators (z >= y >= x)
evaluate left-to-right. Result: 0
.
Operators 5.5: Logical AND z >= y && y >= x
evaluates to 1
.
Operators in C
Operators 6: Logical AND/OR
Program
#define PRINT3(x, y, z) printf("x=%d\ty=%d\tz=%d\n", x, y, z)
main()
{
int x, y, z;
x = y = z = 1;
++x || ++y && ++z;
PRINT3(x, y, z); // Operators 6.1
x = y = z = 1;
++x && ++y || ++z;
PRINT3(x, y, z); // Operators 6.2
x = y = z = 1;
++x && ++y && ++z;
PRINT3(x, y, z); // Operators 6.3
x = y = z = -1;
++x && ++y || ++z;
PRINT3(x, y, z); // Operators 6.4
x = y = z = -1;
++x || ++y && ++z;
PRINT3(x, y, z); // Operators 6.5
x = y = z = -1;
++x && ++y && ++z;
PRINT3(x, y, z); // Operators 6.6
}
Output
- Operators 6.1: x = 2, y = 1, z = 1
- Operators 6.2: x = 2, y = 2, z = 1
- Operators 6.3: x = 2, y = 2, z = 2
- Operators 6.4: x = 0, y = -1, z = 0
- Operators 6.5: x = 0, y = 0, z = -1
- Operators 6.6: x = 0, y = -1, z = -1
Explanation
Operators 6.1: ++x || ++y && ++z
: ++x
is true, so ||
short-circuits and skips ++y && ++z
.
Operators 6.2: ++x && ++y || ++z
: ++x && ++y
is true, so || ++z
is skipped.
Operators 6.3: ++x && ++y && ++z
: All increments are evaluated as true.
Operators 6.4: ++x && ++y || ++z
: ++x
is false, so || ++z
evaluates.
Operators 6.5: ++x || ++y && ++z
: ++x
is false, and ++y && ++z
is skipped.
Operators 6.6: ++x && ++y && ++z
: All increments are evaluated, but values remain unchanged due to propagation of false.