Special Operators and Operator Precedence in C
In the last posts we've discussed about C's all 7 type of operators, now we'll see which all are the Special operators in C.
| Special Operators | ||
|---|---|---|
| Operator | Description | Example |
| , | Used to separate a pair of expressions. In this example two initialization expressions are separated using a comma operator. | for(i=1,j=n;i |
| . | Dot (.) operator is used in C to refer structure members. | struct student s; s.id=1021; s.mark=86; |
| sizeof( ) | Size of operator is used to compute the size of any variable or any type. It will return the size in bytes. | sizeof(int); char grade; sizeof(grade); |
| -> | Arrow operator is used to access structure members using a structure pointer variable. | struct student p*; p=&s; p->id; |
Notes
- Comma operator is used to separate a pair of instructions, its commonly used in for loop to initialize or update more than one variable. For example : for(i=0,j=n;i
- If we defined a user defined structure data type, then its members are accessed its variable by a dot (.) operator.
- sizeof( ) operator is a special operator, which will take a variable or a type as argument and will return the size of its argument in bytes. For example, sizeof(int) will return 2. char ch; sizeof(ch) will return 1.
- -> operator is used with structure pointers to access the structure members.
Operator Precedence and Associativity
When a combination of operators are used in an expression, then that mixed expression will be evaluated in the order of precedence of the operator used in that operation. BODMAS theoram is applicable to the precedence for arithmetic operators. The following is the precedence of the C operators :
1. ( )
2. ++, --, !, sizeof( )
3. /, %, *
4. +, -
5. <, <=, >, >=
6. !=, ==
7. &&
8. ||
9. ? :
10. All assignment and shorthand operators.
11. ,
If more than one same operator used in an expression then it will be evaluated in the order of its associativity rule. !, ++, --, ? :, =, +=, -=, *=, /= are evaluated from Right to left and rest of all operators discussed above are evaluated from Left to right.



0 comments:
Post a Comment