Questions: C Programming
Back to Topics List

C Programming Questions

Page 2 of 3 (Displaying Questions 101 – 200 of 249 Total)

101. What is the difference between character constant and string literal?

Show Answer

A character constant (e.g., 'A') is a single character enclosed in single quotes. A string literal (e.g., "A") is an array of characters enclosed in double quotes and automatically terminated with a null character (\0).

Added: Nov 30, 2025

102. What is the role of the `switch` statement?

Show Answer

The `switch` statement is a selection control mechanism used to allow the value of a variable to change the control flow of program execution via multiple cases.

Added: Nov 30, 2025

103. What is an array index (or subscript)?

Show Answer

An array index is a number used to specify a particular element in an array. In C, array indexing always starts at 0.

Added: Nov 30, 2025

104. When passing an array to a function, what is actually passed?

Show Answer

The base address (memory location) of the first element of the array is passed.

Added: Nov 30, 2025

105. What is a pointer-to-a-pointer (double pointer)?

Show Answer

A pointer-to-a-pointer is a variable that stores the memory address of another pointer variable.

Added: Nov 30, 2025

106. What is the file opening mode "r+" in `fopen()`?

Show Answer

The "r+" mode opens a file for both reading and writing. The file must already exist.

Added: Nov 30, 2025

107. What does the `fseek()` function do?

Show Answer

The `fseek()` function is used to set the file position indicator to a specified offset from the beginning, current position, or end of the file.

Added: Nov 30, 2025

108. What is the return type of the `malloc()` function?

Show Answer

The `malloc()` function returns a `void *` pointer, which is a generic pointer that can be cast to any other data type.

Added: Nov 30, 2025

109. What does the term "undefined behavior" mean in C?

Show Answer

It means the result of executing a piece of code is unpredictable and not specified by the C standard, often leading to crashes or security issues.

Added: Nov 30, 2025

110. What is stack overflow?

Show Answer

Stack overflow occurs when a program uses more memory space on the call stack than is available, often caused by excessive recursion or very large local variables.

Added: Nov 30, 2025

111. What is the primary function of the `string.h` library?

Show Answer

It provides a set of standard functions for manipulating C-style strings (null-terminated arrays of characters).

Added: Nov 30, 2025

112. What is the purpose of the `errno` variable?

Show Answer

`errno` is a global variable used to store the error code for the last system or library function call that failed.

Added: Nov 30, 2025

113. What is the difference between an actual parameter and a formal parameter?

Show Answer

An actual parameter is the argument used in the function call. A formal parameter is the variable declared in the function definition to receive the value.

Added: Nov 30, 2025

114. What is the use of the `register` keyword in function parameters?

Show Answer

It suggests that the passed-in parameter should be stored in a CPU register for faster access.

Added: Nov 30, 2025

115. What is the primary difference between `++i` (pre-increment) and `i++` (post-increment)?

Show Answer

Pre-increment (`++i`) increments the value of `i` first, then uses it. Post-increment (`i++`) uses the current value of `i`, then increments it.

Added: Nov 30, 2025

116. Can you use the `switch` statement with floating-point numbers?

Show Answer

No, the expression in a `switch` statement must evaluate to an integer or a character type.

Added: Nov 30, 2025

117. What is a variable initialization?

Show Answer

Variable initialization is the process of assigning an initial (starting) value to a variable at the time of its declaration.

Added: Nov 30, 2025

118. How can you calculate the length of a string without using the `strlen()` function?

Show Answer

By iterating through the string array until the null character (\0) is encountered, counting the characters along the way.

Added: Nov 30, 2025

119. What is the purpose of the `FILE *fp` structure?

Show Answer

It is the standard C structure that holds all the control information required for file access, such as the current position pointer.

Added: Nov 30, 2025

120. What is the function of the `feof()` function?

Show Answer

The `feof()` function is used to check for the end-of-file indicator on a specified stream (file).

Added: Nov 30, 2025

121. What is the difference between character constant and integer constant?

Show Answer

A character constant is a single character in single quotes. An integer constant is a whole number without any quotes or decimal points.

Added: Nov 30, 2025

122. What does the `realloc()` function do?

Show Answer

The `realloc()` function changes the size of the previously allocated memory block while preserving the existing content.

Added: Nov 30, 2025

