Operators in C
Operators in C: In C programming, operators are special symbols or keywords used to perform operations on variables and values. They are categorized into several types based on their functionality. Here’s a breakdown of the different types of operators in C along with examples for each:
Arithmetic Operators in C
These operators are used to perform basic arithmetic operations.
Addition (+
): Adds two operands.
int a = 5, b = 3;
int result = a + b; // result is 8
Subtraction (-
): Subtracts the second operand from the first.
int result = a - b; // result is 2
Multiplication (*
): Multiplies two operands.
int result = a * b; // result is 15
Division (/
): Divides the first operand by the second.
int result = a / b; // result is 1
Modulus (%
): Returns the remainder of the division of the first operand by the second.
int result = a % b; // result is 2
Relational Operators in C
These operators are used to compare two values.
Equal to (==
): Checks if two operands are equal
if (a == b) {
// Executes if a equals b
}
Not equal to (!=
): Checks if two operands are not equal
if (a != b) {
// Executes if a does not equal b
}
Greater than (>
): Checks if the left operand is greater than the right.
if (a > b) {
// Executes if a is greater than b
}
Less than (<
): Checks if the left operand is less than the right.
if (a < b) {
// Executes if a is less than b
}
Greater than or equal to (>=
): Checks if the left operand is greater than or equal to the right
if (a >= b) {
// Executes if a is greater than or equal to b
}
Less than or equal to (<=
): Checks if the left operand is less than or equal to the right.
if (a <= b) {
// Executes if a is less than or equal to b
}
Logical Operators in C
These operators are used to combine conditional statements.
Logical AND (&&
): Returns true if both operands are true.
if (a > 0 && b > 0) {
// Executes if both a and b are greater than 0
}
Logical OR (||
): Returns true if at least one of the operands is true.
if (a > 0 || b > 0) {
// Executes if either a or b is greater than 0
}
Logical NOT (!
): Returns true if the operand is false.
if (!a) {
// Executes if a is false (0)
}
Bitwise Operators in C
These operators perform bit-level operations.
Bitwise AND (&
): Performs a bitwise AND operation.
int result = a & b; // result is 1 (0101 & 0011 = 0001)
Bitwise OR (|): Performs a bitwise OR operation
int result = a | b; // result is 7 (0101 | 0011 = 0111)
Bitwise XOR (^
): Performs a bitwise XOR operation.
int result = a ^ b; // result is 6 (0101 ^ 0011 = 0110)
Bitwise NOT (~
): Inverts all the bits of the operand
int result = ~a; // result is -6 (bitwise inversion of 0101 is 1010, which is -6 in two's complement form)
Left Shift (<<
): Shifts bits to the left
int result = a << 1; // result is 10 (0101 << 1 = 1010)
Right Shift (>>
): Shifts bits to the right
int result = a >> 1; // result is 2 (0101 >> 1 = 0010)
Assignment Operators in C
These operators are used to assign values to variables.
Assignment (=
): Assigns the right operand to the left operand
int c = 10; // c is assigned the value 10
Add and assign (+=
): Adds the right operand to the left operand and assigns the result to the left operand.
c += a; // c = c + a
Subtract and assign (-=
): Subtracts the right operand from the left operand and assigns the result to the left operand.
c -= a; // c = c - a
Multiply and assign (*=
): Multiplies the left operand by the right operand and assigns the result to the left operand.
c *= a; // c = c * a
Divide and assign (/=
): Divides the left operand by the right operand and assigns the result to the left operand.
c /= a; // c = c / a
Modulus and assign (%=
): Calculates the modulus of the left operand by the right operand and assigns the result to the left operand.
c %= a; // c = c % a
Increment and Decrement Operators in C
These operators are used to increase or decrease the value of a variable by 1.
Increment (++
): Increases the value of the operand by 1.
a++; // a is now 6
Decrement (--
): Decreases the value of the operand by 1.
a--; // a is now 5
Conditional (Ternary) Operator in C
This operator is used to evaluate a condition and return one of two values.
Conditional (? :
): Returns one of two values based on a condition.
int max = (a > b) ? a : b; // max is assigned the greater of a and b
Comma Operator in C
This operator allows you to separate multiple expressions, evaluating them from left to right.
Comma (,
): Separates expressions.
int x, y;
x = (a = 3, b = 4, a + b); // a is 3, b is 4, and x is 7
Sizeof Operator
This operator returns the size of a data type or variable.
Sizeof (sizeof
): Returns the size of a data type or variable
int size = sizeof(int); // size is 4 (depending on the system)
Pointer Operators
These operators are used with pointers to access memory addresses and values.
Address-of (&
): Returns the memory address of a variable
int *ptr = &a; // ptr now holds the address of a
Dereference (*
): Accesses the value at a given memory address
int value = *ptr; // value is the value stored at the address ptr points to (5)
Understanding these operators and how they function is crucial for effective programming in C. Each type of operator serves a specific purpose and allows you to perform various operations on data efficiently.