Question: What is a structure in C and how to declare and access members of a structure?
|
Answer: A structure in C is a user-defined data type that groups related data items under a single name. It is declared using the struct keyword. Example:
struct Person {
char name[20];
int age;
};
struct Person p1;
p1.age = 25;
strcpy(p1.name, "John");
|
Question: What is the purpose of the 'const' keyword in C?
|
Answer: The 'const' keyword is used to declare constants in C.
It indicates that the value of the variable cannot be modified after initialization.
|
Question: What are the function pointers in C and how are they used?
|
Answer: Function pointers in C store addresses of functions.
They allow functions to be passed as arguments to other functions or stored in data structures. Example:.
int add(int a, int b) {
return a + b;
}
int (*ptr)(int, int) = &add; // Pointer to a function that takes two ints and returns an int
int result = (*ptr)(3, 4); // Calling the function using the pointer
|
Question: How do you define a constant in C?
|
Answer: Constants in C can be defined using the '#define' preprocessor directive or the 'const' keyword.
Example:
#define PI 3.14159
const int MAX_SIZE = 100;
|
Question: Why 'malloc()' and 'free()' functions are used together in C?
|
Answer: The 'malloc()' function is used to allocate memory dynamically.
While 'free()' is used to deallocate dynamically allocated memory, allowing it to be reused.
|
Question: What is the role of break statement in C?
|
Answer: The 'break' statement is used to exit from a loop or switch statement.
When code encounters 'break', it terminates the loop or switch statement and transfers control to the statement
immediately following the loop or the switch.
|