Library Functions in C Programming MCQ Questions and Answers

1. Which library function is used to read a single character from stdin and returns it as an int?
A) getc()
B) getchar_unlocked()
C) getchar()
D) fgets()
Answer: C

2. Which function from <stdio.h> writes formatted output to a string (not to stdout)?
A) printf()
B) puts()
C) fprintf()
D) sprintf()
Answer: D

3. To dynamically allocate memory for an array of 50 integers and initialize all bytes to zero, which function and call is appropriate?
A) malloc(50 * sizeof(int))
B) calloc(50, sizeof(int))
C) realloc(NULL, 50 * sizeof(int))
D) alloca(50 * sizeof(int))
Answer: B

4. Which function converts a null-terminated string to a double (floating point)?
A) atoi()
B) atol()
C) atof()
D) strtol()
Answer: C

5. Which <string.h> function compares two memory blocks for a given number of bytes?
A) strcmp()
B) strncmp()
C) memcmp()
D) strcoll()
Answer: C

6. Which function returns the length (number of characters) of a C string (excluding the null terminator)?
A) sizeof()
B) strlen()
C) strnlen()
D) strlen_s()
Answer: B

7. Which function from <stdlib.h> is used to terminate a program immediately, performing cleanup of open streams?
A) abort()
B) exit()
C) _Exit()
D) terminate()
Answer: B

8. Which <math.h> function returns the smallest integer value greater than or equal to a given double?
A) floor()
B) trunc()
C) round()
D) ceil()
Answer: D

9. Which function copies a null-terminated string from source to destination (dangerous if destination is too small)?
A) strncpy()
B) strcpy()
C) memcpy()
D) stpcpy()
Answer: B

10. Which <ctype.h> function checks whether a character is a digit (0–9)?
A) isalpha()
B) islower()
C) isdigit()
D) isalnum()
Answer: C

11. Which function opens a file and returns a FILE* pointer?
A) file_open()
B) open()
C) fopen()
D) fcreate()
Answer: C

12. What does fseek(stream, 0, SEEK_END) accomplish?
A) Moves to beginning of file
B) Moves to end of file
C) Moves to current position
D) Sets file mode to append
Answer: B

13. Which function writes count bytes from a buffer to a stream and returns number of items written?
A) fprintf()
B) fwrite()
C) fputs()
D) putc()
Answer: B

14. Which function returns the current value of the pseudo-random number generator seeded by srand()?
A) rand_r()
B) srand()
C) rand()
D) random()
Answer: C

15. Which <string.h> function concatenates two strings (appends source to destination)?
A) strncat_s()
B) memcat()
C) strcat()
D) stpcpy()
Answer: C

16. Which function converts a string representing an integer to long int and allows error detection using endptr?
A) atoi()
B) atol()
C) strtol()
D) strtod()
Answer: C

17. Which function returns the absolute value of an integer?
A) fabs()
B) absf()
C) abs()
D) labs()
Answer: C

18. Which function from <stdio.h> reads a line from stdin into a buffer safely with size limit?
A) gets()
B) fgets()
C) scanf(“%s”, buf)
D) gets_s()
Answer: B

19. Which function frees memory previously allocated by malloc, calloc, or realloc?
A) delete()
B) free_all()
C) free()
D) dispose()
Answer: C

20. Which <time.h> function returns calendar time as time_t representing seconds since epoch?
A) localtime()
B) clock()
C) time()
D) mktime()
Answer: C

21. Which function from <stdio.h> reads formatted input from a string?
A) scanf()
B) sscanf()
C) fscanf()
D) gets()
Answer: B

22. Which function from <math.h> computes the square root of a double value?
A) pow(x, 0.5)
B) sqrtf()
C) csqrt()
D) sqrt()
Answer: D

23. Which function returns a pointer to the first occurrence of a character c in a string s?
A) strrchr()
B) strstr()
C) strchr()
D) index()
Answer: C

24. Which <stdlib.h> function reallocates previously allocated memory to a new size?
A) malloc_resize()
B) realloc()
C) cmalloc()
D) adjust()
Answer: B

25. Which function returns 0 if two strings are identical and a non-zero value otherwise?
A) strcmp_s()
B) strncmp()
C) strcmp()
D) strcasecmp()
Answer: C

26. Which <stdio.h> function writes a single character (as an int) to a stream?
A) putchar_unlocked()
B) puts()
C) fputc()
D) putc_unlocked()
Answer: C

