-->

Facebook

Jasper Roberts - Blog

Thursday, February 26, 2015

Operators in C Programming Language

- C Language supports rich set of operators.
- Operators are used in doing various operations.
- The Operators can be divided into different categories.

- These Operators are:
1. Arithmetic Operators.
2. Relational Operators.
3. Logical Operators.
4. Assignment Operator.
5. Conditional Operators.
6. Increment and Decrements Operators.
7. Bit wise Operator.
8. Special Operators.

1. Arithmetic Operators.
         - In C programming, Arithmetic Operators are used in performing mathematical expressions.
         - The c language does not provide the operator for exponentiation.  
         - C language supports following Arithmetic Operators:
            Operator Name                    Meaning
                   +                                  Add
                   -                                   Subtract
                   *                                   Multiply
                   /                                    Divide
                   %                                  Modulo Division.
          - The operator + and - can be used as unary operator also.
          - Except % operator all Arithmetic Operators can be used with any type of numeric operands, while
             % operator can only be used with integer data type only.


       
2. Relational Operators.
         - Relational Operators can be used in an expression.
         - Relational Operators are used to compare variables or constants.
         - In C programming, Relational Operators are used to compare whether some variable value is higher,             equal or lower with other variables.
         - The Relational Operators will have value either True(1) or False(0).
         - C language provides following Relational Operators:
            Operator Name                    Meaning
                   = =                                 Equals
                   ! =                                  Not Equals
                   <                                     Less Than
                   >                                     Greater Than
                   <=                                   Less Than or Equal to
                   >=                                   Greater Than or Equal to
       
3. Logical Operators.
         - Sometimes in c programming, we need to take certain action depending upon combination of certain
           conditions is true or false.
        - Logical Operators are used to combine more than one condition and based on the outcome, certain              actions are taken.
        - C language supports following Logical Operators.
            Operator Name                    Meaning
                   &&                               Logical AND
                    | |                                  Logical OR
                    !                                   Logical NOT
        - Logical NOT is a unary operator.
        - In an expression, we can use more than one logical operator.
        - If more than one operator is used, ! (NOT) is evaluated first, then && and then | | .
        - We can use parentheses to change the order of evaluation.

4. Assignment Operator
        - C language supports ' = ' Assignment Operator.
        - Assignment Operator is used to assign a value to a variable.
        - Syntax:
                      variable_name = expression;
       - The expression can be a constant, variable name or any valid expression.
       - In an expression involving arithmetic operators, assignment operator has the least precedence i.e. +, -,           *, / , % will be given more priority.
       - C language also supports the use of shorthand notation.
       - For example, the statement a = a+5 can be written using shorthand notation as a += 5.

5. Conditional Operators.
       - In C programming language, Conditional Operators [ ?: ] :
       - They are also called as Ternary Operator .
       - They also called as ?: operator.
       - Ternary Operators takes on 3 Arguments
       - Syntax :
                     expression 1 ? expression 2 : expression 3
                     Where,
                                expression1 is Condition,
                                expression2 is Statement Followed if Condition is True,
                                expression3 is Statement Followed if Condition is False.

6. Increment and Decrements Operators.
      - Increment and Decrements Operators are special operators provided in c language for Increment and           Decrements a variable value.
      - Sometimes we need to increase or decrease a variable by one. We can use that using assignment                 operator ( = ).
      - C language provides special operators  + + (increment operator) and  - - (decrements operator).
      - Example: a + + , b - -

7. Bit wise Operator.
    - In Computer, we know that internally representation of data in bits 0 or 1.
    - C programming language supports some operators which can perform at the bit level.
    - These type of operations are normally done in assembly or machine level programming.
    - C language supports bit level manipulation also, that's why c language is also known as middle level                programming language..
           Operator Name                    Meaning
                   &                                 Bit-wise AND
                    |                                  Bit-wise OR
                   ^                                  Bit-wise Exclusive OR(XOR)
                  <<                                 Left Shift
                  >>                                 Right Shift
                  ~                                    Bit-wise 1's complement.

8. Special Operators.
      - sizeof operator:
      - the size of operator is used to find out the number of bytes required to store a variable in memory.
      - Syntax:
                     int sizeof(variable name or datatype);
      - Example:
                     int ans;
                     n = sizeof(ans);

 

No comments:

Post a Comment