INTRODUCTION TO OPERATORS AND EXPRESSIONS

 

UNIT STRUCTURE

1. Learning Objectives
2. Introduction
3. Operators
1. Arithmetic Operators
2. Relational Operators
3. Logical Operators
4. Assignment Operators
5. Increments and Decrement Operators
6. Conditional Operators
7. Bitwise Operators
8. Special Operators
4. Precedence and Associativity
5. Expressions
6. Type Conversion
7. Let Us Sum Up
8. Further Readings
9. Answers To Check Your Progress
10. Possible Questions

LEARNING OBJECTIVES

 
After going through this unit, you will be able to:
define operators and operands
define and use different types of operators like arithmetic, logical, relational, assignment, conditional, bitwise and special operators.
learn about the order of precedence among operators and the direction in which each associates.
use expession in programming.
perform type conversion to get the correct result in expression

INTRODUCTION


We have already learnt how variables and different data types can be used in programming . These variables, constants and other elements can be joined together by various operators to form expressions.

In this unit you will learn about operators like Arithmetic operators, Relational operators, Logical operators, Assignment operators, Increments, Decrement operators, Conditional operators, Bitwise operators and Special operators. We will see how unary, arithmetic relational, logical, assignment, conditional and bitwise operator are used to form expressions.

Operators


An operator is a symbol which helps the user to command the computer to do a certain mathematical or logical manipulations. Operators are used in programs to manipulate data and variables. They are used with operands to build expressions. For example the following is an expression containing two operands(i.e., 2 and 4) and one oprator(i.e., +)
2 + 4

C language has a rich set of operators which can be classified as :
  • Arithmetic operators
  • Relational Operators
  • Logical Operators
  • Assignment Operators
  • Increments and Decrement Operators
  • Conditional Operators
  • Bitwise Operators
  • Special Operators

Arithmetic Operators


For arithmetic operations such as plus, minus, multiplication, division etc., we need some operators. C provides all the basic arithmetic operators. They are listed in Table 2.1. The operators +, -, * and / all work the same way as they do in other languages.
These operators can operate on any built-in data types allowed in C.

Here a, b, c, x, y are known as operands. The modulo division(or modulus) operator produces the remainder of an integer division.
For example : 5 / 2 = 2 (Division)
where as 5 % 2 = 1 (Modulo Division)

Integer Arithmetic
When an arithmetic operation is performed on two whole numbers or integers than such an operation is called as integer arithmetic. It always gives an integer as the result. Let, x = 5 and y = 2 be two integer numbers. Then the integer arithmetic leads to the following
results:

x + y = 5 + 2 = 7 (Addition)
x – y = 5 - 2 = 3 (Subtraction)
x * y = 5 * 2 = 10 (Multiplication)
x / y = 5 / 2 = 2 (Division)
x%y=5%2=1 (Modulo Division)

In integer division the fractional part is truncated. Division gives the quotient whereas, modulo division gives the remainder of division. Following program is an example to illustrate the above operations.

Program1: Summation, subtraction, multiplication, division and modulo division of two integer numbers.
#include<stdio.h>
#include<conio.h>
void main( ) //tells the compiler the start of the program
{
int n1, n2, sum, sub, mul, div, mod;
clrscr( );
scanf (“%d %d”, &n1, &n2); //inputs the operands
sum = n1+n2;
printf(“\n The sum is = %d”, sum); //display the output
sub = n1-n2;
printf(“\n The difference is = %d”, sub);
mul = n1*n2;
printf(“\n The product is = %d”, mul);
div = n1/n2;
printf(“\n The division is = %d”, div);
mod = n1%n2;
printf(“\n The modulus is = %d”, mod);
getch( );
}

If we enter n1= 5 and n2 =2, then the output of the above program will be :

The sum is = 7
The difference is = 3
The product is = 10
The division is = 2
The modulus is = 1

 

Floating point arithmetic
When an arithmetic operation is performed on two real numbers or fractional numbers, such an operation is called floating point arithmetic. The floating point results can be truncated according to the properties requirement. The modulus operator is not applicable for fractional numbers.
Let x = 15.0 and y = 2.0 then

x + y = 15.0 +2.0 = 17.0
x – y = 15.0 - 2.0 = 13.0
x * y = 15.0 * 2.0 = 30.0
x / y = 15.0 / 2.0 = 7.5