123. What is the purpose of the `restrict` keyword (C99)?

Show Answer

The `restrict` keyword is a type qualifier used with pointers to tell the compiler that the pointer is the only way to access the memory block, allowing for optimization.

Added: Nov 30, 2025

124. What is a function pointer in C?

Show Answer

A function pointer is a variable that stores the memory address of a function, allowing that function to be called dynamically.

Added: Nov 30, 2025

125. How do you declare a pointer to a function that takes an integer and returns a float?

Show Answer

float (*func_ptr)(int);

Added: Nov 30, 2025

126. Explain the difference between `const int *p` and `int *const p`.

Show Answer

`const int *p` is a pointer to a constant integer (the value pointed to cannot change). `int *const p` is a constant pointer to an integer (the pointer itself cannot change to point elsewhere).

Added: Nov 30, 2025

127. What is the type of a two-dimensional array name when used in a function parameter?

Show Answer

When passed to a function, a 2D array decays into a pointer to an array of its column size (e.g., `int (*arr_ptr)[N]`).

Added: Nov 30, 2025

128. What is a memory leak, and how do you prevent it?

Show Answer

A memory leak occurs when a program allocates memory dynamically but fails to free it after use. It is prevented by ensuring every `malloc()`/`calloc()`/`realloc()` call is matched by a `free()` call.

Added: Nov 30, 2025

129. What is the primary difference between a macro and an inline function?

Show Answer

A macro is processed by the preprocessor (textual substitution) and incurs no function call overhead but has no type checking. An inline function is handled by the compiler, provides type checking, and tries to avoid function call overhead.

Added: Nov 30, 2025

130. What does the `#define` preprocessor directive do?

Show Answer

It is used to define a macro, which substitutes a text with a replacement token string throughout the source code before compilation.

Added: Nov 30, 2025

131. What is the purpose of the `#undef` preprocessor directive?

Show Answer

It is used to undefine a previously defined macro.

Added: Nov 30, 2025

132. What is the use of the `#ifdef` and `#ifndef` directives?

Show Answer

They are used for conditional compilation. `#ifdef` checks if a macro is defined, and `#ifndef` checks if a macro is not defined.

Added: Nov 30, 2025

133. Explain the macro stringizing operator (`#`).

Show Answer

The `#` operator, when used in a macro definition, converts the macro argument into a string literal.

Added: Nov 30, 2025

134. Explain the macro token-pasting operator (`##`).

Show Answer

The `##` operator is used to concatenate two tokens in a macro definition, creating a single token.

Added: Nov 30, 2025

135. What is the purpose of the `#pragma once` directive?

Show Answer

It is a non-standard but widely supported directive used in header files to ensure that the file is included only once in a single compilation.

Added: Nov 30, 2025

136. What is the purpose of the `volatile` keyword?

Show Answer

The `volatile` keyword tells the compiler that a variable's value may change at any time by external factors (e.g., OS, hardware, or concurrent threads), preventing the compiler from performing certain optimizations.

Added: Nov 30, 2025

137. In what scenarios is the `volatile` keyword necessary?

Show Answer

It is necessary when dealing with memory-mapped I/O, global variables modified by interrupt service routines, and variables shared between threads.

Added: Nov 30, 2025

138. What is the purpose of the `restrict` keyword (C99)?

Show Answer

The `restrict` keyword is a type qualifier for pointers that tells the compiler that the memory pointed to by this pointer is not accessed by any other pointer, enabling more aggressive optimization.

Added: Nov 30, 2025

139. How does a self-referential structure define a linked list node?

Show Answer

A self-referential structure contains at least one member that is a pointer to the structure type itself, linking one node to the next.

Added: Nov 30, 2025

140. What is a bit field in a structure?

Show Answer

A bit field allows you to specify the exact number of bits that a structure member should occupy, enabling compact storage.

Added: Nov 30, 2025

141. What is structure padding?

Show Answer

Structure padding is the insertion of uninitialized bytes into a structure by the compiler to ensure that the members are aligned properly in memory, which optimizes access speed.

Added: Nov 30, 2025

142. What is endianness, and why is it relevant in C?

Show Answer

Endianness refers to the order in which bytes of a multi-byte data type are stored in memory (Little-Endian: least significant byte first; Big-Endian: most significant byte first). It is relevant when transferring binary data between different systems.

