Sunday, January 12, 2025
HomeComputer ScienceWhat is Operators in C

What is Operators in C

In C, operators are symbols used to perform operations on variables and values. They are a fundamental part of programming, enabling calculations, logical comparisons, and more.

Types of Operators in C

  1. Arithmetic Operators
    • Used to perform basic mathematical operations.
    • Operators:
      + (Addition), - (Subtraction), * (Multiplication), / (Division), % (Modulus).

    Example:

    c
    int a = 10, b = 3;
    printf("Sum: %d\n", a + b); // Output: 13
    printf("Remainder: %d\n", a % b); // Output: 1
  1. Relational Operators
    • Used to compare values and return a Boolean result.
    • Operators:
      == (Equal to), != (Not equal to), < (Less than), > (Greater than), <= (Less than or equal to), >= (Greater than or equal to).

    Example:

    c
    int a = 5, b = 10;
    printf("a < b: %d\n", a < b); // Output: 1 (true)
  1. Logical Operators
    • Used to perform logical operations on expressions.
    • Operators:
      && (Logical AND), || (Logical OR), ! (Logical NOT).

    Example:

    c
    int a = 1, b = 0;
    printf("a AND b: %d\n", a && b); // Output: 0 (false)
    printf("NOT a: %d\n", !a); // Output: 0 (false)
  1. Bitwise Operators
    • Used to perform operations at the bit level.
    • Operators:
      & (Bitwise AND), | (Bitwise OR), ^ (Bitwise XOR), ~ (Bitwise NOT), << (Left shift), >> (Right shift).

    Example:

    c
    int a = 5, b = 3; // Binary: a = 0101, b = 0011
    printf("a & b: %d\n", a & b); // Output: 1
    printf("a << 1: %d\n", a << 1); // Output: 10
  1. Assignment Operators
    • Used to assign values to variables.
    • Operators:
      = (Assign), +=, -=, *=, /=, %= (Compound assignment).

    Example:

    c
    int a = 10;
    a += 5; // a = a + 5
    printf("a: %d\n", a); // Output: 15
  1. Unary Operators
    • Operate on a single operand.
    • Operators:
      + (Positive), - (Negative), ++ (Increment), -- (Decrement), ! (Logical NOT).

    Example:

    c
    int a = 5;
    printf("Increment: %d\n", ++a); // Output: 6
  1. Ternary Operator
    • A shorthand for if-else.
    • Syntax:
      condition ? value_if_true : value_if_false

    Example:

    c
    int a = 10, b = 20;
    int max = (a > b) ? a : b;
    printf("Max: %d\n", max); // Output: 20
  1. Special Operators
    • Sizeof: Determines the size of a data type.
    • Comma: Combines multiple expressions, evaluates from left to right.
    • Pointer Operators: * (Dereference), & (Address of).

    Example:

    c
    int a = 10;
    printf("Size of a: %zu\n", sizeof(a)); // Output: 4 (depending on system)

Operator Precedence and Associativity

Operators have precedence that determines the order in which they are evaluated, and associativity determines the direction of evaluation (left-to-right or right-to-left).

See also  What Can Computer Science Do?

Summary Table of Operators

Category Operators Example
Arithmetic +, -, *, /, % a + b
Relational ==, !=, <, >, <=, >= a > b
Logical &&, `
Bitwise &, ` , ^, ~, <<, >>`
Assignment =, +=, -=, *=, /=, %= a += b
Unary +, -, ++, --, ! ++a
Ternary ? : a > b ? a : b
Special sizeof, ,, *, & sizeof(a)

Understanding operators is essential for building efficient and logical C programs.

RELATED ARTICLES
0 0 votes
Article Rating

Leave a Reply

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
- Advertisment -

Most Popular

Recent Comments

0
Would love your thoughts, please comment.x
()
x