Mixed mode arithmetic
When one of the operand is real and other is an integer and if the arithmetic operation is carried out on these two operands, then it is called as mixed mode arithmetic. If any one operand is of real type then the result will always be real, thus 15 / 10.0 = 1.5


Relational Operators

Often it is required to compare the relationship between operands and bring out a decision and program accordingly. For example, we may compare the age of two persons, marks of students, salary of persons, or the price of two items, and so on. These comparisons can be done with the help of relational operators. C supports the following relational operators:

Table 2.2 Relational operators

A simple relational expression contains only one relational operator and takes the following form:

exp1 relational operator exp2

where exp1 and exp2 are expressions, which may be simple constants, variables or combination of them. Some examples of relational expressions and their evaluated values are listed below:

3.5 <= 12 TRUE
-6 > 0 FALSE
10 < 7 + 5 TRUE
When arithmetic expressions are used on either side of a relational operator, the arithmetic expressions will be evaluated first and then the results compared.

Relational expressions are used in decision making statements of C language such as if, while and for statements to decide the course of action of a running program.


Logical Operators



Logical operators compare or evaluate logical and relational expressions. C has the following logical operators:


The logical operators AND (&&) and OR (||) are used when we want to test more than one condition and make decisions.

Logical AND (&&)
These operator is used to evaluate two conditions or expressions with relational operators simultaneously. If both the expressions to the left and to the right of the logical operator is true then the whole
compound expression is true. Example:
a > b && x = = 8
The expression to the left is a > b and that on the right is x == 8 the whole expression is true only if both expressions are true i.e., if a is greater than b and x is equal to 8.
Logical OR (||)
The logical OR is used to combine two expressions or the condition evaluates to true if any one of the two expressions is true.
Example:
a < m || a < n
The expression evaluates to true if any one of them is true or if both of them are true. It evaluates to true if a is less than either m or n and when a is less than both m and n.

Logical NOT (!)
The logical not operator takes single expression and evaluates to true if the expression is false and evaluates to false if the expression is true. In other words it just reverses the value of the expression.
For example, ! (x >= y)

the NOT expression evaluates to true only if the value of x is neither greater than or equal to y .


Assignment Operators



The Assignment Operator(=) evaluates an expression on the right of the expression and substitutes it to the value or variable on the left of
the expression. For example:
x = a + b ;
In the above statement, the value of a + b is evaluated and substituted to the variable x.
Let us consider the statement x = x + 1;
This can also be written as x + = 1;
i.e., x + = 1; is same as x = x + 1 ;

The commonly used shorthand assignment operators are as follows:
a = a + 1 is same as a += 1
a = a – 1 is same as a -= 1
a = a * (n+1) is same as a *= (n+1) a = a / (n+1) is same as a /= (n+1) a = a % b is same as a %= b

The assignment operator = and the equality operator == are distinctly different. The assignment operator is to assign a value to an identifier, whereas the equality operator is used to determine if two expressions have the same value. These operator cannot be used in place of one another.

Program2: Calculate the sum and average of five numbers.

#include<stdio.h>
#include<conio.h>
void main( )
{
float a,b,c,d,e,sum,avg;
clrscr( );
printf(“Enter the five numbers:\n ”);
scanf(“%f%f%f%f%f ”, &a,&b,&c,&d,&e);
sum=a+b+c+d+e;
avg=sum/5.0;
printf(“\n\nSum is = %f ”,sum);
printf(“\nAverage is = %f ”,avg);
getch( );
}
If we enter 4,10,12, 3 and 6, then the output of the above program will be :

Enter the five numbers:
4 10 12 3 6

Sum is = 35.00
Average is = 7.00

LET US KNOW

Unary Operators
The operator that acts upon a single operand to produce a new value is called Unary operator. Unary operators usually precede their single operands, though some unary operators are written after their operands. Unary minus operation is distinctly different from the arithmetic operator which denotes subtraction (-). The subtraction operator requires two separate operands. All unary operators are of equal precedence and have right-to-left associativity. Following are some examples of the use of unary minus operation:
-145 // unary minus is followed by an integer constant
-0.5 //unary minus is followed by an floting-point constant
- a //unary minus is followed by a variable ‘a’
-5 *(a + b) //unary minus is followed by an arithmetic expresion


