CBasics

C Operators

Learn C operators including arithmetic, relational, logical, assignment, increment/decrement, and conditional operators used to perform calculations and decision-making in C programs.

What are Operators in C?Link to this section

Operators are symbols that perform operations on variables and values. They are used to manipulate data, perform calculations, compare values, and control program logic. Without operators, C programs would not be able to process data or make decisions.

Types of Operators in C :Link to this section

C provides several categories of operators:
  • Arithmetic Operators
  • Logical Operators
  • Relational Operators
  • Assignment Operators
  • Increment and Decrement Operators
  • Conditional (Ternary) Operator

Arithmetic Operators :Link to this section

Arithmetic operators are used to perform basic mathematical operations.

Common Arithmetic Operators

- `+` Addition - `-` Subtraction - `*` Multiplication - `/` Division - `%` Modulus

note

The modulus operator % works only with integers.

Relational Operators :Link to this section

Relational operators are used to compare two values and return a boolean result (true or false, represented as 1 or 0 in C).

Common Relational Operators

- `==` Equal to - `!=` Not equal to - `>` Greater than - `<` Less than - `>=` Greater than or equal to - `<=` Less than or equal to

Logical Operators :Link to this section

Logical operators are used to combine multiple conditions.

Logical Operators

- `&&` Logical AND - `||` Logical OR - `!` Logical NOT
Explanation: Logical operators return 1 for true and 0 for false.

Assignment Operators :Link to this section

Assignment operators are used to assign values to variables.

Common Assignment Operators

- `=` Assign - `+=` Add and assign - `-=` Subtract and assign - `*=` Multiply and assign - `/=` Divide and assign

Increment and Decrement Operators :Link to this section

These operators increase or decrease a variable’s value by one.

Operators

- `++` Increment - `--` Decrement

note

Increment and decrement can be used in prefix or postfix form.

Conditional (Ternary) Operator :Link to this section

The conditional operator is a short form of if-else. Syntax explanation: condition ? expression1 : expression2

tip

Use the ternary operator for simple conditions to keep code concise.

Operator Precedence :Link to this section

Operator precedence determines which operator is evaluated first in an expression. Example explanation: Multiplication and division have higher precedence than addition and subtraction.

tip

Use parentheses () to improve clarity and avoid confusion.

Why Operators are Important :Link to this section

Operators are essential for:
  • Performing calculations
  • Making decisions
  • Controlling program flow
  • Writing efficient logic

warning

Incorrect operator usage can lead to logical errors that are hard to detect.
Check Your Understanding
Question 1 of 5

Which operator is used for modulus operation?

Practice Challenges

medium
  • Write a program to perform all arithmetic operations on two numbers.
  • Check whether a number is even or odd using the modulus operator.
  • Use relational and logical operators to check voting eligibility.
  • Rewrite an if-else condition using the ternary operator.
  • Predict the output of expressions using different operators.