Interview Questions
C Library Functions · Study material
C Library Functions – Interview Questions
1. What is the difference between a header file and a library?
A header holds declarations used by the compiler; the library holds compiled code used by the linker.
2. What does strcmp return?
A negative value if the first string sorts before the second, 0 if they are equal, a positive value otherwise.
3. memcpy vs memmove?
memmove handles overlapping source and destination; memcpy does not (undefined behaviour if they overlap) but may be faster.
4. What does strlen return?
The number of characters before the terminating '\0', as size_t.
5. How does qsort know how to compare elements?
You pass a comparator int cmp(const void *, const void *) that returns negative, zero or positive.
6. Why call srand()?
rand produces the same sequence for the same seed. srand(time(NULL)) seeds it differently on each run; calling it once is enough.
7. Is strtok thread-safe?
No – it keeps hidden state between calls and modifies the string. Use strtok_r (POSIX) or strtok_s (C11 Annex K) where available.
8. Why should ctype arguments be cast to unsigned char?
The functions accept only unsigned char values and EOF; a negative plain char (e.g. a UTF-8 byte) is undefined behaviour.
9. What is the difference between abs and fabs?
abs (stdlib.h) works on int; fabs (math.h) on double. labs and llabs handle long and long long.
10. What is the problem with strncpy?
If the source is at least n characters long it does not write a '\0', and it pads with zeros otherwise.
11. What does strftime do?
Formats a struct tm into a string using conversion codes like %Y, %m, %d, %H and %A.
12. Is main a library function?
No. It is defined by the programmer and called by the C runtime's startup code.