CHECK YOUR PROGRESS


1. Choose the correct option:
(i) The shorthand expression for x = x + 10 is:
a) x += 10; b) +x = 10; c) x =+ 10; d) x = 10+;
(ii) What is the value of sum for the expression sum = 5 + 3 * 4 - 1 % 3;
a) 31 b) 8 c) 7 d) 16
(iii) The expression i=30*10+27 evaluates to
a) 327 b) -327 c) 810 d) 0
2. State whether the following expressions are true or false.
(i) The modulus operators % can be used only with integers. (ii) The modulo division operator produces the remainder of an integer division
(iii) 10 % 3 yields a result of 3.
(iv) Unary operator requires more than one operands.
(v) If both the expressions to the left and to the right of the && operator is true, then the whole compound expression is true.


Increment and Decrement Operators



The increment and decrement operators are one of the unary operators which are very useful in C language. These are:

+ + and - -
The increment operator ++ adds 1 to the operand, while the decrement operator - - subtracts 1. These operators are used in a program as follows:
+ + i; or i + +;
- - i; or i - -;

+ + i; is equivalent to i = i+1; (or i + =1;)
- - i; is equivalent to i = i -1; (or i - =1;)

where i is an integer type variable. ++i and i++ means the same thing when they form statements independently. But they behave differently when they are used in expressions on right-hand side of an assignment statement. When the operator ++ is written before the variable name (e.g., ++i) then the operator is called prefix operator and when the operator ++ is written after the variable name(e.g., i++) then it is called postfix operator. Let us consider the following statements:

i = 5;
j = ++i; // pre increment
printf(“%d%d”, i, j);

In this case, the value of j and i would be 6. A prefix operator first adds 1 to the operand and then the result is assigned to the variable on left. Thus 1 is added to i and the value of i becomes 6. Then this incremented value of i is assigned to j and the value of j also becomes 6.


Suppose if we rewrite the above statements as
i = 5;
j = i++; // post increment

then, the value of j would be 5 and i would be 6. This is because a postfix operator first assigns the value to the variable on left and then increments the operands. Thus 5 is first assigned to j and then i is incremented by 1.
Rules for + + and - - Operators
· Increment and decrement operators are unary operators and they require single variable as their operand.

· When postfix ++ (or - -) is used with a variable in an expression, the expression is evaluated first using the original value of the variable and then the variable is incremented

· When prefix ++ (or - -) is used in an expression, the variable is incremented (or decremented) first and then the expression is evaluated using the new value of the variable.

· The presedence and associativity of ++ and - - operators are the same as those of unary + and -.

Conditional Operators



The conditional operator consists of two symbols, the question mark (?) and the colon (:) .

The syntax for a ternary operator is as follows:
exp1 ? exp2 : exp3

where exp1, exp2, and exp3 are expressions. exp1 is evaluated first. If the expression is true then exp2 is evaluated and its value be comes the value of the expression. If exp1 is false, exp3 is evaluated and its value becomes the value of the expression. Only one of the expression is evaluated. For example :
a = 10;
b = 15;
x = (a > b) ? a : b
Here x will be assigned to the value of b. The condition follows that the expression is false therefore b is assigned to x.

Program4: Program to illustrate the use of conditional operator.
#include<stdio.h>
#include<conio.h>
void main( )
{
int age;
clrscr( );
printf(“Enter your age in years: ”);
scanf(“%d”,&age);
(age>=18)? printf(“\nYou should vote\n”) : printf(“You cannot vote”);
getch( );
}
Output : Enter your age in years:
26
You should vote
If we run the program again and enter age=15, then the output will be:
Enter your age in years:
15
You cannot vote

Program5: Program for finding the larger value using conditional
operator
#include<stdio.h>
void main()
{
int i,j, large;
printf (“Enter 2 integers : ”); //ask the user to input 2 numbers
scanf(“%d %d”,&i, &j);
large = i > j ? i : j; //evaluation using conditional operator
printf(“The largest of two numbers is %d \n”, large);
}
Output : Enter 2 integers : 14 25
The largest of two numbers is 25

Bitwise Operators


