COMPUTER TRAINING: Operators & Exressions

Monday, 24 December 2012

Operators & Exressions

Operators
Introduction
An operator specifies an operation to be performed. C is rich in operator. Operators join the various variables and constants to from an expression.
Some operator requires one operand and some require more than one operands.
An operator is a symbol that tells the computer to perform certain mathematical or logical manipulation in data stored in variables.
C is extremely rich in operators It has as many as 45 different operators.

Operators
Arithmetic operators
Arithmetic Operators are used to Arithmetical calculation.
There are five Arithmetic operators in C:
OperatorPurpose
+Addition
-Subtraction
*Multiplication
/Division
%Remainder after integer division

Operators
Relational operators
Relational operators are used to compare two operands and to check whether they are equal, unequal, greater than and lesser than other.
There are 6 relational operators:
OperatorMeaning
<Less than
>Greater than
<=Less than equal to
>=Greater than equal to
==Equal to
!=Not equal to
The value of the relational operator is either one or zero. If the relation is true, result is 1 otherwise it is 0.

Operators
Logical operators
Logical operators are used to combine two or more relational expressions.
There are three logical operators:
OperatorMeaning
&&Logical And
||Logical or
!Logical not
The expression which combines two or more relational expressions is termed as logical expression or compound relational expression.
The result of a logical expression is either one or zero.
Example:
a) if (age > 50 && weight < 80)
b) if (a < 0 || ch = = 'a')
c) if ( ! (a < 0))

Operators
Increment & Decrement operators
These types of operators operate on only one operand, therefore these operators are also called Unary operators.
These two powerful operators in C are + + (Increment), _ _ (Decrement). Operands must be declared as variables not a constant.
These operators may be used either after or before the operand.
When they are used before the operand, it is termed as Prefix while when they are used after the operand they are termed as Postfix.
In prefix operations the value of operator is incremented or decremented first and then the expression is evaluated. Prefix operators has the effect of Change then use.
In postfix operation the expression is evaluated first and then the value of operator is either incremented or decremented. Postfix operators has the effect of Use Then Change.
e.g.: b=a++; this is postfix increment expression. In the expression firstly b=1; thena=a+1; will be executed ,
while in prefix increment expression
b=--a;
firstly a =a-1;then b=a; will be executed.
An example program clarifies the Postfix and Prefix operators:
Output of the program:

Operators
Bitwise operators
The smallest element in memory on which we are able to operate as yield is a byte; and we operate on it by use of the data type char Bitwise operator is used for manipulation of data at bit level.
These operators are used for testing the bits, shifting them right to left. Bitwise operatormay not be applied to float or double data type.
This is a powerful feature of C to manipulate a bit. The programmer can access and manipulate individual bits within a piece of data.
Some of the bitwise operators in C are:
OperatorMeaning
&Bitwise Logical AND
|Bitwise Logical OR
^Bitwise Logical XOR
<<Left Shift
>>Right Shift
~Once Compliment

Operators
Conditional & ternary operators
The conditional operator? and: are sometimes called ternary operators.
ternary operator is one which contains three operands.
The general form of ternary operator is:
exp 1 ? exp 2 : exp 3
The operator works as, if exp 1 is evaluated first. If the result is true then exp 2 is executed, otherwise exp 3 is executed.
A program of ternary operator:
Output of the program:

Operators
The comma operators
This operator is used to link the related expression together the expression is separated by the, operator.
An example program showing uses of comma operator:
Output of the program:
Here firstly value 1 is assigned to a, followed by this 2 is assigned to b, and then the result of a+b is assigned to c.
The comma operator is often used in conjunction with a control statement called For.

Operators
Size of operator
The size of operator returns a number of bytes the operand occupies in memory. The operand may be a variable, a constant or a data type qualifier. It is a compile time operator.
An example of size of operator:
Output of size of operator:
The output of the above program is 2 because int occupies two byte in memory.
The size of operator is generally used to determine the length of entities called arrays and structures when their size is not known to the programmer.

Operators
Assignment operator
Assignment operators are used to assign the result of an expression to a variable. The most commonly used assignment operator is (=).
eg:      i=i+10;
      i=i+10 is an assignment expression which assigns the value of i+10 to i.
Expression like   i=i+10,  i=i-5,  i=i*2 etc. can be rewritten using shorthand assignment operators.
e.g.:   i=i+5 is equivalent to i+=5
            i=i*(y+1)  is equivalent to   i*=(y+1)
