61) What is dangling pointer?
If a pointer is de-allocated or freed and the pointer is not assigned to NULL, then it may still contain that address and accessing the pointer means that we are trying to access that location and it will give an error. This type of pointer is called dangling pointer.
We know that a[i] can be written as *(a+i). Same way, the array elements can be written like pointer expression as follows -
a[i][j] == *(*(a+i)+j)
a[i][j][k] == *(*(*(a+i)+j)+k)
a[i][j][k][l] == *(*(*(*(a+i)+j)+k)+l)
63) Which bit wise operator is suitable for checking whether a particular bit is on or off?
"Bitwise And" (&) is used to check if any particular bit is set or not. To check whether 5'th bit is set we can write like this
bit = (byte >> 4) & 0x01;
Here, shifting byte by 4 position means taking 5'th bit to first position and "Bitwise And" will get the value in 0 or 1.
64) When should we use register modifier?
The register modifier is used when a variable is expected to be heavily used and keeping it in the CPU’s registers will make the access faster.
65) Why doesn't the following statement work?
char str[ ] = "Hello" ;
strcat ( str, '!' ) ;
The string function strcat( ) concatenates two strings. But here the second argument is '!', a character and that is the reason why the code doesn't work. To make it work, the code should be changed like this:
strcat ( str, "!" ) ;
66) Predict the output or error(s) for the following program:
int const * p = 5;
The above program will result in compilation error stating “Cannot modify a constant value”. Here p is a pointer to a constant integer. But in the next statement, we are trying to modify the value of that constant integer. It is not permissible in C and that is why it will give a compilation error.
67)Guess the output:
unsigned int a = 2;
int b = -10;
(a + b > 0)?puts("greater than 0"):puts("less than 1");
Output - greater than 0
If you have guessed the answer wrong, then here is the explanation for you. The a + b is -8, if we do the math. But here the addition is between different integral types - one is unsigned int and another is int. So, all the operands in this addition are promoted to unsigned integer type and b turns to a positive number and eventually a big one. The outcome of the result is obviously greater than 0 and hence, this is the output.
68) Write a code fragment to set and clear only bit 3 of an integer.
#define BIT(n) (0x1 << n)
a |= BIT(3);
a &= ~BIT(3);
69) What is wrong with this code?
int square(volatile int *p)
return *p * *p;
The intention of the above code is to return the square of the integer pointed by the pointer p. Since it is volatile, the value of the integer may have changed suddenly and will result in something else which will looks like the result of the multiplication of two different integers. To work as expected, the code needs to be modified like this.
int square(volatile int *p)
int a = *p;
70) Is the code fragment given below is correct? If so what is the output?
int i = 2, j = 3, res;
res = i+++j;
The above code is correct, but little bit confusing. It is better not to follow this type of coding style. The compiler will interpret above statement as “res = i++ + j”. So the res will get value 5 and i after this will be 3.