C has a distinction of supporting special operators known as bitwise operators for manipulation of data at bit level. A bitwise operator operates on each bit of data. Those operators are used for testing, complementing or shifting bits to the right or left. Bitwise operators may not be applied to a float or double.


Bitwise Logical Operators :
The logical bitwise operators are similar to the Boolean operators, except that they operate on every bit in the operand(s). For instance, the bitwise AND operator (&) compares each bit of the left operand to the corresponding bit in the righthand operand. If both bits are 1, a 1 is placed at that bit position in the result. Otherwise, a 0 is placed at that bit position.

Bitwise AND (&) Operator
The bitwise AND operator performs logical operations on a bit-by-bit level using the following truth table:



Let us consider the following program segment for understanding AND(&) operation.
void main( )
{ unsigned int a = 60; // 60 = 0011 1100
unsigned int b = 13; // 13 = 0000 1101
unsigned int c = 0;
c = a & b; //12 = 0000 1100
}
Bitwise OR (|)
The bitwise OR operator performs logical operations on a bit-by-bit level using the following truth table:


The bitwise OR operator (|) places a 1 in the resulting value’s bit position if either operand has a bit set (i.e.,1) at the position. Bitwise OR(|) operation can be understood with the following example:
void main( )
{ unsigned int a = 60; // 60 = 0011 1100
unsigned int b = 13; // 13 = 0000 1101
unsigned int c = 0;
c = a | b; // 61 = 0011 1101
}
Bitwise exclusive OR (^)
The bitwise exclusive OR(XOR) operator performs logical operations on a bit-by-bit level using the following truth table:



The bitwise exclusive OR(^) operator sets a bit in the resulting value’s bit position if either operand (but not both) has a bit set (i.e.,1)at the position. Bitwise exclusive OR(^) operation can be understood with the following example:
void main( )
{ unsigned int a = 60; // 60 = 0011 1100
unsigned int b = 13; // 13 = 0000 1101
unsigned int c = 0;
c = a ^ b; // 49 = 0011 0001
}

Bitwise Complement (~)
The bitwise complement operator (~) performs logical operations on a bit-by-bit level using the following truth table:

Table 2.8: Truth table for the ~, Bitwise Complement


The bitwise complement operator (~) reverses each bit in the operand.
Bitwise Shift Operators :
C provides two bitwise shift operators, bitwise left shift (<<) and bitwise right shift (>>), for shifting bits left or right by an integral number of positions in integral data. Both of these operators are binary, and the left operand is the integral data whose bits are to be shifted, and the right operand, called the shift count, specifies the number of positions by which bits need shifting. The shift count must be nonnegative and less than the number of bits required to represent data of the type of the left operand.

Left-Shift (<<) operator
The left shift operator shift bits to the left. As bits are shifted toward high-order positions, 0 bits enter the low-order positions. Bits shifted out through the high-order position are lost. For example, let us consider the following declaration:

unsigned int Z = 5;
and Z in binary 00000000 00000101

Now if we apply left-shift, then
Z << 1 is 00000000 00001010 or 10 decimal
and Z << 15 is 10000000 00000000 or 32768 decimal.

Left-Shift is useful when we want to MULTIPLY an integer (not floating point numbers) by a power of 2. The operator, takes 2 operands like this:
a << b
This expression returns the value of a multiplied by 2 to the power of b.

For example, let us consider 4 << 2. In binary, 4 is 100. Adding 2 zeros to the end gives 10000, which is 16, i.e., 4*22 = 4*4 = 16.

Similarly, 4 << 3 can be evaluated by adding 3 zeros to get 100000, which is 4*23 = 4*8 = 32.

Shifting once to the left multiplies the number by 2.Multiple shifts of 1 to the left, results in multiplying the number by 2 over and over again. In other words, multiplying by a power of 2. Some examples are:

5 << 3 = 5*23 = 5*8 = 40
8 << 4 = 8*24 = 8*16 = 128
1 << 2 = 1*22 = 1*4 = 4

Right-Shift (>>) operator
The right shift operator shifts bits to the right. As bits are shifted toward low-order position, 0 bits enter the high-order positions, if the data is unsigned. If the data is signed and the sign bit is 0, then 0 bits also enter the high- ordr positions. However, if the sign bit is 1, the bits entering high-order positions are implementation-dependent. On some machines 1s, and on others 0s, are shifted in. The former type of operation is known as the arithmetic right shift, and the latter type the logical right shift. For example,
unsigned int Z = 40960;
and Z in bibary 16-bit format is 10100000 00000000

