C documentation V1 Help

Basic Operators

Operators in C are symbols that perform operations on variables and values. Here is an overview of basic operators in C:

1. Arithmetic Operators

Arithmetic operators perform mathematical operations on numerical values.

  • Addition (+): Adds two operands.

    int result = 5 + 3; // Result is 8
  • Subtraction (-): Subtracts the right operand from the left operand.

    int result = 10 - 4; // Result is 6
  • Multiplication (*): Multiplies two operands.

    int result = 3 * 7; // Result is 21
  • Division (/): Divides the left operand by the right operand.

    float result = 15.0 / 2.0; // Result is 7.5
  • Modulus (%): Returns the remainder when the left operand is divided by the right operand.

    int result = 10 % 3; // Result is 1

2. Assignment Operator

The assignment operator (=) is used to assign a value to a variable.

int num = 42; // Assigns the value 42 to the variable 'num'

3. Comparison Operators

Comparison operators compare two values and return a Boolean result (true or false).

  • Equal to (==): Checks if two values are equal.

    int a = 5, b = 5; if (a == b) { // Code executed if 'a' is equal to 'b' }
  • Not equal to (!=): Checks if two values are not equal.

    int x = 10, y = 20; if (x != y) { // Code executed if 'x' is not equal to 'y' }
  • 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.

4. Logical Operators

Logical operators perform logical operations and return a Boolean result.

  • 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.

int age = 25; if (age >= 18 && age <= 30) { // Code executed if age is between 18 and 30 }

Conclusion

These basic operators in C provide the foundation for performing various operations in your programs. As you progress, you'll encounter more operators and gain a deeper understanding of their usage.

In the upcoming sections, we'll explore more advanced topics in C programming. If you have specific questions or areas you'd like to delve into further, feel free to ask. Happy coding!

Last modified: 25 February 2024