27. Which <string.h> function searches for a substring within a string?
A) strchr()
B) strstr()
C) strpbrk()
D) strsep()
Answer: B

28. Which function returns the remainder of division of two double values?
A) fmod()
B) remainder()
C) modf()
D) fmod()
Answer: D

29. Which function is used to change the current working directory in POSIX systems (not standard C)?
A) chdir()
B) setcwd()
C) cwd()
D) chdir()
Answer: D

30. Which <ctype.h> function converts a character to its uppercase equivalent (if possible)?
A) toupper_s()
B) capitalize()
C) toupper()
D) upper()
Answer: C

31. Which function from <stdlib.h> causes abnormal program termination without invoking functions registered by atexit()?
A) exit()
B) terminate()
C) _Exit()
D) abort()
Answer: C

32. Which <stdio.h> function returns the next character from a given FILE* stream?
A) getc_unlocked()
B) getchar()
C) fgetc()
D) getc()
Answer: C

33. Which <string.h> function copies n characters from source to destination, padding with \0 if source shorter?
A) strcpy()
B) strncpy()
C) memmove()
D) stpncpy()
Answer: B

34. Which function from <errno.h> provides the numeric error code from the last library call?
A) perror()
B) strerror()
C) errno (macro/global)
D) error()
Answer: C

35. Which <stdio.h> function prints a string followed by a newline to stdout?
A) printf(“\n”)
B) fputs()
C) puts()
D) puts_s()
Answer: C

36. Which function returns a pointer to the final component in a pathname (POSIX basename() — not strictly C standard but common)?
A) dirname()
B) filename()
C) basename()
D) path_leaf()
Answer: C

37. Which <math.h> function returns the value of x raised to the power y for doubles?
A) powf()
B) pow()
C) exp()
D) powl()
Answer: B

38. Which function compares two strings ignoring case (POSIX / GNU extension)?
A) strcmp()
B) strcasecmp()
C) stricmp()
D) strncasecmp()
Answer: B

39. Which <stdlib.h> function converts a string to a floating-point number (double) with end-pointer support?
A) atof()
B) strtod()
C) strtof()
D) stod()
Answer: B

40. Which function copies overlapping memory areas safely?
A) memcpy()
B) strcpy()
C) memmove()
D) bcopy()
Answer: C

41. Which function prints an error message prefixed by the user string and the textual description of errno?
A) fprintf(stderr, …)
B) strerror()
C) perror()
D) err()
Answer: C

42. Which <time.h> function converts time_t to a broken-down struct tm in local time?
A) gmtime()
B) localtime()
C) ctime()
D) asctime()
Answer: B

43. Which function from <stdlib.h> returns the integer value of the environment variable named by the string (POSIX getenv)?
A) env()
B) setenv()
C) getenv()
D) putenv()
Answer: C

44. Which function computes the fractional and integer parts of a double, storing the integer part in a pointer argument?
A) modf()
B) modf()
C) floor()
D) frexp()
Answer: B

45. Which <stdio.h> function moves the file position to a specific offset and can be used to determine file size when combined with ftell()?
A) fgetpos()
B) rewind()
C) fseek()
D) fresize()
Answer: C

46. Which <stdlib.h> function sorts an array using a user-provided comparison function?
A) qsort_r()
B) qsort()
C) sort()
D) bsearch()
Answer: B

47. Which function performs binary search on a sorted array and returns a pointer to the matched element?
A) search()
B) qsearch()
C) bsearch()
D) linsearch()
Answer: C

48. Which <stdio.h> function flushes the output buffer of a stream?
A) clearerr()
B) fflush()
C) fsync()
D) flushall()
Answer: B

49. Which function from <string.h> returns a pointer to the first character in s that matches any of the characters in accept?
A) strpbrk()
B) strpbrk()
C) strspn()
D) strcspn()
Answer: B

50. Which function returns the number of characters in the initial segment of s consisting entirely of characters in accept?
A) strcspn()
B) strspn()
C) strpbrk()
D) strtok()
Answer: B

51. Which function breaks a string into tokens based on delimiter characters (not reentrant)?
A) strtok_r()
B) strtok()
C) strsep()
D) strchr()
Answer: B

