Pages

Thursday 21 July 2011

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 *), 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.
Size of 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 Associativity

DESCRIPTION  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
Right shift

c programmming online,basic of c language,structure of c programming,c language statements,Compiling a C Program,Pseudo Codes in c,basis data type in c ,variables in C Program,constants in C Program,c operators in C Program,special operators in C ,Decision Making,Branching & Looping in C Program,if statement in C Program,for loop in C Program

No comments:

Post a Comment