EmbLogic's Blog

Tricky C interview questions

1.Give a fastest way to multiply any number by 9.
Answer:
#include
#include
void main()
{
int n;
printf(“Enter a number:”);
scanf(“%d”,&n);
printf(“%d”, (n<<3)+n);
getch();
}
2. How to measure the size of any variable without “sizeof” operator?
Answer:
#define size_of(x) ((char *)(&x+1) – (char *)&x)
void main()
{
Short int x;
Printf(“%d”,size_of(x));
}

Explanation:
By using the &x, we get the base address of the variable x and by adding 1 to it we get the base address of next short int type. Hence the resulting address of (&x+1) will be 2 bytes more than the base address of the variable x. But if we just display the difference between the base address of x and the incremented address, then the difference will be ‘1’, means “1 block of short int type has been added” but we need the result of size of variable in terms of bytes not in terms of blocks. This can be achieved if we typecast the address into char *, because the address of char data type will always be in block of one byte, hence if the difference between the base address of x and the incremented address is displayed in terms of char type, then the difference will be displayed as 2, because the difference is actually 2 blocks or 2 bytes in terms of char type representation.

3. How to measure the size of any variable without using “sizeof” operator?
Answer:
#define SIZE_OF(T) (((T *)0)+1)
void main()
{
clrscr();
printf(“%d”,SIZE_OF(long int));
}

Explanation:

Whenever we typecast any constant, it converts the constants into a base address of specified data type and as we know (address+1) will always return the next address of its type or we can say the address of next block of memory of that type, so accordingly if 1 will be added with the address ‘0’, then it will return the size of any data type in bytes. For example 1 for char, 2 for short int, 4 for long int or float and so on.

4. Write code snippets to swap two variables in five different ways.
Answer:
a. /* swapping using three variables*/ (Takes extra memory space)
Int a=5, b=10, c;
c=a;
a=b;
b=c;

b. /* using arithmetic operators */
a=a+b;
b=a-b;
a=a-b;

c. /* using bit-wise operators */
a=a^b;
b=b^a;
a=a^b;

Line
Operation
Value of a
Value of b

1
-
5
10
Initial values
2
a=a^b
15
10

3
b=a^a
15
5

4
a=a^b
10
5
values after swapping

d. /* one line statement using bit-wise operators */ (most efficient)
a^=b^=a^=b;

The order of evaluation is from right to left. This is same as in approach (c) but the three statements are compounded into one statement.

e. /* one line statement using arithmetic & assignment operators */
a=(a+b) – (b=a);
In the above axample, parenthesis operator enjoys the highest priority & the order of evaluation is from left to right. Hence (a+b) is evaluated first and replaced with 15. Then (b=a) is evaluated and the value of a is assigned to b, which is 5. Finally a is replaced with 15-5, i.e. 10. Now the two numbers are swapped.

5. How to swap between first & 2nd byte of an integer in one line statement?
Answer:
int x=0×1234;
x = x<>8;

Explanation:
Let x = 0×1234

x = 00010010 00110100
x<>8 = 00000000 00010010
x<>8 = 00110100 00010010 i.e. value of ‘x’ after swap is 0×3412

6. What is the efficient way to divide a no. by 4?
Answer:
x = x>>2;

7. Suggest an efficient method to count the no. of 1’s in a 32 bit no. Remember without using loop & testing each bit.
Answer:
(i) int count_set_bits (long n)
{
int count = 0;
while (n)
{
count ++;
n & = n-1;
}
return (count);
}
(ii) int count_set_bits (long n)
{
return (n ? 1+ count_set_bits (n&n-1) : 0);
}

Explanation:

The logic is that the while loop continues as long as there are set bits (1) in the no. If all the bits became 0, that means no. is ‘0’ & the loop terminates. The logic needs conversion of every 1 to 0, which can be achieved by making a bitwise ‘&’ operation between the no. (n) and its previous no. (n-1) and assigning the resulting value back to n. If this is done, then with every run of the loop each set bit (1) will be reset. ( i.e. converted to 0).
Let n= 30
30 = 00011110
29 = 00011101
(30 & 29) = 00011100

We can see that the 2nd bit of 30 is reset to 0 which gives the no 28 which is now assigned to n (n &=n-1). This step is repeated until n becomes ‘0’.

8. Test whether a no. is power of 2 or not.
Answer:
void main ()
{
int n;
printf (“\n Enter any no:”);
scanf (“%d”, & n);
if (n & & ((n & n-1) = = 0))
printf (“It is power of 2”);
else
printf (“It is not power of 2”);
}
Test:
Enter any no: 32
It is power of 2

Enter any no: 56
It is not power of 2

Explanation:

The logic says that if a no. is power of 2, then in the binary representation, only one bit of the no. can be ‘1’ & rest are must be ‘0’.
For example the number 32 has a binary representation of 00100000.

