2.2) C Operators and expressions
Operators and expressions are crucial aspects of C programming, as they allow you to perform computations and manipulate data.
They form the building blocks for creating meaningful and dynamic programs.
Let’s explore into these concepts in detail.
Table of Contents
Operators:
Operators are symbols that represent operations to be performed on one or more operands. C provides a wide range of operators that can be categorized into different groups:
Arithmetic Operators:
+
(Addition): Adds two operands.-
(Subtraction): Subtracts the second operand from the first.*
(Multiplication): Multiplies two operands./
(Division): Divides the first operand by the second.%
(Modulus): Returns the remainder of the division.
Relational Operators:
==
(Equal to): Checks if two operands are equal.!=
(Not equal to): Checks if two operands are not equal.<
(Less than): Checks if the first operand is less than the second.>
(Greater than): Checks if the first operand is greater than the second.<=
(Less than or equal to): Checks if the first operand is less than or equal to the second.>=
(Greater than or equal to): Checks if the first operand is greater than or equal to the second.
Logical Operators:
&&
(Logical AND): Returns true if both operands are true.||
(Logical OR): Returns true if at least one operand is true.!
(Logical NOT): Returns true if the operand is false and vice versa.
Assignment Operators:
=
(Assignment): Assigns the value of the right operand to the left operand.+=
,-=
,*=
,/=
,%=
: Performs the specified operation and assigns the result to the left operand.
Increment and Decrement Operators:
++
(Increment): Increases the value of the operand by 1.--
(Decrement): Decreases the value of the operand by 1.
Bitwise Operators:
&
(Bitwise AND)|
(Bitwise OR)^
(Bitwise XOR)~
(Bitwise NOT)<<
(Left shift)>>
(Right shift)
Conditional (Ternary) Operator:
- condition ? expression1 : expression2: Evaluates the condition and returns either expression1 or expression2 based on the condition.
Expressions:
An expression is a combination of operators, operands, and constants that yields a result. Expressions can be used for calculations, comparisons, and various other operations. Here are some examples of expressions:
int x = 5, y = 10, z;
z = x + y; // Arithmetic expression
if (z > 15) { // Relational expression
printf("Z is greater than 15\n");
}
int a = (x > y) ? x : y; // Conditional expression
Expressions can be as simple as a single variable or as complex as a combination of various operators and operands.
Examples of Operators and Expressions in C
Here are examples demonstrating the use of various operators and expressions in C:
Arithmetic Operators:
#include <stdio.h>
int main() {
int a = 10, b = 5;
int sum = a + b;
int difference = a - b;
int product = a * b;
int quotient = a / b;
int remainder = a % b;
printf("Sum: %d\n", sum); // Output: Sum: 15
printf("Difference: %d\n", difference); // Output: Difference: 5
printf("Product: %d\n", product); // Output: Product: 50
printf("Quotient: %d\n", quotient); // Output: Quotient: 2
printf("Remainder: %d\n", remainder); // Output: Remainder: 0
return 0;
}
Relational and Logical Operators:
#include <stdio.h>
int main() {
int x = 5, y = 10;
if (x == y) {
printf("x is equal to y\n");
} else if (x != y) {
printf("x is not equal to y\n");
}
if (x > 0 && y > 0) {
printf("Both x and y are positive\n");
}
if (x > 0 || y > 0) {
printf("At least one of x or y is positive\n");
}
if (!(x > 0)) {
printf("x is not positive\n");
}
return 0;
}
Assignment and Increment/Decrement Operators:
#include <stdio.h>
int main() {
int a = 5, b = 10;
a += 2; // Equivalent to a = a + 2;
b *= 3; // Equivalent to b = b * 3;
printf("a: %d\n", a); // Output: a: 7
printf("b: %d\n", b); // Output: b: 30
int count = 0;
count++; // Increment by 1
count--; // Decrement by 1
printf("Count: %d\n", count); // Output: Count: 0
return 0;
}
Conditional (Ternary) Operator:
#include <stdio.h>
int main() {
int x = 5, y = 10;
int max = (x > y) ? x : y;
printf("Maximum: %d\n", max); // Output: Maximum: 10
return 0;
}
Bitwise Operators:
#include <stdio.h>
int main() {
int a = 5, b = 3;
int bitwiseAnd = a & b;
int bitwiseOr = a | b;
int bitwiseXor = a ^ b;
int bitwiseNot = ~a;
int leftShift = a << 2;
int rightShift = a >> 1;
printf("Bitwise AND: %d\n", bitwiseAnd); // Output: Bitwise AND: 1
printf("Bitwise OR: %d\n", bitwiseOr); // Output: Bitwise OR: 7
printf("Bitwise XOR: %d\n", bitwiseXor); // Output: Bitwise XOR: 6
printf("Bitwise NOT: %d\n", bitwiseNot); // Output: Bitwise NOT: -6
printf("Left Shift: %d\n", leftShift); // Output: Left Shift: 20
printf("Right Shift: %d\n", rightShift); // Output: Right Shift: 2
return 0;
}