Special Operators

                    The special Operators are mainly classified into three types.
These are    1) Ternary (or) Conditional Operators
                    2) comma Operator
                    3) sizeof Operator

1) Ternary (or) Conditional Operator:

                    A ternary Operator pair is  "?"  and  ":" is available in C Language. It is used to construct a Conditional Expression.
                    The general form of ternary operator is :
                    Exp1    ?    Exp2    :    Exp3;
                    In this operator first Exp1 is evaluated, if it is true then Exp2 is evaluated and its value becomes the value of the Expression, otherwise Exp3 is evaluated and its value becomes the value of the Expression. 

Eg:    int a=10, b=20, c;
          c = a<b ? a : b;

output:  c = 10

2) comma operator:

                    It can be used to link the related expressions together.
                    The general form of comma operator is :
                    Var = (Var-1 = Value, Var-2 = Value,........., Var-n = Value, Expr);
Eg:    int a, b, c;
          c = (a=10, b=20, a+b);
          Here first assigns the value of 10 to a, then assigns 20 to b  and finally assigns 30(a+b) to c.

3) sizeof operator:

                    It is used to get the memory size of specified variable or expression or data type.
syntax:
                    sizeof (variable / expression /  datatype);
Eg1:
        1. sizeof(int);     => 2
        2. sizeof(float);  => 4
Eg2:
        int a, b;
        sizeof(a);         =>     2 
        sizeof(b);         =>     2 
        sizeof(a+b);    =>     2 



No comments:

Post a Comment