Operators

Lesson 2.3: Operators


Operators are symbols that perform specific operations on values or variables. Think of them as the verbs in the C programming language.  Understanding operators is fundamental to manipulating data and controlling the flow of your programs. Let's dive into the key types:


1. Arithmetic Operators


Arithmetic operators are used for basic mathematical operations:


Operator Name Example Description

+ Addition 5 + 3 (result: 8) Adds two numbers together.

- Subtraction 10 - 4 (result: 6) Subtracts the second number from the first.

* Multiplication 6 * 2 (result: 12) Multiplies two numbers.

/ Division 20 / 5 (result: 4) Divides the first number by the second.

% Modulus 13 % 5 (result: 3) Returns the remainder after division.


2. Relational Operators


Relational operators compare two values and return a Boolean result (true or false):


Operator Name Example Description

== Equal to 5 == 5 (result: 1) Checks if two values are equal.

!= Not equal to 7 != 3 (result: 1) Checks if two values are not equal.

< Less than 2 < 8 (result: 1) Checks if the first value is less than the second.

> Greater than 10 > 6 (result: 1) Checks if the first value is greater than the second.

<= Less than or equal to 4 <= 4 (result: 1) Checks if the first value is less than or equal to the second.

>= Greater than or equal to 9 >= 5 (result: 1) Checks if the first value is greater than or equal to the         second.


Important Note: In C, 1 represents true, and 0 represents false.


3. Logical Operators


Logical operators combine or modify Boolean expressions:


Operator Name Example Description

&& AND true && false (result: 0) Returns true (1) only if both expressions are true, otherwise false (0).

 | OR         true

 ! NOT !true (result: 0) Reverses the logical value of an expression (true becomes false, and vice versa).


4. Bitwise Operators


Bitwise operators work on individual bits (0s and 1s) of integer data:


Operator Name Description

& Bitwise AND Performs a logical AND on each pair of corresponding bits.

|         Bitwise OR

^ Bitwise XOR Performs a logical XOR on each pair of corresponding bits.

~ Bitwise NOT (complement) Inverts all the bits of a number.

<< Left shift Shifts the bits of a number to the left by a specified number of positions.

>> Right shift Shifts the bits of a number to the right by a specified number of positions.



5. Assignment Operators


Assignment operators assign a value to a variable:


Operator Name Example Equivalent to

= Simple assignment x = 5; x = 5;

+= Add and assign x += 3; x = x + 3;

-= Subtract and assign x -= 2; x = x - 2;

*= Multiply and assign x *= 4; x = x * 4;

/= Divide and assign x /= 2; x = x / 2;

%= Modulus and assign x %= 3; x = x % 3;


Key Points


Precedence: Operators have a specific order of evaluation (e.g., * and / are performed before + and -).

Associativity: When operators have the same precedence, associativity determines the order (left-to-right or right-to-left).

Let me know if you'd like more examples or exercises for students!

Course Syllabus