[go: up one dir, main page]

0% found this document useful (0 votes)
38 views7 pages

Types of Operators

Uploaded by

gamboamarcjill
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views7 pages

Types of Operators

Uploaded by

gamboamarcjill
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Types of Operators

1. Arithmetic Operators: These operators perform basic mathematical operations.


▪ + (addition)
▪ - (subtraction)
▪ * (multiplication)
▪ / (division)
▪ % (modulus, remainder)

Arithmetic Operators in C programming are used to perform various mathematical


operations on numeric operands, such as integers and floating-point numbers. These
operators allow you to perform addition, subtraction, multiplication, division, and
modulus (remainder) operations.

Here are the primary arithmetic operators in C:

Addition +: Adds two operands together.


int sum = 5 + 3; // sum is assigned the value 8

Subtraction -: Subtracts the right operand from the left operand.


int difference = 10 - 4; // difference is assigned the value 6

Multiplication *: Multiplies two operands.


int product = 6 * 7; // product is assigned the value 42

Division /: Divides the left operand by the right operand. If both operands
are integers, the result is truncated to an integer.
int quotient = 20 / 4; // quotient is assigned the value 5

Modulus %: Computes the remainder when the left operand is divided by


the right operand.
int remainder = 15 % 7; // remainder is assigned the value 1

2. Relational Operators: These operators compare two values and return a Boolean
result.
▪ == (equal to)
▪ != (not equal to)
▪ < (less than)
▪ > (greater than)
▪ <= (less than or equal to)
▪ >= (greater than or equal to)

Relational operators in C programming are used to compare two values and determine
the relationship between them. These operators return a Boolean (true or false) result
based on the comparison. Relational operators are often used in conditional
statements and loops to make decisions in your programs.
Here are the primary relational operators in C:

Equal to ‘==’: Checks if two values are equal.


int a = 5, b = 5; if (a == b) {
// This condition is true because a is equal to b.
}

Not equal to ‘!=’: Checks if two values are not equal.


int x = 10, y = 7; if (x != y) {
// This condition is true because x is not equal to y.
}

Less than ‘<’: Checks if the left operand is less than the right operand.
int m = 3, n = 6; if (m < n) {
// This condition is true because m is less than n.
}

Greater than ‘>’: Checks if the left operand is greater than the right
operand.
int p = 12, q = 8; if (p > q) {
// This condition is true because p is greater than q.
}

Less than or equal to ‘<=’: Checks if the left operand is less than or equal
to the right operand.
int i = 5, j = 5; if (i <= j) {
// This condition is true because i is less than or equal to j.
}

Greater than or equal to ‘>=’: Checks if the left operand is greater than or
equal to the right operand.
int r = 9, s = 7; if (r >= s) {
// This condition is true because r is greater than or equal to s.
}

3. Logical Operators: These operators are used to perform logical operations on


Boolean values.
▪ && (logical AND)
▪ || (logical OR)
▪ ! (logical NOT)
Logical operators in C programming are used to perform logical operations on
Boolean values (true or false) or expressions. These operators are often used in
conditional statements and loops to control program flow and make decisions based
on multiple conditions. C provides three main logical operators: && (logical AND), ||
(logical OR), and ! (logical NOT).

Here's a brief explanation of each logical operator:

Logical AND ‘&&’


