Special Operators in C Language

Special Operators :

C Supprots the following three types of special operators.

  • Comma Operator
  • SizeOf Operator
  • Member Selection Operator

comma is a binary operator whereas sizeof is a unary operator.
comma operators generally used in loops.
A sizeof operator is normally used to determine the lengths of arrays and structures.

Comma Operator :

The comma operator (,) can be used to link the related expressions together. A comma-linked list of expressions are evaluated left to right and the value of right most expression is the value of the combined expression.

Syntax : exp m = (exp 1, exp 2, .... exp n)

The comma operator has the lowest priority among all the operators available in C.
Comma operator follows right to left associativity.
Comma operator is a binary operator.
Example : value = (x = 10, y = 5, x + y );

First assigns the value 10 to x, then assigns 5 to y, and finally assigns 15(i.e, 10 + 5) to value. Since comma operator has the lowest precedence of all operators, the pair of parentheses are necessary.

Applications :
   In for loops : for(n = 1, m = 10, n <= m; n++, m++)
   In while loops : while( c = getchar(), != '10')
   Exchanging values : t = x, x = y, y = t;

SizeOf Operator :

The SizeOf is a compile time operator and when used with an operand, it returns the number of bytes that the operand occupies. The operand may be a variable, a contant or a data type qualifier.

Syntax : sizeOf(opeand)

Examples :

1. y = sizeOf(int); Here value of y will be 2 bytes.
2. z = sizeOf(a); Here value of z depends on data type of a

A sizeOf operator is normally used to determine the lengths of arrays and structures, when their sizes are not known to the programmer. It is also used to allocate memory space dynamically to variables during execution of a program.

Member Selection Operator :

These operators are used to access members of structures and unions. They are represented by dot(.) and arrow().

Examples :

1. var.member1; /* when var is a structure variable */
2. var member2; /* when var is a pointer to structure variable */

Post a Comment