9. How to check endianness of the computer.

Answer:
Logic-1
void main ()
{
int x = 300;
if ((* ((unsigned char *) & x) == 1)
&& (*(unsigned char*) & x+1) == 44))
printf (“BIG ENDIAN”);
else
printf (“LITTLE ENDIAN”);
}

Logic-2
void main ()
{
union xxx
{
unsigned int x;
unsigned char ch[2];
};
union xxx p = {300};
if ((p.ch [0] = = 1) && (p. ch [1] == 44))
printf (“BIG ENDIAN”);
else
printf (“LITTLE ENDIAN”);
}

Explanation:
00000001
00101100
1
44
Big Endian:

00101100
00000001
44
1
Little Endian:

Address are always created in memory in the form of ABCD format, but the data can be stored in memory either in DCBA format, which is little endian system or in the ABCD format in case of big-endian system.
Lets store 300 in the memory location of an integer. Now if we visit the memory location byte by byte we will find that the contents at first byte will be 1 & the content of the other will be 44. In a Big-endian system the 1st byte will contain 1, and the 2nd byte will be 44 according to the ABCD data storage format where as the little-endian system the 1st will contain 44 & the 2nd byte will be 1 according to the DCBA data storage format.
Logic-1:
First we have to extract the base address of x by type casting it i.e. (unsigned char *). Then by using de-referencing operator ‘*’, we can get the value of the low or 1st byte. If that is found to be equal to 1 & the value of the high / 2nd byte obtained in the same method by adding 1 to the base address of x is equal to 44, then the system is “Big-endian” or else it is “Little-endian”.
Logic-2:
In this logic, we have taken the union called xxx with 2 members – one is an integer x and the other is an array of 2 unsigned characters i.e. ch [2]. As in a union all data members share the same memory location, as the largest data member of the union, hence in this case, ch[0] will be accessing the 1st/low byte of x and ch[1] will be accessing the 2nd/high byte of x. So we can compare p.ch[0] with 1 & p.ch[1] with 44. If it matches, its “Big-endian”, else “little endian”.

10. Write a C-program which does the addition of two integers without using ‘+’ operator.

Answer:
Logic-1:
c = a – (-b);
as a+b is equivalent to a – (-b), binary ‘+’ operator is replaced by one unary ‘-‘ & one binary ‘-‘ operator.

Logic-2:
void main ()
{
int a,b,c;
printf (“\n Enter any two numbers”);
scan f (“%d %d”, &a, &b);
asm mov ax, a;
asm mov bx, b;
asm aad ax, bx;
asm mov c, ax;
printf ( “%d”, c);
}

11. Write a C-program to find the smallest of three integers without using any of the comparision operators.

Answer:
void main ()
{
int a,b,c;
printf (“\n Enter three numbers”);
scan f (“%d %d %d”, &a, &b, &c);
if ((a-b) & 32768)
{
if ((a-c) & 32768)
printf (“ %d”, a);
else
printf (“ %d”, c);
}
else
if ((b-c) & 32768)
printf (“ %d”, b);
else
printf (“ %d”, c);
}

Explanation:

When we substract a number from another number we can find out which one is greater than other. If the result is positive then the first number is greater else the second one is greater. This is known by doing a bitwise & operator with the sign bit (i.e. 32768 or 15th bit).

12. Find the maximum & minimum of two numbers in a single line without using any condition & loop.

Answer:
void main ()
{
int a=15, b=10;
printf (“ max = %d, min = %d”, ((a+b) + abs(a-b)) /2, ((a+b) – abs (a-b)) /2);
}

13. What “condition” expression can be used so that the following code snippet will print Hello world.

Answer:
if “condition”
printf (“ Hello”);
else
printf (“ world !”);

Answer:
void main ()
{
if ( ! printf (“Hello”));
printf (“Hello”);
else
printf (“world !”);
}

Explanation:
As we know the printf function displays the string & returns an integer. So here printf (“Hello”) will print Hello as well as return 5. Then !5 is evaluated which is false. As the condition is false world is printed on the screen. By this we can get Hello world on the screen.

14. How to print number from 1 to 100 without using conditional operators.

Answer:
void main ()
{
int i=0;
while (100 – i++)
printf (“ %d”, i);
}

15. WAP to print 100 times “Hello” without using loop & goto statement.

Answer:
void main()
{
show (1, 100);
}
show (int x, int y)
{
if (x>=y)
return;
printf (“\n Hello”);
show (x+1, y);
}
i.e. Recursive function

16. Write the equivalent expression for x%8.

Answer:
x & 7;

Explanation:
x = 21 = 00010101
7 = 00000111
x & 7 = 00000101 (which is 5)

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>