Please share it with your friends who are preparing for interview , and comment down your suggestions , or any mistake in the post.
code snippets are hosted on ideone.com. you can run code there as well.
1) modulo operator ( % ) , shift operators ( >> , << ) cannot be used with floats & double , it generates error.
2) Negative integers on right-hand side is undefined behavior in the C language. Means doing "b = a << -1 ; doesn't generate error but behavior is undefined in C.
3) divide ( / ) by 2 and right shift ( >> ) by 1 gives same result for positive integers. Means
b = 4;
a = b / 2;
a = b >> 1;
will give same results.
4) Multiply ( * ) by 2 and left shift ( << ) by 1 gives same result for positive integers. Means
b = 4;
a = b * 2;
a = b << 1;
will give same results.
5) Using negative numbers as index of array. This is legal and will not genrate error.
int a[5] = {1,2,3,4,5}
doing printf( "%d" , a[-1] ) works.
so suppose a[0] address is 0x4000 then a[-1] will access 0x3FFF.
In this kind of tricky questions always remember ARRAY ARITHMATIC IS SIMLAR TO POINTER ARITHMATIC.
READ : pointer in c by Yashwant Kanetkar
6) Everything is Pass-By-Value in C => even in case of array a pointer is passed by value.
Read this post : < https://denniskubes.com/2012/08/20/is-c-pass-by-value-or-reference/ >
few pointers to remember :
- structures in C is pass by value i.e entire structure will be copied to the called function.
- in case of array only pointer of first element is passed. so using that pointer inside called function will modify the actual array:
7) Questions related to structure , Union , enums, bitfields , packed , unpacked structure.
* interesting question regarding structure member initialization :
* what is structure padding , and why it is done ?
try to run following code on your given compiler and see output of different structure.
also read this article and even comments : < https://www.geeksforgeeks.org/structure-member-alignment-padding-and-data-packing/ >
8) Bit fields questions :
< https://stackoverflow.com/questions/45532622/how-do-the-bit-fields-work-exactly-in-these-c-programs >
check out size of this code
9) A tricky switch case question :
10 ) Read enum vs #define vs static const.
* what is default size of enum - > some embedded compiler uses adaptive approach for enum i.e if the values can be fit in 8-bit ( i..e 0 to 255 ) then it will be 8-bit. But mostly it will int.
< https://stackoverflow.com/questions/1674032/static-const-vs-define-vs-enum/1674040 >
11 ) Inline function vs Macro definitions.
12) bitwise AND vs logical AND ( i know it's basic but sometimes you just lose it ):
13) switch case vs if ... else ( one of the hot favorite one ):
< https://www.geeksforgeeks.org/switch-vs-else/ >
< https://stackoverflow.com/questions/767821/is-else-if-faster-than-switch-case/767849 >
try to compile and see disassembly of following code in your compiler:
### you can use godbolt.org for that if you don't have compiler installed ###
14) shallow copy vs deep copy in case of structures in c.
15) what is practical use of counting semaphore :
< https://stackoverflow.com/questions/26217282/the-usage-case-of-counting-semaphore >
16) binary semaphore vs mutex :
< https://stackoverflow.com/questions/62814/difference-between-binary-semaphore-and-mutex >
mutex is well suited in thread.
and binary semaphore is well suited with processes
17) What is size of integer in C ? and is default integer signed or unsigned ?
=> One should not answer 4 byte immediately. Size of integer depends on processor architecture and compiler. For many16-bit devices and 8-bit 8051 , size of integer is 2 bytes whereas for for 32-bit devices size of integer is mostly 4 byte.
similar way a plain => int x; could be unsigned or signed. This completely depends on compiler default. and they should not be relied upon.
To keep the code always portable one should not use ints , instead uint8_t , uint16_t and uint32_t should be used for unsigned 8-bit , 16-bit and 32-bits.
similarly int8_t , int16_t and int32_t should be used for signed 8-bit , 16-bit and 32-bits.
Definition for this is provided in <stdint.h> file in C.
You can have look at this file here : < https://sites.uclouvain.be/SystInfo/usr/include/stdint.h.html >
*** this one is also one must read with the introduction of 64-bit machines :
< https://stackoverflow.com/questions/384502/what-is-the-bit-size-of-long-on-64-bit-windows >
18) Questions related to little Endian , big Endian. ( you can be asked lot of coding question around this topic as well ).
19) Reentrant function in c , and rules that makes a function reentrant.
20) Basic bit twiddling/manipulation questions.
read this post : < https://stackoverflow.com/questions/47981/how-do-you-set-clear-and-toggle-a-single-bit#:~:text=Use%20the%20bitwise%20OR%20operator,n%20th%20bit%20of%20number%20 >
21) Rotate the number :
< https://www.geeksforgeeks.org/rotate-bits-of-an-integer >
22) Count number of set bit in a number :
< https://www.geeksforgeeks.org/count-set-bits-in-an-integer >
23) Count total bits in a number
< https://www.geeksforgeeks.org/count-total-bits-number >
24) Insert N number of bits into M number of bits , with i starting location and j end location :
M : 1 000 0101
N : 110
i: 3, j: 5
N = N<<i;
M = M | N ;
Result: 1 011 0101
25) count position of first 0 bit in a number:
count = 0
while( ~(Num >>= count) & 0x01)
{
count++;
}
return count
26) Difference between const char *p, char * const p and const char * const p
28) implement a bubble sort ( even though bubble sort is very inefficient but this questions is widely asked at beginner level to check basic programming skills ):
< https://www.geeksforgeeks.org/bubble-sort >
29) Must go through on geeksforgeeks :
< https://www.geeksforgeeks.org/c-cpp-tricky-programs/ >
< https://www.geeksforgeeks.org/commonly-asked-c-programming-interview-questions-set-1/ >
< https://www.geeksforgeeks.org/commonly-asked-c-programming-interview-questions-set-2/ >
< https://www.geeksforgeeks.org/commonly-asked-c-programming-interview-questions-set-3/ >
30) Highly recommended questions from InterviewBit :
< Top C Interview Questions (2021) - InterviewBit >
Very much informative. Thank you sir!
ReplyDeleteInformative Loved it sir,mind blowing facts explained in your blogs .
ReplyDeleteVery informative !!! great collection
ReplyDelete