Operators in C

Operators in C

What are Operators in C?

  • Operators are essential components of programming languages that enable you to perform various operations on data.

Types of Operators in C

  • Arithmetic Operators
  • Unary Operators
  • Relational Operators
  • Logical Operators
  • Assignment Operators
  • Shorthand assignment Operators
  • Conditional Operators
  • Comma Operators

Arithmetic Operators

  • Arithmetic operators are used for mathematical calculations.
  • They include addition, subtraction, multiplication, division, and more.
1#include <stdio.h>
2
3int main() {
4    int result; // Declare the 'result' variable
5
6    // Addition
7    result = 5 + 3;
8    printf("Addition: %d\n", result); // Add format specifier %d
9
10    // Subtraction
11    result = 8 - 2;
12    printf("Subtraction: %d\n", result);
13
14    // Multiplication
15    result = 4 * 6;
16    printf("Multiplication: %d\n", result);
17
18    // Division
19    result = 10 / 2;
20    printf("Division: %d\n", result);
21
22    return 0;
23}
24

Unary Operators

  • Unary operators work on a single operand.
  • Common examples include increment and decrement.
1#include <stdio.h>
2
3int main() {
4    int x = 5;
5
6    // Increment
7    x += 1;  // This is equivalent to x = x + 1
8    printf("Increment: %d\n", x);
9
10    int y = 8;
11
12    // Decrement
13    y -= 1;  // This is equivalent to y = y - 1
14    printf("Decrement: %d\n", y);
15
16    return 0;
17}
18

Relational Operators

  • Relational operators are used to compare values.
  • They return True or False based on the comparison
1#include <stdio.h>
2
3int main() {
4    // Greater than
5    int result = 5 > 3;
6    printf("Greater than: %d\n", result);
7
8    // Less than
9    result = 4 < 2;
10    printf("Less than: %d\n", result);
11
12    // Equal to
13    result = 10 == 10;
14    printf("Equal to: %d\n", result);
15
16    // Not equal to
17    result = 7 != 7;
18    printf("Not equal to: %d\n", result);
19
20    return 0;
21}
22

Logical Operators

  • Logical operators perform logical operations on Boolean values.
1#include <stdio.h>
2
3int main() {
4    _Bool result;
5
6    // AND operator
7    result = 1 && 0; 
8    printf("AND: %d\n", result);
9
10    // OR operator
11    result = 1 || 0; 
12    printf("OR: %d\n", result);
13
14    // NOT operator
15    result = !1; 
16    printf("NOT: %d\n", result);
17
18    return 0;
19}
20

Assignment Operators

  • Assignment operators are used to assign values to variables.
1#include <stdio.h>
2
3int main() {
4    int x = 10; // Variable assignment
5
6    printf("Assignment: %d\n", x); // Printing the value of x
7
8    return 0;
9}
10
11

Shorthand Assignment Operators

  • Shorthand assignment operators combine an operation with assignment.
1#include <stdio.h>
2
3int main() {
4    int x = 5;
5
6    // Shorthand Addition
7    x += 3; // Equivalent to x = x + 3
8    printf("Shorthand Addition: %d\n", x);
9
10    int y = 4;
11
12    // Shorthand Multiplication
13    y *= 2; // Equivalent to y = y * 2
14    printf("Shorthand Multiplication: %d\n", y);
15
16    return 0;
17}
18

Conditional (Ternary) Operator

  • The conditional operator allows you to assign values based on a condition.
1#include <stdio.h>
2
3int main() {
4    int x = 7;
5    int y = 5;
6    int result = (x > y) ? x : y; // Conditional Operator
7
8    printf("Conditional Operator: %d\n", result);
9
10    return 0;
11}
12

Bitwise Operators

  • Bitwise operators perform operations at the binary level.
1#include <stdio.h>
2
3int main() {
4    int result;
5
6    // Bitwise AND
7    result = 5 & 3;
8    printf("Bitwise AND: %d\n", result);
9
10    // Bitwise OR
11    result = 5 | 3;
12    printf("Bitwise OR: %d\n", result);
13
14    return 0;
15}
16

Comma Operator

  • The comma operator is used to evaluate multiple expressions, returning the value of the last one.
1#include <stdio.h>
2
3int main() {
4    int x, y, z;
5
6    // Comma Operator
7    x = (5, 3, 8);
8    printf("Comma Operator: %d\n", z);
9
10    return 0;
11}
12