C Operators: A Comprehensive Guide to Unary, Binary, and Ternary Operations

Explore the various operators in C programming, including unary, binary, and ternary operators. Understand their functions and applications in manipulating data and performing calculations in your C code.



C - Operators

An operator is a symbol that performs operations on operands. Operators can be unary (one operand), binary (two operands), or ternary (three operands).

Unary Operators

  • ++ (increment)
  • -- (decrement)
  • ! (NOT)
  • ~ (complement)
  • & (address of)
  • * (dereference)

Binary Operators

These include arithmetic, logical, and relational operators.

Ternary Operators

  • ? (conditional)

Arithmetic Operators

Used for basic math operations:

Operator Description Example
+ Adds two operands. A + B = 30
- Subtracts the second operand from the first. A - B = -10
* Multiplies two operands. A * B = 200
/ Divides numerator by denominator. B / A = 2
% Modulus operator; remainder of division. B % A = 0
++ Increments the value by one. A++ = 11
-- Decrements the value by one. A-- = 9

Relational Operators

Used to compare two operands:

Operator Description Example
== Checks if values are equal. (A == B) is false
!= Checks if values are not equal. (A != B) is true
> Checks if left operand is greater than right. (A > B) is false
< Checks if left operand is less than right. (A < B) is true
>= Checks if left operand is greater or equal to right. (A >= B) is false
<= Checks if left operand is less or equal to right. (A <= B) is true

Logical Operators

Used to combine boolean expressions:

Operator Description Example
&& Logical AND; true if both operands are non-zero. (A && B) is false
|| Logical OR; true if any operand is non-zero. (A || B) is true
! Logical NOT; reverses the logical state. !(A && B) is true

Bitwise Operators

Operate on bits and perform bit-level operations:

Operator Description Example
& Bitwise AND; copies bit if it exists in both operands. (A & B) = 12
| Bitwise OR; copies bit if it exists in either operand. (A | B) = 61
^ Bitwise XOR; copies bit if it is set in one operand but not both. (A ^ B) = 49
~ Bitwise NOT; flips the bits. (~A) = -61
<< Left Shift; shifts bits to the left. (A << 2) = 240
>> Right Shift; shifts bits to the right. (A >> 2) = 15

Assignment Operators

Used to assign values to variables:

Operator Description Example
= Assigns value from right to left. C = A + B
+= Add and assign. C += A
-= Subtract and assign. C -= A
*= Multiply and assign. C *= A
/= Divide and assign. C /= A
%= Modulus and assign. C %= A
<<= Left shift and assign. C <<= 2
>>= Right shift and assign. C >>= 2
&= Bitwise AND and assign. C &= 2
^= Bitwise XOR and assign. C ^= 2
|= Bitwise OR and assign. C |= 2

Miscellaneous Operators

Other important operators:

Operator Description Example
sizeof() Returns the size of a variable. sizeof(a)
& Returns the address of a variable. &a
* Pointer to a variable. *a
? : Conditional expression. Condition ? X : Y

Operators Precedence

Operators have different precedence levels which determine the order of evaluation in expressions:

Category Operator Associativity
Postfix () [] -> . ++ -- Left to right
Unary + - ! ~ ++ -- (type)* & sizeof Right 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

Other Operators

Includes increment/decrement operators (++ and --), address-of (&), dereference (*), and type cast (()) operators.