Operators and Expressions II-45
Table 3.7 Assignment Operators
=
*= /= %=
+=
=
<<= >>=
17. What are compound assignment operators?
Ans: A compound assignment operator consists of a binary operator and assignment operator.
They carry out binary operation and assign the result to the left operand.
Compound assignment operators and equivalent expressions are as follows:
a += b is equivalent to a = a + b
a = b is equivalent to a = a b
a *= b is equivalent to a = a * b
a /= b is equivalent to a = a/b
a %= b is equivalent to a = a%b
a <<= b is equivalent to a = a << b
a >>= b is equivalent to a = a >> b
a &= b is equivalent to a = a&b
a |= b is equivalent to a = a|b
a ^= b is equivalent to a = a^b
18. Explain precedence and associativity of operators.
Ans: Operators have the following properties:
1. Precedence: It refers to the priority given to the operator while evaluation of an expression.
When an expression contains many operators, the operations are carried out according to the
priority of the operators. The higher precedence operations are evaluated first.
2. Associativity: When an expression has operators with equal precedence, the associativity property
decides which operation to be carried out first. Associativity means the direction of execution.
It has two types.
(a) Left to right: In this type, the expression evaluation starts from the left to the right direction.
(b) Right to left: In this type, the expression evaluation starts from the right to the left direction.
19. What are the differences between precedence and associativity?
Ans: Precedence decides which operator to execute first when there are more than one different
operators with different priorities. On the other hand, associativity decides which operator to execute
first when there is more than one operator with same priority.
20. What is the difference between pre- and post-increments?
Ans: Consider the following pre- and post-increment operators:
1. Pre-increment example ++a;
2. Post-increment example a++;
In the first case, the variable as value is incremented first before being used and in the second case value
of a is incremented after being used.
M03_ITL-ESL4791_02_SE_C03.indd 45 12/22/2012 5:00:07 PM
II-46 Programming Concepts
Programming example:
void main()
{
int a=2;
printf( " %d",++a);
printf( " %d",a++);
printf( " %d",a);
getche();
}
OUTPUT:
3
3
4
21. Comment on the execution speed of ++a and a +1.
Ans: ++a execution is faster than a +1. ++a needs single machine instruction and a +1 needs more
instructions. In the latter case, the value of the variable is to be stored in the memory first and then
it is be fetched by CPU and stored in some register and then 1 is added. After this process, result is
assigned to a. Result of this operation is to be stored in the memory. Hence, CPU consumes more time.
This procedure of execution needs more time. Hence, ++a is faster than a +1.
Note: Nowadays compilers are smarter, if it encounters i +1, then it replaces it by i++. Hence, it really
does not matter for user since optimization is gained by compiler.
22. What are the rules for using unary ++ and − − operators?
Ans:
1. The operand should not be a constant but it should be a variable.
2. The ++ and −− should not be applied on an expression.
3. The operator ++ and −− may succeed or precede the operand.
23. What are the uses of masking?
Ans: A bit or desired bits of a binary number can be set to 0 with the help of masking. The AND
operator is required for masking.
24. Which binary operator is suitable to check a bit is on or off?
Ans: & (AND) operator is suitable for checking whether a particular bit is on or off.
Following example can be referred for understanding the question.
void main()
{
int a=02,b=02;
printf("%d",a&b);
M03_ITL-ESL4791_02_SE_C03.indd 46 12/22/2012 5:00:07 PM
Operators and Expressions II-47
getche();
}
OUTPUT:
02
Due to ANDing between 02 and 02 answer returns 02. In case b = 00, the answer becomes 00.
25. Which binary operator is used for addition of two numbers?
Ans: EX-OR operator can be used for addition of two numbers.
One can verify from the following program.
void main()
{
int a=2,b=4;
printf("%d",a^b);
getche();
}
OUTPUT:
6
26. What is the 'sizeof()'operator in C? Explain the use of 'sizeof()'operator by 
using a short program?
Ans: The 'sizeof()' operator is a unary operator. The 'sizeof()' operator returns the size
of data type/operand in bytes. The operand may be a variable or data type. Size of various data types are
listed Table 3.8.
Table 3.8 Sizes of Various Data Types
Data Type Size in Bytes
char
1
int 2
long int 4
float
4
double float
8
long double
10
Program by using 'sizeof()' operator.
#include <stdio.h>
#include <conio.h>
void main()
{
char a;
printf(" Sizeof(a) = %d bytes",sizeof(a));
M03_ITL-ESL4791_02_SE_C03.indd 47 12/22/2012 5:00:07 PM
II-48 Programming Concepts
printf(" Sizeof(int) = %d bytes",sizeof(int));
}
OUTPUT:
Sizeof(a) = 1 bytes
Sizeof(int) = 2 bytes
27. How can multiple assignments bewritten in C? Explain it with programming one example.
Ans: The syntax of multiple assignments is as follows: identifier1=identifier2=
identifier3=…………….identifier N= value or expression.
Note:The assignments are carried out from right to left.
Example:
#include <stdio.h>
#include <conio.h>
void main()
{
clrscr();
int x,y,z;
clrscr();
x=y=z=10;
printf("x = %d y = %d z = %d ",x,y,z);
getch();
}
OUTPUT:
x = 10
y = 10
z = 10
Expla nation: Inthe above program, the expression x = y = z = 10 uses multiple assignment operator.
The value 10 will be assigned to x, y and z from right to left, i.e., first assigns to z, then to y and at
last to x.
28. Which type of operator is logical not? What is the associativity of logical NOT?
Ans: The not !’is unary operator. Not operator negates the value of expression. The unary not
operator is also known as logical not operator. The associativity of logical operator is from right
to left.
29. What is type casting? Give an example.
Ans: It is a data type converter. For example, floating number can be converted to integers or result
that produces integers can be converted to floating. The expected type of data is to be placed within the
parentheses.
M03_ITL-ESL4791_02_SE_C03.indd 48 12/22/2012 5:00:07 PM
Operators and Expressions II-49
Programming example is as follows:
void main()
{
int a=12,b=5;
printf("%f",(float)a/b);
getche();
}
2.400000
void main()
{
float a=12.40;
printf("%d",(int)a);
}
OUTPUT:
12
30. Which binary shifting is equivalent to dividing an unsignednumber by 2:right shifting 
by 1 or left sifting by 1?
Ans: Dividing an unsigned number by 2 is equivalent to right shifting an unsigned number by
1 is the correct option. This can be verified from the following example:
void main()
{
unsigned int a=8,b=1;
printf("%d",a>>b);
}
OUTPUT:
4
31. What is the difference between shift right (>>) and shift left (<<) operators?
Ans: Theshift right >> operator shifts the bits of the variable/operand to right side. The shift left <<
operator is used to shift the bits of the variable/operand to left.
The syntax of shift right operator is as follows:
Variable >> number of bits to shift right.
M03_ITL-ESL4791_02_SE_C03.indd 49 12/22/2012 5:00:08 PM
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset