Saturday, 12 December 2020

C interview questions - Part 3

 1) Difference between memcpy , memmove and memset :

All operations are done for memory copy. 

In case of memmove memory overlapping will be detected and adjustments will be made.

Behaviour of memcpy with overlapping is undefined. Not that it is undefined. I have personally tried this with different compilers and you cannot predict outpur of memcpy with overlapping.

memset is used to initialise memory with same integer.

see below example try it on different compiler.


2) NULL pointer in C :

NULL pointer in C is mostly defined as (void*)0; as it can be assigned to any other valid pointer type.

dereferancing NULL pointer can create system crash.

Size of NULL pointer is similar to any other pointer.

Try below program on Code Blocks + MinGW.

ref : < https://www.geeksforgeeks.org/few-bytes-on-null-pointer-in-c/


3) What are various way to intialise all elements of auto array with zero :


4) How to find number of element in an array


5) what is difference between strcpy() and strcat() function in C ?

They do different things. Use strcpy() if you want to copy a string and strcat() when you need to append a string to another one. Of course, you can use strcpy() instead of strcat() if you want (and don't mind the time needed for an additional function call to find the end of the first string) but there is no additional inherent safety in using one or the other.

ref : < https://bytes.com/topic/c/answers/216438-difference-between-strcpy-strcat >

proto types:

char* strcpy(char* destination, const char* source);

char *strcat(char *dest, const char *src)

ref < https://www.tutorialspoint.com/c_standard_library/c_function_strcpy.htm >

ref < https://www.tutorialspoint.com/c_standard_library/c_function_strcat.htm >


6) write a program to find if string is rotation of other string 

I have tried to write my copy with the help of this geek for geek article.

https://www.geeksforgeeks.org/a-program-to-check-if-strings-are-rotations-of-each-other/ >


7) write a program to find missing bracket in a string.

ref : < https://www.geeksforgeeks.org/check-for-balanced-parentheses-in-an-expression/ >

we will try to write this program in C and C++.

As C++ have readymade STL availible for stack.

C++ example

I have made little bit modification to make it little easy.

C example

The difference here is we do not have STL support in C , so we have to implement our out stack , so this can be a seprate question " implement stack in C". I have changed few names in geeksforgeeks implementation to make it more easy to understand.

8) Why mostly double pointers are used while implementing "linked list" or "stack" in C ?


9) why data alignment is required in C programming ?

we know that on 32-bit machines char are aligned on 1 byte boundry , shor int on 2 byte boundry , and long int on 4 byte boundry.


10) What is IEEE 754 float numbers and what are disadvantage of them and what are alternatives ??


11) why use of goto statement is discouraged in C language.


12) why multiple return statement in a single function is discouraged in C language.


13) 2 diamentional arrays in C language.


14) why int should not be used as bool in C language.


15) Swap all charaters in string ( like first with last and go on ) in C code :


16) Hamming code implementation in C :

https://www.geeksforgeeks.org/hamming-code-implementation-in-c-cpp/ >


17) write a code to caluculate redundancy from 3 sets of bit :

 




No comments:

Post a Comment