Operator Precedence:
While executing an arithmetic statement which has two or more operators, we may have some problems about how exactly does it get executed.
To answer these questions satisfactorily we have to understand the precedence of operators.
Precedence defines the sequence in which operators are to be applied on the operands. 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.
Summary of precedence of associativity is given below:
DescriptionOperatorAssociativity
Function Expression( )Left to Right
Array Expression[ ]Left to Right
Structure Operator->Left to Right
Structure Operator.Left to Right
DescriptionOperatorAssociativity
Unary minus-Right to Left
Increment/Decrement++/--Right to Left
One's Compliment~Right to Left
Negation!Right to Left
Address of&Right to Left
Value at address*Right to Left
Type cast(type)Right to Left
Size in bytessizeofRight to Left
DescriptionOperatorAssociativity
Multiplication*Left to Right
Division/Left to Right
Modulus%Left to Right
Addition+Left to Right
Subtraction-Left to Right
DescriptionOperatorAssociativity
Left Shift<<Left to Right
Right Shift>>Left to Right
DescriptionOperatorAssociativity
Less Than<Left to Right
Less Than Equal to<=Left to Right
Greater than>Left to Right
Greater than Equal to>=Left to Right
DescriptionOperatorAssociavity
Equal to==Left to Right
Not equal to!=Left to Right
DescriptionOperatorAssociavity
Bitwise AND&Left to Right
Bitwise XOR^Left to Right
DescriptionOperatorAssociavity
Bitwise OR^Left to Right
DescriptionOperatorAssociavity
Logical AND&&Left to Right
Logical OR||Left to Right
DescriptionOperatorAssociavity
Conditional?:Right to Left
DescriptionOperatorAssociavity
Assignment=Right to Left
Assignment*=   /=   %=Right to Left
Assignment+=   -=   &=Right to Left
Assignment^=   |=Right to Left
Assignment<<=   >>=Right to Left
DescriptionOperatorAssociavity
Comma,Right to Left

Operators
Type modifier
The basic data types may have modifier preceding them to indicate special properties of the object being declared.
These modifiers change the meaning of the basic data types to suit the specific needs.
These modifiers are unsigned, signed, long and short. It is also possible to give these modifiers in combination, e.g., unsigned long int.
eg:-
Modifier for char Data Type
main()
{
char ch=291;
printf("%d\t%c\n",ch,ch);
}
output:- 35
Here ch has been defined as a char ,and char cannot take a value bigger than +128.That is why assigned value of ch is 291 and is considered to be 35 (291-128-128).
Data typeRangeBytes occupiedFormat
signed char-128 to +1271%c
unsigned char0 to 2551%c
short signed int-32768 to 327672%d
short unsigned int0 to 655352%u
long signed int-2147483648 to +21474836474%ld
long unsigned int0 to 42949672954%lu
float-3.4e38 to 3.4e384%f
double-1.7e4932 to +1.7e3088%lf
long double-1.7e4932 to 1.7e493210%lf

Expressions
 
Evaluation of expression
 
An expression is a combination of variables, constants and operators arranged according to the syntax of the language.
 
C can handle any complex expression with ease.
 
It is little bit different from algebraic expression.
 
Algebraic ExpressionsC Expressions
axb-cxda*b-c*d
(m+n)(a+b)(m+n)*(a+b)
 
Evaluation of expression:
 
We can evaluate an expression by using assignment statement. As
 
Variable = Expression.
 
e.g. :
 
Temp = ((f * cos(x)/sin (y))+(g * sin(x)/cos(y)))
 
All relevant variables must be assigned values before the evaluation of the expression.
 
Type conversion in expression:
 
To effectively develop C programs, it would be necessary for you to understand the rules that are used for the implicit conversion of operands.
 
If an expression contains an operation between an int and a float, the int would be automatically promoted to a float before carrying out of operation.
 
 
 
 
Expressions
 
Automatic type conversion
 
If the operands are of different types the lower type is automatically converted to thehigher type before the operation proceeds
The result is of the higher type
 
Given below is the sequence of rules that are applied by evaluating expressions.
 
Operator 1Operator 2Result
Long DoubleanyLong Double
DoubleanyDouble
FloatanyFloat
Unsigned Long IntanyUnsigned Long In
Long IntanyLong Int
Unsigned IntanyUnsigned Int
 
Final result of an expression to the type of the variable on the left of the assignment signed before assigning the value to it.
 
However, the following changes are introduced during the final assignment:
 
1. Float to Int causes truncation of the fractional part.
 
2. Double to float causes rounding of digits.
 
3. Long int to int causes dropping of the excess higher order bits
 
Type Casting:
 
Casting a value is a forcing a type conversion in a way that is different from the automatic conversion and this process is called type cast.
This should be clear from the following examples:
 
An example of automatic conversion:
 
 
Output of automatic conversion:
 
 
The answer turns out to be 1.000000 and not 1.5 this is because, 6 and 4 are both integers, and hence 6/4 yields an integer, 1.
 
This 1 when stored in a is converted to 1.000000. But what if you don't want the question to be truncated. One solution is to make either x or y as a float.
 
The general form of casting is:
 
(type_desired) expression;
where type_desired : standard C data types and
expression: constant, variables or an expression.
 
An example of type casting:
 
 
Output of type casting example:
 
 
Type Definition using typedef
 
C allows us to create data types via the typedef statement.
 
The format of typedef statement is:
 
typedef data type new_type_name;
 
Example:
 
typedef int units;
units bat1,bat2; /*this statement is equivalent to int bat1,bat2*/
 
 
 
 

No comments:

Post a Comment