Now if we apply right-shift, then
Z >> 1 is 01010000 00000000 or 20480 decimal
and Z >> 15 is 00000000 00000001 or 1 decimal

In the second example, the 1 originally in the fourteenth bit position has dropped off. Another right shift will drop off the 1 in the first bit position, and Z will become zero. Bitwise Right-Shift does the opposite, and takes away bits on the right.



Special Operators



C supports some special operators 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.

The Comma Operator
The comma operator can be used to link related expressions together. The comma allows for the use of multiple expressions to be used where normally only one would be allowed. It is used most often in the for loop statement where one statement is called for, but several actually need to be coded.

The comma operator forces all operations that appear to the left to be fully completed before proceeding to the right of comma. This helps eliminate side effects of the expression evaluation.

num1 = num2 + 1, num2 = 2

The comma insures that num2 will not be changed to a 2 before num2 has been added to 1 and the result placed into num1. Some examples of comma operator are:
In for loops:
for (n=1, m=15, n <=m; n++,m++)
In while loops:
while (c=getchar(), c != ‘15’)
Exchanging values :
temp = x, x = y, y = temp;

Program5 : Swap (interchange) two numbers using a temporary
variable.
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,temp;
clrscr();
printf("\nEnter the two integer numbers:");
scanf("%d%d",&a,&b);
printf("\nEntered numbers are.:");
printf("%d%8d",a,b);
temp=a,a=b,b=temp; // comma operator is used
printf("\n\nSwapped numbers are:%d%8d",a,b);
getch();
}

Outout : (Suppose we have entered 4 and 8)
Enter the two integer numbers.: 4 8
Entered numbers are: 4 8

Swapped numbers are: 8 4

The Sizeof Operator

The sizeof operator returns the physical size, in bytes, of the data item for which it is applied. It can be used with any type of data item except bit fields.

When sizeof is used on a character field the result returned is 1 (if a character is stored in one byte). When used on an integer the result returned is the size in bytes of that integer. When used on an array the result is the number of bytes in the array, not the number of characters which appear before a NULL.

For example: s = sizeof (sum);
t = sizeof (long int);
The sizeof 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.

Program6: Program that employs different kinds of operators like arithmetic, increment, conditional and sizeof operators.
#include<stdio.h>
#include<conio.h>
void main( )
{ int a, b, c, d,s;
clrscr( );
a = 20;
b = 10;
c = ++a-b;
printf (“a = %d, b = %d, c = %d\n”, a,b,c);
d=b++ + a;
printf (“a = %d, b = %d, d = %d\n, a,b,d);
printf (“a / b = %d\n, a / b);
printf (“a %% b = %d\n, a % b);
printf (“a *= b = %d\n, a *= b);
printf (“%d\n, (c < d) ? 1 : 0 );
printf (“%d\n, (c > d) ? 1 : 0 );
s=sizeof(a);
printf(“\nSize is: %d bytes”,s);
}
Output : a=21 b=10 c=11
a=21 b=11 d=32
a/b=1
a%b=10
a*=b=231
1
0
2 bytes
The increment operator ++ works when used in an expression. In the statement c = ++a – b; new value a = 16 is used thus giving value 6 to C. That is a is incremented by 1 before using in expression
However in the statement d = b++ + a; The old value b = 10 is used in the expression. Here b is incremented after it is used in the expres
sion.

EXERCISE - 1

1. Write a program that reads a floting-point number and then displays the right-most digit of the integral part of the number.

CHECK YOUR PROGRESS



1. Find the output of the following profram segment?
(a) void main(){ int x = 50;
printf(“%d\n”,5+ x++);
printf(“%d\n”,5+ ++x); }
(b) void main(){ int x, y;
x = 50;
y =100;
printf(“%d\n”,x+ y++);
printf(“%d\n”,++y -3); }
(c) void main(){ int s1,s2;
char c='A';
float f;
s1=sizeof(c);
s2=sizeof(f);
printf("ASCII value of 'A' is %d",c);
printf("\nSize of s1 and s2 in bytes:%d%8d",s1,s2);}

