C Programming • C Basics, Operators, Loops
C Programming / C Operators and precedence

C Operators and precedence

Notes 2 C Basics, Operators, Loops

An operator is a symbol used to perform an operation on one or more values or variables. Operators are an important part of expressions in C.

Notes

C Operators

An operator is a symbol used to perform an operation on one or more values or variables. Operators are an important part of expressions in C.

💡 In Simple Words

Operators tell the computer what operation to perform, such as add, subtract, compare, assign, or check a condition.

Example

int sum = a + b;

Here + is an operator used to add a and b, while = is the assignment operator.

Types of Operators in C

Type Operators Purpose
Arithmetic + - * / % Mathematical calculations
Relational == != > < >= <= Compare two values
Logical && || ! Combine or reverse conditions
Assignment = += -= *= /= %= Assign values to variables
Increment / Decrement ++ -- Increase or decrease a value by 1
Bitwise & | ^ ~ << >> Operate on individual bits
Conditional ?: Short form of a simple if-else expression

1. Arithmetic Operators

Arithmetic operators are used to perform mathematical calculations.

Operator Operation Example
+ Addition 10 + 5 = 15
- Subtraction 10 - 5 = 5
* Multiplication 10 * 5 = 50
/ Division 10 / 5 = 2
% Remainder 10 % 3 = 1

2. Relational Operators

Relational operators compare two values. The result is used as a logical condition.

Operator Meaning Example
== Equal to a == b
!= Not equal to a != b
> Greater than a > b
< Less than a < b
>= Greater than or equal to a >= b
<= Less than or equal to a <= b

3. Logical Operators

Logical operators are used to combine or reverse conditions.

Operator Meaning Example
&& Logical AND a > 5 && b < 10
|| Logical OR a > 5 || b < 10
! Logical NOT !(a > 5)

4. Assignment Operators

Assignment operators are used to assign or update values stored in variables.

Operator Example Meaning
= a = 10 Assign 10 to a
+= a += 5 a = a + 5
-= a -= 5 a = a - 5
*= a *= 5 a = a * 5
/= a /= 5 a = a / 5

5. Increment and Decrement Operators

The ++ operator increases a value by 1, while the -- operator decreases a value by 1.

int a = 5; a++; → a becomes 6 a--; → a becomes 5

Pre-Increment and Post-Increment

Form Meaning
++a Increase first, then use the value.
a++ Use the value first, then increase it.
--a Decrease first, then use the value.
a-- Use the value first, then decrease it.

6. Bitwise Operators

Bitwise operators work on the individual bits of integer values. They are mainly useful when low-level or bit-level operations are required.

Operator Name
& Bitwise AND
| Bitwise OR
^ Bitwise XOR
~ Bitwise NOT
<< Left Shift
>> Right Shift

7. Conditional Operator

The conditional operator ?: is a short form for selecting one of two expressions based on a condition.

condition ? expression1 : expression2
💡 Example

max = (a > b) ? a : b;

If a > b is true, a is assigned; otherwise b is assigned.

Operator Precedence

Operator precedence determines which operator is evaluated first when an expression contains multiple operators.

💡 Example

Consider: 2 + 3 * 4

Multiplication has higher precedence than addition, so: 3 * 4 = 12 is evaluated first.

Final result: 2 + 12 = 14

Basic Precedence Order

Priority Operators Example
1 — Highest () (a + b)
2 ++ -- ++a
3 * / % a * b
4 + - a + b
5 < <= > >= a < b
6 == != a == b
7 && a && b
8 || a || b
9 ?: a ? b : c
10 Assignment operators a = b

Associativity

When two operators have the same precedence, associativity determines the direction in which they are evaluated.

💡 Example

Multiplication and division have the same precedence and are generally evaluated from left to right.

20 / 5 * 2 → first 20 / 5 = 4 → then 4 * 2 = 8

Using Parentheses

Parentheses can be used to explicitly control the order of evaluation in an expression.

Expression Result
2 + 3 * 4 14
(2 + 3) * 4 20
🧠 Easy Rule to Remember

Parentheses → Increment/Decrement → Multiplication/Division → Addition/Subtraction → Relational → Logical → Conditional → Assignment

Quick Revision

Concept Remember
Arithmetic Performs mathematical calculations.
Relational Compares values.
Logical Combines conditions.
Assignment Assigns or updates values.
++ / -- Increase or decrease by 1.
Conditional Short form of simple if-else selection.
Precedence Determines which operator is evaluated first.
Associativity Determines evaluation direction for equal precedence.

Important Exam Questions

Short Answer Questions

  1. What is an operator in C?
  2. Write the different types of operators in C.
  3. What are arithmetic operators?
  4. What are relational operators?
  5. What are logical operators?
  6. What is the use of the assignment operator?
  7. What are increment and decrement operators?
  8. What is operator precedence?
  9. What is associativity?

Long Answer Questions

  1. Explain the different types of operators in C with suitable examples.
  2. Explain operator precedence and associativity with suitable examples.
  3. Evaluate suitable C expressions using the rules of operator precedence.
🎥 Recommended Learning

Watch a beginner-friendly explanation of C operators, precedence and associativity.

▶ Watch: C Operators & Precedence — Hindi

📝 Handwritten Notes

A short handwritten-style revision sheet for C operators and precedence will be provided here.

🧠 Mind Map

Use the mind map for quick revision of C operator categories and precedence.