Special operators



C language also provides number of special operators which have no counter parts in other languages. These operators include

  • Comma operator(,)
  • Size of operator
  • Pointer operators (& and *)
  • Member selection operators (. and ->)

The comma operator can be used to link related expressions together. A comma-linked list of expressions is evaluated left to right and value of right most expression is the value of the combined expression. For example the statement

Value = (x = 10, y = 5, x + y);

First it assigns 10 to x and 5 to y and x+y will be 15.Finally 15 gets assigned to value.

The operator size of gives the size of the data type or variable in terms of bytes occupied in the memory. The operand may be a variable, a constant or a data type qualifier.
Example :-
m = sizeof (sum);
n = sizeof (long int);
k = sizeof (235L);

The size of 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 the execution of the program.

Increment and decrement operator

The increment and decrement operators are one of the unary operators since they operate on only one operand. These operators are very useful in C language. They are extensively used in for and while loops.

The syntax of the operators is given below

  • ++ variable name
  • variable name++
  • – –variable name
  • variable name– –
  • The increment operator ++ adds the value 1 to the current value of operand.
  • The decrement operator – – subtracts the value 1 from the current value of operand.
  • ++variable name and variable name++ mean the same thing when they form statements independently
  • They behave differently when they are used in expression on the right hand side of an assignment statement.
Consider the following

m = 5;
y = ++m; (prefix)
In this case the value of y and m would be 6 Suppose if we rewrite the above statement as
m = 5;
y = m++; (post fix)
Then the value of y will be 5 and that of m will be 6.

  • A prefix operator first adds 1 to the operand and then the result is assigned to the variable on the left.
  • On the other hand, a postfix operator first assigns the value to the variable on the left and then increments the operand.