2. Find the output of the following C program:
void main( )
{ int a,b,c;
a=b=c=0;
printf(“Initial value of a,b,c :%d%d%d\n”,a,b,c);
a=++b + ++c;
printf(“\na=++b + ++c=%d%d%d\n”,a,b,c);
a= b++ + c++;
printf(“\na=b++ + c++= %d%d%d\n”,a,b,c);
a=++b + c++;
printf(“\na=++b + c++= %d%d%d\n”,a,b,c);
a=b-- +c --;
printf(“\na=b-- +c --= %d%d%d\n”,a,b,c);
}

3. Choose the correct option:
(i)If i=6, and j=++i, the the value of j and i will be
(a) i=6,j=6 (b) i=6, j=7 (c)i=7,j=6 (d)i=7,j=7

(ii) If the following variables are set to the values as shown below, then what will be the expression following it?
answer=2;
marks=10;
!((“answer<5”)&& (marks>2))
(a) 1 (b)0 (c)-1 (d)2



Precedence and Associativity




There are two important characteristics of operators which determine how operands group with operators. These are precedence and associativity. The operators have an order of precedence among themselves. This order of precedence dictates in what order the operators are evaluated when several operators are together in a statement or expression. An operator’s precedence is meaningful only if other operators with higher or lower precedence are present. Expressions with higher-precedence operators are evaluated first. The grouping of operands can be forced by using parentheses. Also, with each operator is an associativity factor that tells in what order the operands associated with the operator are to be evaluated. Associativity is the left-to-right or right-to-left order for grouping operands to operators that have the same precedence.

The following Table.2.9 lists C operators in order of precedence (highest to lowest). Their associativity indicates in what order operators of equal precedence in an expression are applied. R indicates Right and L indicates Left.

Table.2.9: Precedence and Associativity



For example, in the following statements, the value 10 is assigned to both a and b because of the right-to-left associativity of the = operator. The value of c is assigned to b first, and then the value of b is assigned to a.
b = 9;
c = 10;
a = b = c;
In the expression
a + b * c / d
the * and / operations are performed before + because of precedence. b is multiplied by c before it is divided by d because of associativity.


Expressions


C Expressions are based on algebra expressions - they are very similar to what we learn in Algebra, but they are not exactly the same. An expression is a combination of variables, constants and operators written according to the syntax of C language. In C every expression evaluates to a value i.e., every expression results in some value of a certain type that can be assigned to a variable. Here are some examples of expressions:
15 // a constant
i // a variable
i+15 // a variable plus a constant
(m + n) * (x + y)

The following program illustrates the effect of presence of parenthesis in expressions.

Program7:
#include<stdio.h>
#include<conio.h>
void main( )
float a, b, c x, y, z;
a = 9;
b = 12;
c = 3;
x = a – b / 3 + c * 2 – 1;
y = a – b / (3 + c) * (2 – 1);
z = a – ( b / (3 + c) * 2) – 1;
printf (“x = %fn”,x);
printf (“y = %fn”,y);
printf (“z = %fn”,z);
}

Output
x = 10.00
y = 7.00
z = 4.00

Rules for evaluation of expression

  • First parenthesized sub expression left to right are evaluated.
  • If parenthesis are nested, the evaluation begins with the inner most sub expression.
  • The precedence rule is applied in determining the order of application of operators in evaluating sub expressions.
  • The associability rule is applied when two or more operators of the same precedence level appear in the sub expression.
  • Arithmetic expressions are evaluated from left to right using the rules of precedence.
  • When Parenthesis are used, the expressions within paren thesis assume highest priority.

Type conversion


The type conversion or typecasting refers to changing an entity of one data type into another. C allows programmers to perform typecasting by placing the type name in parentheses and placing this in front of the value. The form of the cast data type is :

(type) expression

Let us consider the case where we want to divide two integers a/b, where the result must be an integer. However, we may want to force the output to be a float type in order to keep the fraction part of the division. The typecast operator is used in such a case. It will do the conversion without any loss of fractional part of data.

Program8:
#include<stdio.h>
void main()
{
int a,b;
a=3,b=2;
printf(“\n%f”,(float)a/b);
}

