Miscellaneous Operators

   C also offers us with many other type of operators such as ternary conditional,assignment,  compound assignment, sizeof, dot operator(.), arrow operator(->), comma operator(,) etc,,,,

  We shall discuss about these operators below:

1.Ternary Conditional:

                                            This operator helps us evaluate an expression based on a condition. The syntax of this operator is :

                                          condition? exp1 : exp2;

So now when the condition is true exp1 is evaluated and if it is false exp 2 will be evaluated. For example, if we write:

                                        a>b? printf (" a is greater than b") : printf ("b is greater than a");

Here if a is greater than b then the condition returns true so statement ' a is greater than b' is printed else 'b is greater than a' is printed.


A small challenge for you guys:
  •  Write a program that uses ternary operator to find largest among three numbers.

2.Assignment operator:

                                            This operator is used to assign a R-value to L-value but not the reverse. L-values are address (for example variables) and R-values are values or constant expressions. We have already used assignment operator a lot of times in our previous chapters.

For example, 
                      int a=5;

here 'a' is a L-value and 5 is a R-value.

                     The other part of this topic is compound assignment. This means evaluating the L-value in an expression and assigning it at the same time to the that L-value. The operators generally used are +=,-=,/=,*= etc..., For example,

                                       int a=5,b=6;
                                       a+=b;               // this is same as a=a+b;

     So a value becomes 11(5+6).


3.Sizeof operator:

                                      This operator as the name signifies is used to find the size of a unknown data types or known data types sometimes. As each data type allocates some memory depending on the compilers and system we need this operator some times. The syntax of this operator is :

                                                          sizeof(data_type);
For example:
                              int a=sizeof(int);

Now a gets initialized to the value of memory that is allocated to int in that compiler or PC.

4.Dot operator(.):

                                 This is used to access member of a structure using structure variable. Structures and Structure variables are discussed in the coming tutorials.

5.Arrow operator(->):

                                  This is used to access pointer members of a structure and in accessing members of pointer structure variables. Pointer and structures are discussed in coming tutorials.

6.Comma operator(,):

                                 This operator is used to separate expressions. The order of execution of expressions is from left to right. If any comma separated expressions are assigned to a variable then all the expressions are executed left to right and the value of last expression is assigned to the variable.For example,

                                         


#include <stdio.h>

int  main() 
{

     int a  =  5;
     int b  =  6;
     int c  =  (a++, ++b, b+5);
    
     printf (" A  =  %d, B  =  %d, C  =  %d", a, b, c);
    
     return 0;


}

Output:


Let us look what expression means,

  • a=5, b=6 , a++ is evaluated which results in a=6.
  • Now ++b is evaluated which results in b=7.
  • Then b+5 is evaluated which is 7+5 i.e 12.
  • According to the associativity of comma operator 12 will be assigned to C which is shown in the output.
Precedence and associativity of operators will be discussed  in the next tutorial.


Previous
Next Post »