Special Operators


Special Operators

C supports some special operators of interest such as comma operator, size of operator, pointer operators (& and *) and member selection operators (. and ->). The size of and the comma operators are discussed here.

Comma Operator

The comma operator can be used to link related expressions together. A comma-linked list of expressions are evaluated left to right and value of right most expression is the value of the combined expression.
Eg:-
value = (p = 14, q = 4, p + q);
First assigns 14 to p and 4 to q and finally assigns 18 to value. Since comma has the lowest precedence in operators the parenthesis is necessary.
Some examples of comma operator are
for (n=1, m=10, n <=m; n++,m++)
While (c=getchar(), c != ‘10’)
t = x, x = y, y = t  à Exchanging values
Comma Operator will be used to declare different variables for a data type.
Eg:- int a,b,c;

sizeof Operator

The “sizeof” 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. The Returned value
Will be in integer Bytes.

Eg:-     int m,n,k,p,sum;
m = sizeof (sum);        // 2
n = sizeof (long int); 
  //4    
k = sizeof (235L);      
 //4
p = sizeof (100);        //2

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.

No comments:

Post a Comment