Added: Nov 30, 2025

143. What is the primary difference between `signed` and `unsigned` integer types?

Show Answer

`signed` integers can store both positive and negative values (using one bit for the sign). `unsigned` integers can only store positive values and use all bits to represent magnitude, effectively doubling the positive range.

Added: Nov 30, 2025

144. How do you check if dynamic memory allocation (`malloc`) was successful?

Show Answer

By checking if the returned pointer is `NULL`. If `malloc` fails to allocate the requested memory, it returns `NULL`.

Added: Nov 30, 2025

145. What is the role of the `atexit()` function?

Show Answer

The `atexit()` function registers a function that is automatically called when the program terminates normally (either by reaching the end of `main()` or by calling `exit()`).

Added: Nov 30, 2025

146. What does the `perror()` function do?

Show Answer

The `perror()` function displays a descriptive error message to `stderr`, typically based on the current value of the global variable `errno`.

Added: Nov 30, 2025

147. What is the difference between `exit()` and `_exit()`?

Show Answer

`exit()` performs cleanup operations like calling functions registered with `atexit()` and flushing output buffers. `_exit()` (or `_Exit()`) terminates immediately without any cleanup.

Added: Nov 30, 2025

148. How is the return value of `main()` used?

Show Answer

The return value of `main()` (typically 0 for success, non-zero for failure) is passed back to the operating system as the program's exit status.

Added: Nov 30, 2025

149. What is the purpose of the `time()` function in `<time.h>`?

Show Answer

The `time()` function returns the current calendar time as a value of type `time_t` (typically the number of seconds since the Epoch, January 1, 1970).

Added: Nov 30, 2025

150. What is the function of `sqrt()` in `<math.h>`?

Show Answer

The `sqrt()` function is used to calculate the square root of a number.

Added: Nov 30, 2025

151. How do you perform a bitwise left shift, and what is its effect?

Show Answer

The left shift operator is `<<`. It shifts all bits of a number to the left by a specified number of positions, effectively multiplying the number by 2 for each position shifted.

Added: Nov 30, 2025

152. What is the primary use of the bitwise AND operator (`&`)?

Show Answer

The bitwise AND is commonly used for **masking**—selectively clearing bits or checking the status of specific bits in a value.

Added: Nov 30, 2025

153. What is the primary use of the bitwise OR operator (`|`)?

Show Answer

The bitwise OR is commonly used for **setting** specific bits in a value.

Added: Nov 30, 2025

154. What is the difference between `static` linkage and `external` linkage?

Show Answer

Static linkage means a symbol (variable/function) is only visible within its compilation unit (file). External linkage means a symbol can be referenced from other compilation units.

Added: Nov 30, 2025

155. When passing a large structure to a function, what is the best practice?

Show Answer

It is best practice to pass the structure by **reference** (using a pointer) rather than by value, to avoid copying the entire structure onto the stack, saving time and memory.

Added: Nov 30, 2025

156. What is the role of the `setjmp()` and `longjmp()` functions?

Show Answer

These functions provide a non-local jump mechanism in C, allowing the program flow to jump from one function call back to a previously saved state, often used for centralized error handling.

Added: Nov 30, 2025

157. What is the difference between `fgetc()` and `getc()`?

Show Answer

In C89, `getc()` is typically implemented as a macro and may evaluate its arguments multiple times, while `fgetc()` is guaranteed to be a function call. Both read a character from a specified stream.

Added: Nov 30, 2025

158. What is the primary purpose of the `sscanf()` function?

Show Answer

The `sscanf()` function is used to read formatted input from a **string** rather than from the standard input stream.

Added: Nov 30, 2025

159. What is the primary purpose of the `sprintf()` function?

Show Answer

The `sprintf()` function is used to write formatted output to a **string** rather than to the standard output stream.

Added: Nov 30, 2025

160. What does the term "re-entrancy" mean in the context of C functions?

Show Answer

A re-entrant function is one that can be safely interrupted during execution and called again by a concurrent process or thread without causing data corruption.

Added: Nov 30, 2025

161. What is the meaning of the file opening mode "rb" in `fopen()`?

Show Answer

"rb" stands for **read binary**. It opens a file for reading in binary mode, without character translation.

Added: Nov 30, 2025

