2. Operators and expressions

Operator

An operator is a symbol that tells the compiler to perform specific mathematical or logical functions. C language is rich in built-in operators and provides the following types of operators −

  • Arithmetic Operators
  • Relational Operators
  • Logical Operators
  • Unary Operators
  • Assignment Operators
  • Conditional Operators
  • Bitwise Operators

1) Arithmetic Operators: These are used to perform arithmetic/mathematical operations on operands. The binary operators falling in this category are:

    • Addition: The ‘+’ operator adds two operands. For example, a+b.
    • Subtraction: The ‘-‘ operator subtracts two operands. For example, a-b.
    • Multiplication: The ‘*’ operator multiplies two operands. For example, a*b.
    • Division: The ‘/’ operator divides the first operand by the second. For example, a/b.
    • Modulus: The ‘%’ operator returns the remainder when first operand is divided by the second. For example, a%b.
2) Relational Operators:-A relational operator checks the relationship between two operands. If the relation is true, it returns 1; if the relation is false, it returns value 0.
Relational operators are used in decision making and loops.
  • OperatorMeaning of Operator
    ==Equal to
    >Greater than
    <Less than
    !=Not equal to
    >=Greater than or equal to
    <=Less than or equal to

  • 3)Logical Operators:-An expression containing logical operator returns either 0 or 1 depending upon whether expression results true or false. Logical operators are commonly used in decision making in C programming.

    OperatorMeaning
    &&Logical AND. True only if all operands are true
    ||Logical OR. True only if either one operand is true
    !Logical NOT. True only if the operand is 0

4). Unary operators:-Unary operator are operators that act upon a single operand to produce a new value.

Types of unary operators:

    1. unary minus(-)
    2. increment(++)
    3. decrement(- -)
    4. NOT(!)
    5. Addressof operator(&)
    6. sizeof()
    5).Assignment operators:- Assignment operators are used to assigning value to a variable. The left side operand of the assignment operator is a variable and right side operand of the assignment operator is a value.
    Operators
    Example/Description
    =
    sum = 10;
    10 is assigned to variable sum
    +=
    sum += 10;
    This is same as sum = sum + 10
    -=
    sum -= 10;
    This is same as sum = sum – 10
    *=
    sum *= 10;
    This is same as sum = sum * 10
    /=
    sum /= 10;
    This is same as sum = sum / 10
    %=
    sum %= 10;
    This is same as sum = sum % 10
    &=
    sum&=10;
    This is same as sum = sum & 10
    ^=
    sum ^= 10;
    This is same as sum = sum ^ 10
    6). Conditional operator :-The conditional operator conditional operator is also known as a ternary operator. The conditional statements are the decision-making statements which depends upon the output of the expression. It is represented by two symbols, i.e., '?' and ':'.

    As conditional operator works on three operands, so it is also known as the ternary operator.

    The behavior of the conditional operator is similar to the 'if-else' statement as 'if-else' statement is also a decision-making statement.

    Syntax:-Expression1?expression2:expression3;


              Post a Comment

              0 Comments