52. Which function returns a pointer to the last occurrence of a substring in a string (POSIX strrstr() not standard) — but which standard function returns last occurrence of a character?
A) strrstr()
B) strrchr()
C) strchr()
D) strrchr()
Answer: D

53. Which <math.h> function decomposes a floating-point number into mantissa and exponent, returning the mantissa and storing exponent in an int pointer?
A) frexp()
B) ldexp()
C) frexp()
D) modf()
Answer: C

54. Which function converts a character string to an unsigned long integer using a given base?
A) strtol()
B) strtoul()
C) atoi()
D) atol()
Answer: B

55. Which <stdio.h> function rewinds a stream to its beginning and clears error indicators?
A) fseek(stream, 0, SEEK_SET)
B) clearerr()
C) rewind()
D) ftell()
Answer: C

56. Which <stdlib.h> function creates a temporary file name (deprecated and unsafe)?
A) mkstemp()
B) tmpnam()
C) tempnam()
D) tmpfile()
Answer: B

57. Which function opens a temporary binary file and returns a FILE* that will be removed automatically on program termination?
A) tmpnam()
B) tempfile()
C) tmpfile()
D) mktemp()
Answer: C

58. Which <string.h> function returns the length of the initial segment of s which consists entirely of characters not in reject?
A) strspn()
B) strcspn()
C) strpbrk()
D) strtok()
Answer: B

59. Which <ctype.h> function checks whether a character is a printable character including space?
A) isgraph()
B) isspace()
C) isprint()
D) isblank()
Answer: C

60. Which <math.h> function returns the next representable floating-point value after x in the direction of y (C99)?
A) nextafter()
B) nextafterf()
C) nextafter()
D) nexttoward()
Answer: C

61. Which function from <stdio.h> writes formatted data to a FILE* stream?
A) sprintf()
B) fprintf()
C) printf_stream()
D) fwrite()
Answer: B

62. Which <stdlib.h> function converts an integer to a string in C? (Standard C has no direct itoa; which alternative is standard?)
A) itoa()
B) sprintf()
C) ltoa()
D) to_string()
Answer: B

63. Which function returns the position indicator for the given stream as a long integer?
A) fpos()
B) ftell()
C) fgetpos()
D) ftell()
Answer: D

64. Which <stdio.h> function sets the position of the stream using a fpos_t object?
A) fseek()
B) ftell()
C) fsetpos()
D) rewind()
Answer: C

65. Which <math.h> function returns the nearest integer value to x, rounding halfway cases away from zero (C99 round())?
A) trunc()
B) floor()
C) round()
D) ceil()
Answer: C

66. Which <stdlib.h> function replaces the current process image with a new process (POSIX — not C standard)?
A) exec()
B) spawn()
C) no direct standard C function
D) system()
Answer: C

67. Which function from <stdio.h> reads formatted input from a FILE* stream?
A) scanf()
B) sscanf()
C) fscanf()
D) fgets()
Answer: C

68. Which <math.h> function returns the floating-point remainder of dividing two values, but with different rounding than fmod() (C99)?
A) fmod()
B) remainder()
C) modf()
D) remquo()
Answer: B

69. Which <string.h> function tokenizes a string but is reentrant (thread-safe)?
A) strtok()
B) strtok_r()
C) strsep()
D) strtok_s()
Answer: B

70. Which function returns the string representation of local time as a human-readable string pointer (not thread-safe)?
A) ctime()
B) asctime()
C) strftime()
D) localtime()
Answer: A

71. Which function formats date and time into a string according to a format specification?
A) ctime()
B) asctime()
C) strftime()
D) gmtime()
Answer: C

72. Which <stdlib.h> function executes a shell command?
A) system_call()
B) system()
C) exec()
D) popen()
Answer: B

73. Which function from <stdio.h> opens a process by creating a pipe, forking, and invoking the shell?
A) popen()
B) system()
C) fopen()
D) popen()
Answer: D

74. Which <string.h> function returns a pointer to the first occurrence in s of any of the characters in accept?
A) strspn()
B) strpbrk()
C) strcspn()
D) strchr()
Answer: B

75. Which <stdlib.h> function sets the value of errno to zero and returns a pointer to the environment list (POSIX getenv counterpart)? (Which standard function retrieves the environment variable — repeated check)
A) getenv()
B) setenv()
C) putenv()
D) getenv()
Answer: D

76. Which function from <math.h> returns the exponent part of a floating point number as an integer and the mantissa in return (C99 frexp)?
A) ldexp()
B) frexp()
C) ilogb()
D) scalbn()
Answer: B