▪ The ‘&&’ operator returns true if both of its operands are true; otherwise, it
returns false.
▪ It is often used to combine two or more conditions in such a way that all
conditions must be true for the overall expression to be true.
if (condition1 && condition2) {

// This block of code executes if both condition1 and condition2 are true.

Logical OR ‘||’

▪ The ‘||’ operator returns true if at least one of its operands is true; it returns
false only if both operands are false.
▪ It is often used when you want to check if any one of several conditions is
true.
if (condition1 || condition2) {

// This block of code executes if either condition1 or condition2 (or both) is true.

Logical NOT ‘!’

▪ The ‘!’ operator is a unary operator that negates the value of its operand. If
the operand is true, ‘!’ makes it false, and if the operand is false, ‘!’ makes it
true.
▪ It is used to reverse the logical value of a condition.
if (!condition) {

// This block of code executes if the condition is false.

4. Assignment Operators: These operators are used to assign values to variables.


▪ = (assignment)
▪ += (addition assignment)
▪ -= (subtraction assignment)
▪ *= (multiplication assignment)
▪ /= (division assignment)
▪ %= (modulus assignment)
Assignment operators in C programming are used to assign values to variables. They
allow you to store a result or value in a variable which can be used in subsequent
calculations or operations. C provides various assignment operators, each with a
specific purpose.

Here are some common assignment operators:

Assignment Operator ‘=’


▪ The basic assignment operator ‘=’ is used to assign the value on the right side
of the operator to the variable on the left side.
int x = 10; // Assigns the value 10 to the variable x

Addition Assignment Operator ‘+=’

▪ The ‘+=’ operator is used to add the value on the right side to the variable on
the left side and then assign the result to the variable on the left.
int y = 5;
y += 3; // Equivalent to y = y + 3; y is now 8

Subtraction Assignment Operator ‘-=’

▪ The -= operator is used to subtract the value on the right side from the variable
on the left side and then assign the result to the variable on the left.
int z = 7;
z -= 2; // Equivalent to z = z - 2; z is now 5

Multiplication Assignment Operator ‘*=’

▪ The *= operator is used to multiply the variable on the left side by the value on
the right side and then assign the result to the variable on the left.
int a = 3;
a *= 4; // Equivalent to a = a * 4; a is now 12

Division Assignment Operator ‘/=’

▪ The /= operator is used to divide the variable on the left side by the value on
the right side and then assign the result to the variable on the left.
int b = 16;
b /= 4; // Equivalent to b = b / 4; b is now 4

Modulus Assignment Operator ‘%=’

▪ The %= operator is used to calculate the modulus (remainder) of the variable


on the left side divided by the value on the right side and then assign the
result to the variable on the left.
int c = 17;
c %= 5; // Equivalent to c = c % 5; c is now 2
5. Increment and Decrement Operators:
▪ ++ (increment)
▪ -- (decrement)

Increment and decrement operators in C programming are used to increase or


decrease the value of a variable by 1. These operators are often employed in loops
and other situations where you need to manipulate variables by a fixed amount. C
provides two main increment and decrement operators: ‘++’ (increment) and ‘--'
(decrement).

Increment Operator ‘++’

▪ The ‘++’ operator is used to increase the value of a variable by 1.


▪ It can be applied as a prefix (e.g., ‘++x’) or a postfix (e.g., ‘x++’) operation.
▪ When used as a prefix, the increment operation is performed before the current
value of the variable is used.
▪ When used as a postfix, the increment operation is performed after the current
value of the variable is used.

Examples:
int x = 5;
++x; // x is incremented to 6 (prefix)

int y = 10;
y++; // y is incremented to 11 (postfix)

Decrement Operator ‘--'

▪ The ‘--' operator is used to decrease the value of a variable by 1.


▪ Like the increment operator, it can be applied as a prefix (e.g., ‘--x’) or a postfix
(e.g., ‘x--') operation.
▪ Prefix decrement subtracts 1 before using the current value, while postfix
decrement subtracts 1 after using the current value.

Examples:
int a = 8;
--a; // a is decremented to 7 (prefix)

int b = 15;
b--; // b is decremented to 14 (postfix)

6. Bitwise Operators: These operators work at the bit level.


▪ & (bitwise AND)
▪ | (bitwise OR)
▪ ^ (bitwise XOR)
▪ ~ (bitwise NOT)
▪ << (left shift)
▪ >> (right shift)
Bitwise operators in C programming are used for manipulating individual bits of integer
data types, such as int and char. These operators work at the binary level, allowing you to
perform operations on the binary representations of numbers. Bitwise operators are
useful in tasks related to low-level programming, device control, and certain algorithms
that require fine-grained control over individual bits. C provides several bitwise operators:

Bitwise AND ‘&’

▪ Performs a bitwise AND operation on corresponding bits of two operands.


▪ If both bits are 1, the result is 1; otherwise, it's 0.
int a = 5; // binary: 0101
int b = 3; // binary: 0011
int result = a & b; // binary result: 0001 (decimal 1)

Bitwise OR ‘|’

▪ Performs a bitwise OR operation on corresponding bits of two operands.


▪ If at least one bit is 1, the result is 1.
int x = 6; // binary: 0110 int
y = 3; // binary: 0011
int result = x | y; // binary result: 0111 (decimal 7)

Bitwise XOR ‘^’

▪ Performs a bitwise XOR (exclusive OR) operation on corresponding bits of two


operands.
▪ If the bits are different (one is 1, and the other is 0), the result is 1; otherwise, it's 0.
int p = 10; // binary: 1010
int q = 5; // binary: 0101
int result = p ^ q; // binary result: 1111 (decimal 15)

Bitwise NOT ‘~’

▪ Performs a bitwise NOT operation on a single operand, inverting all its bits.
▪ If a bit is 1, it becomes 0, and if it's 0, it becomes 1.
int r = 7; // binary: 0111
int result = ~r; // binary result: 1000 (decimal -8 due to two

Left Shift ‘<<’

▪ Shifts the bits of the left operand to the left by a specified number of positions.
▪ Filling with zeros on the right side.
int num = 4; // binary: 0100
int shifted = num << 2; // binary result: 10000 (decimal 16)
Right Shift ‘>>’

▪ Shifts the bits of the left operand to the right by a specified number of positions.
▪ Depending on the data type, it may fill with zeros or sign-extend (copy the leftmost
bit).
int value = -8; // binary: 11111111111111111111111111111000 (assuming 32-bit)
int shifted = value >> 2; // binary result: 11111111111111111111111111111110 (decimal -2)

Operator Precedence

Precedence Operator
Highest Order Casting

++ and - -

! (not)

*, /, %

+, -

<, <=, >, >=

==, ! =

& (unconditional AND)

| (unconditional OR)

&& (AND)

Lowest Order | | (OR)

=, +=, -=, *=, /=, %=

You might also like