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 *), and member selection operators (. and ->). Pointer operators will be discussed while introducing pointers and member selection operators will be discussed with structures and union.
We will discuss comma operator and size of operator in this section.
Comma Operator
This operator is used to link the related expressions together.
e.g.: int x, y, z;
z = (x = 10, y=20, x+y) ;
Here, the first statement will create three integer type variables - x, y and z. In the second statement, right-hand side will be evaluated first. Consequently, 10 will be stored in variable x, then 20 will be stored in variable y~, and then values in x and y will be multiplied result of which will be stored in variable z. Thus, the value stored in the variable z will be 200 at the end of execution.
Sizeof Operator
The size of operator works on variables, constants and even on data types. It returns the number of bytes the operand occupies in the memory. Consider the following C program for illustration.
#include<stdio.h>
main()
{
printif("%d, %d", sizeof (int) , sizeof (float));
}
The output of this program will be 2, 4. Don't get disheartened if you get different result. This is only because the machine on which this program was run allotted 2 bytes for int type and 4 bytes for float type. The result that you get depends on the number of bytes allocated to these types on your machine. Nevertheless in all cases sizeof operator returns the number of bytes occupied by its operand on that particular machine.
Operators Precedence
An expression may contain more than one operator. Which operator will execute first depends on its precedence. Precedence defines the sequence in which operators are to be applied on the operands, while evaluating the expressions involving more than one operators.
Operators of same precedence are evaluated from left to right or right to left, depending upon the level. This is known as associativity property of an operator. A complete list of operator precedence as applicable in C language is presented below. Some of the operators you are already familiar with, others will be covered elsewhere in the book.
Summary of Precedence and AssociativityDESCRIPTION OPERATORS ASSOCIATIVITY
Function expression () Left—>Right
Array expression [] Left—>Right
Structure operator —> Left—>Right
Structure operator Left—»Right
Unary Minus - Right-»Left
Increment/Decrement ++ - Right—>Left
One's complement ~ Right—>Left
. Negation ! Right—>Left
Address of & Rights-Left
Value at address * Right—>Left
Type cast (type) Right—>Left
Size in bytes sizeof Right->Left
Multiplication * Left—>Right
Division / Left->Right
Modulus % Left-»Right
Addition + Left—>Right
Subtraction - Left—>Right
Left shift < < Left->Right
Right shift > > Left—>Right
Less than < Left—>Right
Less than or equal-to < = Left—»Right
Greater than > . Left->Right
Greater than or equal to >= Left—>Right
Equal to = = Left-»Right
Not equal to ! = Left—»Right
Bitwise AND & Left~>Right
Bitwise XOR ^ Left->Right
Bitwise OR | Left->Right
Logical AND && Left—>Right
Logical OR | | Left—>Right
Conditional : ?: Right—>Left
Assignment = Right—»Left
* = / = % = Right—»Left
+ = - = & = Right—>Left
^ = | = Right->Left
<< = >> = Right->Left
Comma \ Right—»Left
Unary operators
Operators can be classified on the basis of number of operands they operate on. Under this classification, operators can be of the following type:
1. Unary operators
2. Binary Operators
3. Ternary operators
4. n-ary operators
Unary operators, as the name suggests, operate on a single operand. Binary operators take two while ternary operators, take three operands respectively. Although, higher order operators are possible, they are not available in language C.
You have already learn about them in the previous section. Here is a list of the unary operators available in C:
1. Minus operator , -
2. Increment operator ++
3. Decrement operator --
4. Negation operator !
5. sizeof operator sizeof
6. One’s complement operator ~
No comments:
Post a Comment