Operators and Expressions in C

In C programming, operators are symbols that instruct the compiler to perform specific mathematical, relational, logical, or bit-level operations. An expression is a combination of variables, constants, and operators that is evaluated to produce a value.

Arithmetic Operators

Arithmetic operators are used to perform common mathematical operations. These include:

  • Addition: Combines two values.
  • Subtraction: Finds the difference between two values.
  • Multiplication: Produces the product of two values.
  • Division: Divides one value by another and returns the quotient.
  • Modulus: Returns the remainder after division of one value by another (used with integers).
Operator Description
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulus (Remainder)

These operators work on numeric types like integers and floating-point numbers.

Relational Operators

Relational operators are used to compare two values or expressions. The result of a relational operation is either true or false (non-zero or zero in C).

The key relational operations are:

  • Equal to: Checks if two values are the same.
  • Not equal to: Checks if two values differ.
  • Greater than: Determines if one value is larger than another.
  • Less than: Determines if one value is smaller than another.
  • Greater than or equal to: Checks if one value is greater than or equal to another.
  • Less than or equal to: Checks if one value is less than or equal to another.
Operator Description
== Equal to
!= Not equal to
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to

Relational operators are typically used in conditional statements and loops.

Logical Operators

Logical operators are used to combine or invert boolean expressions. These are essential in decision-making structures.

They include:

  • Logical AND: Returns true if both expressions are true.
  • Logical OR: Returns true if at least one expression is true.
  • Logical NOT: Inverts the value of a boolean expression.
Operator Description
&& Logical AND
` ` Logical OR
! Logical NOT

Logical operators help in forming complex conditions using simpler relational expressions.

Assignment Operators

Assignment operators are used to assign values to variables. The basic assignment operator assigns the value on the right-hand side to the variable on the left-hand side.

In addition, compound assignment operators perform an operation and assignment in one step. Examples include:

  • Add and assign
  • Subtract and assign
  • Multiply and assign
  • Divide and assign
  • Modulus and assign
Operator Description
= Assignment
+= Add and assign
-= Subtract and assign
*= Multiply and assign
/= Divide and assign
%= Modulus and assign

These operators reduce redundancy and make code more concise.

Bitwise Operators

Bitwise operators perform operations on the binary representations of integers. They are powerful for low-level programming tasks and optimizing performance.

They include:

  • Bitwise AND: Sets each bit to 1 if both bits are 1.
  • Bitwise OR: Sets each bit to 1 if one of the bits is 1.
  • Bitwise XOR: Sets each bit to 1 if the bits are different.
  • Bitwise NOT: Inverts all bits.
  • Left shift: Shifts bits to the left, inserting zeros from the right.
  • Right shift: Shifts bits to the right, discarding bits on the right.
Operator Description
& Bitwise AND
` ` Bitwise OR
^ Bitwise XOR (Exclusive OR)
~ Bitwise NOT
<< Left shift
>> Right shift

Bitwise operations are commonly used in embedded systems, graphics, and hardware interfacing.

Increment and Decrement Operators

These operators are shorthand for increasing or decreasing a variable's value by one.

  • Increment increases a variable's value by one.
  • Decrement decreases a variable's value by one.
Operator Description
++ Increment by 1
-- Decrement by 1

These operators can be used in prefix or postfix forms. The difference lies in the order of operation with respect to other expressions.

Operator Precedence and Associativity

When multiple operators are used in an expression, precedence determines the order in which operations are performed. Associativity resolves the order when two operators of the same precedence appear in an expression.

For example:

  • Multiplication, division, and modulus have higher precedence than addition and subtraction.
  • Assignment operators have right-to-left associativity.
  • Most arithmetic and relational operators have left-to-right associativity.
Category Operators Associativity
Postfix ++, -- Left to Right
Unary +, -, ++, --, !, ~ Right to Left
Multiplicative *, /, % Left to Right
Additive +, - Left to Right
Relational <, <=, >, >= Left to Right
Equality ==, != Left to Right
Logical AND && Left to Right
Logical OR ` ` Left to Right
Assignment =, +=, -=, *=, /=, %= Right to Left

Parentheses can be used to override default precedence and associativity to ensure expressions are evaluated as intended.

Type Casting

Type casting refers to converting a variable from one data type to another. This is important when performing operations between different data types (such as an integer and a float).

There are two types of type casting:

  • Implicit casting: Performed automatically by the compiler when it is safe to do so.
  • Explicit casting: Performed manually by the programmer to convert data from one type to another, especially when precision or memory control is required.
Type Cast Description
Implicit Casting Automatic conversion by compiler
Explicit Casting Manual conversion by programmer

Type casting helps avoid unexpected results in mixed-type expressions and ensures compatibility in calculations and function arguments.

Operators and expressions are the core of logic and computation in C programs. Mastering arithmetic, relational, logical, and bitwise operations, along with understanding operator precedence and type casting, equips you to build robust and efficient programs. Always use parentheses to clarify complex expressions and avoid ambiguity in operator evaluation.