162. How do you check for end-of-file (EOF) when reading characters from a file?

Show Answer

By checking the return value of the read function (like `fgetc` or `getc`). It returns the constant `EOF` (End Of File) upon failure or reaching the end of the file.

Added: Nov 30, 2025

163. What is a common error when using `scanf()` for strings?

Show Answer

Forgetting to include the address-of operator (`&`) is a common error for non-string types. For strings (`char[]`), the array name itself already represents the address, so `&` should be omitted.

Added: Nov 30, 2025

164. What is the order of compilation stages in C?

Show Answer

The typical order is: Preprocessing -> Compilation -> Assembly -> Linking.

Added: Nov 30, 2025

165. What is the purpose of the linker in the C compilation process?

Show Answer

The linker combines the object files generated by the assembler with necessary library routines (like `printf` and `scanf`) to produce the final executable program.

Added: Nov 30, 2025

166. What is the difference between `extern` and `static` keywords in terms of scope?

Show Answer

`extern` extends the scope of a global variable or function to other files. `static` restricts the scope of a global variable or function to the current file only.

Added: Nov 30, 2025

167. What is a null statement in C?

Show Answer

A null statement is a statement that does nothing, consisting only of a semicolon (`;`). It is often used where syntax requires a statement but no action is needed, such as in an empty loop body.

Added: Nov 30, 2025

168. What does the `break` statement do when used within a `switch` statement?

Show Answer

It terminates the `switch` statement, causing program execution to proceed with the statement immediately following the `switch` body.

Added: Nov 30, 2025

169. Why is it important to use `sizeof()` when working with memory allocation functions like `malloc()`?

Show Answer

It ensures that the correct number of bytes, which can vary depending on the data type and system architecture, is requested for allocation.

Added: Nov 30, 2025

170. How can you access members of a structure using a pointer?

Show Answer

By using the arrow operator (`->`), which is shorthand for dereferencing the pointer and then using the dot operator (e.g., `ptr->member` is the same as `(*ptr).member`).

Added: Nov 30, 2025

171. What is the purpose of the `va_list` type and related macros (`va_start`, `va_arg`, `va_end`)?

Show Answer

They are used to implement functions that take a **variable number of arguments** (variadic functions), like `printf`.

Added: Nov 30, 2025

172. What is the difference between `0` and `\0` in C?

Show Answer

`0` is the integer value zero. `\0` is the null character, which has an ASCII value of 0, and is used to terminate strings.

Added: Nov 30, 2025

173. What is the `main` function prototype for handling environment variables?

Show Answer

`int main(int argc, char *argv[], char *envp[])` where `envp` is an array of strings representing environment variables.

Added: Nov 30, 2025

174. What is the primary danger of using the `goto` statement?

Show Answer

It can lead to code that is difficult to read, debug, and maintain, often resulting in "spaghetti code."

Added: Nov 30, 2025

175. How does a `do-while` loop differ from a `while` loop?

Show Answer

The `do-while` loop is an **exit-controlled** loop, meaning the condition is checked after the loop body executes, guaranteeing execution at least once. The `while` loop is entry-controlled.

Added: Nov 30, 2025

176. What are compound literals (C99)?

Show Answer

Compound literals are an expression that creates an unnamed object (like an array or structure) with a specified type and initialization, often used to pass anonymous values to functions.

Added: Nov 30, 2025

177. What is the `register` keyword best suited for in C?

Show Answer

It is best suited for variables that are accessed very frequently within a small block of code, typically loop counters.

Added: Nov 30, 2025

178. Explain the purpose of the `restrict` keyword when passing arrays to functions.

Show Answer

It informs the compiler that the two pointers passed will not point to overlapping memory regions, enabling optimization based on this guarantee.

Added: Nov 30, 2025

179. What is the primary difference between a `union` and an `enum`?

Show Answer

A `union` is a user-defined type where members share memory. An `enum` is a set of named integer constants.

Added: Nov 30, 2025

180. When should you use `calloc()` instead of `malloc()`?

Show Answer

Use `calloc()` when you need the allocated memory block to be initialized to zero, which is useful for arrays or structures that rely on an initial zero state.

Added: Nov 30, 2025

181. What is the significance of the `_IOFBF`, `_IOLBF`, and `_IONBF` constants in `<stdio.h>`?

Show Answer

