Course Content
C Tutorial
About Lesson

Operators (+, -, *, /, %, etc.)

Programming languages utilize operators to perform various computations and operations. In C programming, several fundamental operators play a pivotal role in manipulating values and variables.

Arithmetic Operators (+, -, *, /, %)

Arithmetic operators in C perform basic mathematical operations:

  • Addition (+): Adds two operands.
  • Subtraction (-): Subtracts the right operand from the left operand.
  • Multiplication (*): Multiplies two operands.
  • Division (/): Divides the left operand by the right operand.
  • Modulus (%): Computes the remainder of the division of two operands.

Example Usage of Arithmetic Operators

#include <stdio.h>

int main() {
int a = 10, b = 4;
int sum = a + b;
int difference = a - b;
int product = a * b;
int quotient = a / b;
int remainder = a % b;

printf("Sum: %dn", sum);
printf("Difference: %dn", difference);
printf("Product: %dn", product);
printf("Quotient: %dn", quotient);
printf("Remainder: %dn", remainder);

return 0;
}

Assignment Operators (=, +=, -=, *=, /=, %=)

Assignment operators are used to assign values to variables:

  • Assignment (=): Assigns the value of the right operand to the left operand.
  • *Compound Assignment (+=, -=, =, /=, %=): Performs arithmetic operation on the variable and then assigns the result to the variable.

Example Usage of Assignment Operators

#include <stdio.h>

int main() {
int x = 10;
int y = 5;

x += y; // Equivalent to x = x + y;
printf("x: %dn", x);

y *= 2; // Equivalent to y = y * 2;
printf("y: %dn", y);

return 0;
}

Comparison Operators (==, !=, >, <, >=, <=)

Comparison operators evaluate the relationship between two operands:

  • Equal to (==): Checks if two operands are equal.
  • Not equal to (!=): Checks if two operands are not equal.
  • Greater than (>): Checks if the left operand is greater than the right operand.
  • Less than (<): Checks if the left operand is less than the right operand.
  • Greater than or equal to (>=): Checks if the left operand is greater than or equal to the right operand.
  • Less than or equal to (<=): Checks if the left operand is less than or equal to the right operand.

Example Usage of Comparison Operators

#include <stdio.h>

int main() {
int p = 10, q = 20;

if (p == q) {
printf("p is equal to qn");
} else {
printf("p is not equal to qn");
}

if (p < q) {
printf("p is less than qn");
}

return 0;
}

Logical Operators (&&, ||, !)

Logical operators perform logical operations on operands:

  • Logical AND (&&): Returns true if both operands are true.
  • Logical OR (||): Returns true if at least one operand is true.
  • Logical NOT (!): Returns the opposite of the operand’s logical state.

Example Usage of Logical Operators

#include <stdio.h>

int main() {
int x = 5, y = 10;

if (x > 0 && y > 0) {
printf("Both x and y are positiven");
}

if (x > 0 || y > 0) {
printf("At least one of x or y is positiven");
}

if (!(x > 10)) {
printf("x is not greater than 10n");
}

return 0;
}