Post

Operators in C

Operators in C

1. Arithmetic Operators in C


Arithmetic operators are used to perform basic mathematical operations.

OperatorNameDescriptionExample
+AdditionAdds two valuesx + y
-SubtractionSubtracts one value from anotherx - y
*MultiplicationMultiplies two valuesx * y
/DivisionDivides one value by anotherx / y
%ModulusReturns the remainder of divisionx % y
++IncrementIncreases the value of a variable by 1++x (Pre) / x++ (Post)
--DecrementDecreases the value of a variable by 1--x (Pre) / x-- (Post)

Example Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <stdio.h>

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

    printf("Addition: %d\n", x + y);
    printf("Subtraction: %d\n", x - y);
    printf("Multiplication: %d\n", x * y);
    printf("Division: %d\n", x / y);  // Integer division
    printf("Modulus: %d\n", x % y);

    x++;  // Increment
    printf("Incremented x: %d\n", x);

    y--;  // Decrement
    printf("Decremented y: %d\n", y);

    return 0;
}

πŸ”Ή Output:

1
2
3
4
5
6
7
Addition: 13
Subtraction: 7
Multiplication: 30
Division: 3
Modulus: 1
Incremented x: 11
Decremented y: 2

2. Augmented Assignment Operators.

Assignment operators are used to assign values to variables.

| Operator | Example | Equivalent To | Description | | ————– | β€”β€”β€”β€”- | β€”β€”β€”β€”β€”β€”- | β€”β€”β€”β€”β€”β€”β€”β€”β€” | | = | x = 5 | x = 5 | Assigns value to variable | | += | x += 3 | x = x + 3 | Adds and assigns | | -= | x -= 3 | x = x - 3 | Subtracts and assigns | | *= | x *= 3 | x = x * 3 | Multiplies and assigns | | /= | x /= 3 | x = x / 3 | Divides and assigns | | %= | x %= 3 | x = x % 3 | Modulus and assigns | | &= | x &= 3 | x = x & 3 | Bitwise AND and assigns | | ` | = | x | = 3 | | ^= | x ^= 3 | x = x ^ 3 | Bitwise XOR and assigns | | Β»= | xΒ Β»= 3 | x = xΒ Β» 3 | Right shift and assigns | | Β«= | x Β«= 3 | x = x « 3` | Left shift and assigns |

Example Code

1
2
3
4
5
6
7
8
#include <stdio.h>

int main() {
    int x = 10;
    x += 5; // x = x + 5
    printf("Updated x: %d\n", x); // Output: 15
    return 0;
}

3. Comparison Operators

Comparison operators compare two values and return 1 (true) or 0 (false).

OperatorNameExampleDescription
==Equal tox == yReturns1 if values are equal
!=Not equalx != yReturns1 if values are not equal
>Greater thanx > yReturns1 if x is greater than y
<Less thanx < yReturns1 if x is less than y
>=Greater than or equal tox >= yReturns1 if x is greater than or equal to y
<=Less than or equal tox <= yReturns1 if x is less than or equal to y

Example Code

1
2
3
4
5
6
7
8
#include <stdio.h>

int main() {
    int x = 5, y = 3;
    printf("x > y: %d\n", x > y); // Output: 1 (true)
    printf("x < y: %d\n", x < y); // Output: 0 (false)
    return 0;
}

4. Logical Operators

Logical operators are used to combine multiple conditions.

OperatorNameExampleDescription
&&ANDx < 5 && x < 10Returns1 if both conditions are true
||ORx < 5 || x < 4Returns 1 if one of the statements is true
!NOT!(x < 5 && x < 10)Reverses the result (true β†’ false, false β†’ true)

Example Code

1
2
3
4
5
6
7
8
9
10
11
#include <stdio.h>

int main() {
    int x = 4, y = 6;

    printf("(x < 5 && y > 5): %d\n", (x < 5 && y > 5)); // Output: 1
    printf("(x > 5 || y > 5): %d\n", (x > 5 || y > 5)); // Output: 1
    printf("!(x == 4): %d\n", !(x == 4)); // Output: 0

    return 0;
}

Let me know if you want more examples! πŸš€

Would you like to see more examples or explanations? 😊

This post is licensed under CC BY 4.0 by the author.