They are used with `setvbuf()` to control file stream buffering: Fully Buffered, Line Buffered, and Unbuffered, respectively.

Added: Nov 30, 2025

182. How do you correctly read a string with spaces using `scanf()`?

Show Answer

By using a format specifier like `%[^\n]`, which tells `scanf()` to read all characters up until a newline character.

Added: Nov 30, 2025

183. What is the `strerror()` function used for?

Show Answer

The `strerror()` function takes an integer error code (like the value of `errno`) and returns a pointer to the corresponding, human-readable error message string.

Added: Nov 30, 2025

184. What is the effect of dividing two integers in C (integer division)?

Show Answer

Integer division truncates the result towards zero, discarding any fractional part. For example, `7 / 3` is 2, and `-7 / 3` is -2.

Added: Nov 30, 2025

185. What is type promotion in C?

Show Answer

Type promotion is the automatic conversion of smaller integer types (like `char` and `short`) to `int` or `unsigned int` when they are used in expressions.

Added: Nov 30, 2025

186. What does it mean for a C standard library function to be "thread-safe"?

Show Answer

A thread-safe function can be called simultaneously from multiple threads without causing race conditions or data corruption.

Added: Nov 30, 2025

187. What are the primary modes for opening a file in binary format?

Show Answer

The primary modes are "rb" (read binary), "wb" (write binary), and "ab" (append binary).

Added: Nov 30, 2025

188. What is the purpose of the `rewind()` function in file handling?

Show Answer

The `rewind()` function sets the file position indicator back to the beginning of the specified stream (file).

Added: Nov 30, 2025

189. How do you declare an array of function pointers?

Show Answer

You declare it by first defining the function pointer type using `typedef`, and then declaring an array of that type (e.g., `typedef int (*func_ptr)(void); func_ptr array_of_funcs[10];`).

Added: Nov 30, 2025

190. What is the result of using a loop counter that is declared as `unsigned int` and decremented to below zero?

Show Answer

The loop will likely become infinite because the unsigned integer will "wrap around" to the largest positive value (e.g., `UINT_MAX`) instead of becoming negative.

Added: Nov 30, 2025

191. What is the significance of the `extern "C"` linkage specification?

Show Answer

It is used in C++ code to tell the compiler to use C-style naming and calling conventions when compiling a function or variable, allowing it to be linked with C object files.

Added: Nov 30, 2025

192. What is the key difference between a function definition and a function declaration?

Show Answer

A function declaration (prototype) specifies the function signature. A function definition provides the actual body (implementation) of the function.

Added: Nov 30, 2025

193. How do you check for buffer overflow in C?

Show Answer

While C does not prevent it, it is checked by carefully managing buffer sizes, using safer string functions (`strncpy`, `fgets`), and boundary checking.

Added: Nov 30, 2025

194. What is the main pitfall of using macros with arguments?

Show Answer

They can lead to unexpected side effects due to repeated evaluation of arguments or issues with operator precedence if the arguments are not fully parenthesized.

Added: Nov 30, 2025

195. What does the `restrict` keyword imply about pointers?

Show Answer

It guarantees to the compiler that the pointer provides the *only* means of accessing the data object pointed to in its scope, enabling more aggressive optimizations.

Added: Nov 30, 2025

196. How can you calculate the number of elements in a statically allocated array?

Show Answer

By using the formula: `sizeof(array) / sizeof(array[0])`.

Added: Nov 30, 2025

197. What is the difference between `NULL` and `(void *)0`?

Show Answer

In C, `NULL` is a macro that expands to an implementation-defined null pointer constant, often `(void *)0` or simply `0`. They are used interchangeably to represent a null pointer.

Added: Nov 30, 2025

198. What are designated initializers (C99)?

Show Answer

They allow you to initialize array or structure members by specifying the index or member name, respectively, rather than relying on order (e.g., `int a[3] = {[2] = 5};`).

Added: Nov 30, 2025

199. What is a compound statement (or block) in C?

Show Answer

A compound statement is a group of zero or more statements enclosed in curly braces (`{}`) that are treated as a single unit.

Added: Nov 30, 2025

200. What is the use of the `longjmp` function?

Show Answer

It is used to restore the program state to a point previously saved by `setjmp`, often used to implement exception-like handling.

Added: Nov 30, 2025