What is the advantage of using function pointers in C for implementing callback functions?
- Allows dynamic callback selection
- Enhances code modularity
- Reduces code redundancy
- Simplifies debugging
Function Pointers allow dynamic selection of callback functions in C, which is useful in scenarios where you need to change callbacks at runtime without modifying the main code. It reduces code redundancy and enhances code modularity, but it might make debugging more challenging due to indirection.
What is the effect of not specifying the size of the array when initializing it with a list of values?
- Compilation error
- The array is initialized with a default size of 10
- The array size is automatically set based on the number of values
- Undefined behavior
When initializing an array with a list of values in C without specifying the size, the array size is automatically set based on the number of values. It is a convenient feature in C known as array initialization without a size specification.
The function ________ is used to read formatted input from the standard input.
- scanf
- printf
- cout
The "scanf" function is used in C and C++ to read formatted input from the standard input (usually the keyboard). It allows you to input and store values in variables while specifying the format in which data should be entered. Options like "print," "printf," and "cout" are used for output, not input.
String literals in C are stored in ________ memory segment.
- Code
- Data
- Heap
- Stack
In C, string literals are stored in the data memory segment. This segment contains constants, such as global and static variables.
The function ________ can be used to determine the size of a file in C.
- size()
- filesize()
- file_size()
- fsize()
The correct option is fsize(). The fsize() function is commonly used in C to determine the size of a file by positioning the file pointer at the end and then retrieving the position.
The function ________ is used in C to allocate memory for an array of specified size dynamically.
- free
- malloc
- printf
- scanf
In C, the function 'malloc' is used to allocate memory for an array of specified size dynamically. 'malloc' stands for memory allocation.
In C programming, what is a common use case for having an array of structures?
- Storing data in a linked list.
- Storing data with different structures.
- Storing multiple records of the same type.
- Storing unrelated data types together.
A common use case for having an array of structures is to store multiple records of the same type. This allows you to create collections of related data with the same structure, such as a list of students' information or employee records.
What is the purpose of using pointers to structures in C programming?
- To access structure members
- To create a new structure
- To declare a structure
- To store an integer
Pointers to structures are used to access and manipulate individual members of a structure.
Which looping construct is best suited when the number of iterations is known beforehand?
- do-while
- for
- switch
- while
The 'for' loop is commonly used when the number of iterations is known beforehand, as it allows you to specify the initialization, condition, and iteration steps in a compact manner.
The keyword ________ is used to define a structure in C.
- allocate
- define
- struct
- typedef
In C, the "struct" keyword is used to define a structure. Structures are used to group related data members under a single name.