
This post has few of the frequently asked C interview questions. Questions below are related to storage classes, bitwise operators in C language. Question.17, 18 test whether the interviewee candidate is aware of storage classes in C language, their default values, scope etc. Question.19 is an open ended question that tests the knowledge of usage of pointers in C programs. Question.20, 21 test knowledge of usage of C bitwise operators to ON / OFF bits. These questions are siginificant especially if the interview is on embedded systems.See all C interview questions in this blog in one page.
< Previous Questions(12-16) | All | Next Questions(22-26) >
C Language Interview Questions (17 to 21)
- What are storage memory, default value, scope and life of Automatic and Register storage class?
- What are storage memory, default value, scope and life of Static and External storage class?
- What are the advantages of using pointers in a program?
- Which bitwise operator is suitable for checking whether a particular bit is ON or OFF?
- Which bit wise operator is suitable for turning OFF a particular bit in a number?
< Previous Questions(12-16) | All | Next Questions(22-26) >
- What are storage memory, default value, scope and life of Automatic and Register storage class?
- Automatic storage class:
- Storage : main memory.
Default value : garbage value.
Scope : local to the block in which the variable is defined.
Lifetime : till control remains within the block. - Register storage class:
- Storage : cpu registers.
Default value : garbage value.
Scope : local to the block in which the variable is defined.
Lifetime : till control remains within the block.
- Automatic storage class:
- What are storage memory, default value, scope and life of Static and External storage class?
- Static storage class:
- Storage : main memory
Default value : zero
Scope : local to the block in which the variable is defined
lifetime : till the value of the variable persists between different function calls. - External storage class:
- Storage : main memory
Default value : zero
Scope : global
Lifetime : as long as the program execution doesn’t come to an end.
- Static storage class:
- What are the advantages of using pointers in a program?
- Pointers allow us to pass values to functions using call by reference. This is useful when large sized arrays are passed as arguments to functions. A function can return more than one value by using call by reference.
- Dynamic allocation of memory is possible with the help of pointers.
- We can resize data structures. For instance, if an array’s memory is fixed, it cannot be resized. But in case of an array whose memory is created out of malloc can be resized.
- Pointers point to physical memory and allow quicker access to data.
- Which bitwise operator is suitable for checking whether a particular bit is ON or OFF?
- Which bit wise operator is suitable for turning OFF a particular bit in a number?
Pointers are special variables which store address of some other variables.
Syntax: datatype *ptr;
Here * indicates that ptr is a pointer variable which represents value stored at a particular address.
Example: int *p;
‘p’ is a pointer variable pointing to address location where an integer type is stored.
Advantages:
Bitwise AND operator.
Example: Suppose in byte that has a value 10101101 . We wish to check whether bit number 3 is ON (1) or OFF (0) . Since we want to check the bit number 3, the second operand for AND operation we choose is binary 00001000, which is equal to 8 in decimal.
Explanation:
ANDing operation :
10101101 original bit pattern
00001000 AND mask
---------
00001000 resulting bit pattern
---------
The resulting value we get in this case is 8, i.e. the value of the second operand. The result turned out to be a 8 since the third bit of operand was ON. Had it been OFF, the bit number 3 in the resulting bit pattern would have evaluated to 0 and complete bit pattern would have been 00000000. Thus depending upon the bit number to be checked in the first operand we decide the second operand, and on ANDing these two operands the result decides whether the bit was ON or OFF.
Bitwise AND operator (&), one’s complement operator(~)
Example: To unset the 4th bit of byte_data or to turn off a particular bit in a number.
Explanation:
Consider,
char byte_data= 0b00010111;
byte_data= (byte_data)&(~(1<<4));
1 can be represented in binary as 0b00000001 = (1<<4)
<< is a left bit shift operator,
it shifts the bit 1 by 4 places towards left.
(1<<4) becomes 0b00010000
And ~ is the one's complement operator in C language.
So ~(1<<4) = complement of 0b00010000
= 0b11101111
Replacing value of byte_data and ~(1<<4) in
(byte_data)&(~(1<<4));
we get (0b00010111) & (0b11101111)
Perform AND operation to below bytes.
00010111
11101111
-----------
00000111
-----------
Thus the 4th bit is unset.
| Previous | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | Next |