77. Which function computes integer absolute value for long int?
A) abs()
B) labs()
C) llabs()
D) labsf()
Answer: B

78. Which function computes absolute value for long long int?
A) abs()
B) labs()
C) llabs()
D) iabs()
Answer: C

79. Which <string.h> function moves memory and correctly handles overlapping source and destination?
A) memcpy()
B) memset()
C) memmove()
D) memcmp()
Answer: C

80. Which function sets the first n bytes of the block of memory pointed by s to the specified value (interpreted as unsigned char)?
A) memcpy()
B) memset()
C) bzero()
D) memfill()
Answer: B

81. Which function from <stdio.h> reads characters from a file into a buffer until a newline or EOF, and returns the buffer pointer?
A) fgets()
B) fgets()
C) gets()
D) fscanf()
Answer: B

82. Which <stdlib.h> function converts a string to a wide-character string (not standard mbstowcs does this) — choose correct standard function:
A) mbstowcs()
B) wcstombs()
C) mbstowcs()
D) mbtowc()
Answer: C

83. Which function from <wchar.h> converts wide character string to multi-byte string?
A) mbstowcs()
B) wcstombs()
C) mbtowc()
D) wctomb()
Answer: B

84. Which <stdlib.h> function registers a function to be called at normal program termination?
A) onexit()
B) atexit()
C) register()
D) onterminate()
Answer: B

85. Which function from <stdio.h> reads a single line from stdin and automatically allocates a buffer (GNU extension)?
A) getline()
B) fgets()
C) gets()
D) getline()
Answer: D

86. Which <math.h> function returns the mantissa and exponent pair, where the mantissa is returned as double and exponent through pointer (re-check duplicates)?
A) ldexp()
B) frexp()
C) frexp()
D) modf()
Answer: C

87. Which function returns a pointer to a string describing the error number passed (thread-safe alternative uses buffer)?
A) perror()
B) strerror()
C) errno()
D) errstr()
Answer: B

88. Which <stdlib.h> function sorts and uses a comparison function whose prototype expects pointers to elements (re-affirm qsort)?
A) qsort()
B) sort()
C) heapsort()
D) qsort()
Answer: D

89. Which <stdio.h> function writes a formatted string using a va_list argument to stdout?
A) vfprintf()
B) vprintf()
C) vprintf()
D) vsprintf()
Answer: C

90. Which function writes formatted output to a string using a va_list argument (dangerous if buffer too small)?
A) vsprintf()
B) vsprintf()
C) vfprintf()
D) vprintf()
Answer: B

91. Which function is used to convert a double to a string using specified format into a user-provided buffer with va_list (safer variant exists)?
A) vsprintf()
B) vsnprintf()
C) snprintf()
D) sprintf()
Answer: B

92. Which function copies a C-string into another buffer and guarantees null-termination (C11 optional strcpy_s exists), choose safe standard alternative:
A) strcpy()
B) strncpy()
C) strncpy()
D) strcpy_s()
Answer: C

93. Which <math.h> function scales a floating-point number by an integral power of two?
A) frexp()
B) ldexp()
C) scalbn()
D) scalbln()
Answer: B

94. Which function removes leading and trailing whitespace from a string in standard C?
A) strtrim()
B) trim()
C) strip()
D) No single standard library function
Answer: D

95. Which <ctype.h> function checks for printable characters except space (graphical characters)?
A) isprint()
B) isgraph()
C) isblank()
D) isspace()
Answer: B

96. Which function converts a character string to an integer with base detection and provides end pointer?
A) atoi()
B) strtol()
C) atol()
D) stoi()
Answer: B

97. Which <math.h> function returns the nearest integer toward zero?
A) ceil()
B) round()
C) trunc()
D) floor()
Answer: C

98. Which function from <stdlib.h> sets the program’s locale for categories like numeric and time?
A) setlocale()
B) locale()
C) setlocale()
D) uselocale()
Answer: C

99. Which <stdio.h> function clears the error indicator for the given stream?
A) clearerr()
B) feof()
C) clearerr()
D) ferror()
Answer: C

100. Which function from <stdlib.h> returns a pseudo-random number in a reentrant way given state pointer (POSIX rand_r)?
A) rand()
B) rand_r()
C) random()
D) srand()
Answer: B