Which function in C is used to flush the output buffer of a stream?
- fflush()
- fprintf()
- fputc()
- fputs()
The fflush() function in C is used to flush the output buffer of a stream, ensuring that any buffered data is written to the file or console.
How can you handle an error when attempting to allocate memory using malloc() in a C program?
- Check if the return value of malloc() is NULL and handle accordingly.
- Use try-catch blocks to handle memory allocation errors.
- Set a flag in case of an error and handle it in a separate error-handling function.
- Call exit() to terminate the program if malloc() fails.
The correct option is "Check if the return value of malloc() is NULL and handle accordingly." Malloc() returns a NULL pointer if it fails to allocate memory. It's crucial to check this return value to handle memory allocation errors gracefully.
How is the size of a union determined in C?
- By the number of members
- By the size of its largest member
- By the size of its smallest member
- By the sum of the sizes of its members
In C, the size of a union is determined by the size of its largest member. If you have a union with multiple members, the size will be at least as large as the largest member's size.
How does using an array of structures impact memory usage in a C program?
- Using an array of structures depends on the size of the array.
- Using an array of structures has no impact on memory usage.
- Using an array of structures increases memory usage.
- Using an array of structures reduces memory usage.
Using an array of structures in a C program increases memory usage because each element of the array holds a complete structure, and memory is allocated for each. This can be more memory-intensive compared to individual structures.
Imagine you are developing a C program to manage a library's book inventory. What data structure would be beneficial for storing information about each book?
- Array
- Binary Search Tree
- Hash Table
- Linked List
A hash table would be efficient for storing information about each book in the library's inventory. It allows for fast retrieval of book details based on a unique key (e.g., book ISBN or title). This data structure provides constant-time average lookup, making it suitable for efficient management of the library's book inventory.
In C, what is the relation between the addresses of consecutive elements of an array of type 'int'?
- The addresses are consecutive with a difference of 1
- The addresses are consecutive with a difference of 2
- The addresses are consecutive with a difference of 4
- The addresses are random and not related
In C, the addresses of consecutive elements of an array of type 'int' are typically consecutive with a difference of 4, as 'int' data type is usually 4 bytes in size. This allows for efficient traversal and indexing.
In a C program that manipulates a large dataset, you observe that the performance is significantly reduced when passing data between functions. How could pointers be used to improve the performance?
- Pointers can be used to create additional datasets.
- Pointers can be used to pass data by reference.
- Pointers can be used to pass data by value.
- Pointers can't improve performance in this scenario.
Pointers can be used to pass data by reference, which avoids the overhead of copying large datasets, improving performance in data manipulation tasks.
What is the purpose of the #pragma directive in a C program?
- It controls compiler-specific behaviors.
- It has no specific purpose in C programming.
- It is used to define custom macros in the code.
- It is used to include external libraries in the code.
The #pragma directive in C is used to control compiler-specific behaviors, such as optimization settings and platform-specific configurations.
In the context of C programming, what is the size of an 'enum' data type?
- 2 bytes
- 4 bytes
- It depends
- Varies
The size of an 'enum' data type varies in C, and it depends on the specific implementation. Typically, it's large enough to represent all the enumeration constants.
The ________ function in C is used to search for the first occurrence of a character in a string.
- strchr()
- strnchr()
- strsearch()
- strstr()
In C, the strchr() function is used to search for the first occurrence of a character in a string. It returns a pointer to the first occurrence or a null pointer if the character is not found. This function is commonly used in C for string manipulation.