The output of the above program will be 1.500000. This is because data type cast (float) is used to force the type of the result to be of the type float.

From the above it is clear that the usage of typecasting is to make a variable of one type, act like another type for one single operation. So by using this ability of typecasting it is possible for create ASCII characters by typecasting integer to its character equivalent.

Typecasting is also used in arithmetic operation to get correct result. This is very much needed in case of division when integer gets divided and the remainder is omitted. In order to get correct precision value, one can make use of typecast as shown in example above. Another use of the typecasting is shown in example below

For instance:
void main()
{
int a = 5000, b = 7000 ; long int c = a * b ;
}

Here two integers are multiplied and the result is truncated and stored in variable c of type long int. But this would not fetch correct result for all. To get a more desired output the code is written as

long int c = (long int) a * b;

Though typecast has so many uses one must take care about its usage since using typecast in wrong places may cause loss of data like for instance truncating a float when typecasting to an int.

CHECK YOUR PROGRESS


1. State whether the following expressions are true or false.

(i) Conditional operator (? :) has right to left associativity.

(ii) Logical OR operator has right to left associativity

(iii)C permits mixing of constants and variables of different types in an expression.

(iv)Precedence dictates in what order the operators are evalu ated when several operators are together in a statement or expression.

(v) A typecast is used to force a value to be of a particular variable type.



LET US SUM UP




Assignment Operator
The Assignment Operator(=) evaluates an expression on the right of the expression and substitutes it to the value or variable on the left of the expression. For example:
sum = n1+n2 ;
value of n1 and n2 are added and the result is assigned to the variable sum.

Increment and Decrement



The Conditional Operator
It works on three values. The conditional operator is used to replace if-else logic in some situations. It is a two-symbol operator ?: with the format:
result = condition ? expression1 :expression2;

Comma Operator
We can use the comma operator(,) available in c language, to build a compound expression by putting several expressions inside a set of parentheses. The expressions are evaluated from left to right and the final value is evaluated last.

sizeof Operator
The sizeof operator returns the physical size in bytes of the data item for which it is applied. It can be used with any type of data item except bit fields. The general form is: s = sizeof (item );

C expressions are syntactically valid combinations of operators amd operands that compute to a value determined by the priority and associativity of the operators.

FURTHER READINGS


1. Balagurusamy, E: Programming in ANSI C, Tata McGraw-Hill publication.
2. Gottfried Byron S: Programming with C, Tata McGraw-Hill publication.

ANSWERS TO CHECK YOUR PROGRESS


CHECK YOUR PROGRESS -1

1.(i) (a) x = x +10 (ii)(d) 16 (iii) (a) 327

2. (i)True (ii) True (iii) False (iv) False (v) True

CHECK YOUR PROGRESS - 2

1. (a) 55 (b)150
99 57

(c) ASCII value of 'A' is 65
Size of s1 and s2 in bytes: 1 4


2. Initial value of a,b,c : 0 0 0
a= ++b + ++c = 2 1 1
a= b++ + c++ = 2 2 2
a= ++b + c++ = 5 3 3
a= b-- + c -- = 6 2 2

3. (i)(d)i=7,j=7 (ii) (b) 0

CHECK YOUR PROGRESS - 3

1. (i) True (ii)False (iii)True (iv)True (v) True



POSSIBLE QUESTIONS


1.What is an operator ? What are the types of operators that are included in C.

2. What is an operand ? What is the relationship between operator and operands?

3.Describe the three logical operators included in C?

4. Write a c program to compute surface area and volume of a cube if the side of the cube is taken as input.

5. What is unary operator ? How many operands are associated with a unary operator?

6. What is meant by operator precedence?

7. What is meant by associativity? What is the asslociativity of the arithmetic operators?

8. What will be the output of the following program:
#include<stdio.h>
#include<conio.h>
void main( )
{
printf(“The size of char is %d”,sizeof(char));
printf(“\nThe size of int is %d”,sizeof(int));
printf(“\nThe size of short is %d”,sizeof(short));
printf(“\nThe size of float is %d”,sizeof(float));
printf(“\nThe size of long is %d”,sizeof(long));
printf(“\nThe size of char is %d”,sizeof(char));
printf(“The size of double is %d”,sizeof(double));
getch();
}


  TOP
s