Question: What is the difference between 'malloc()' and 'calloc()' functions in C?
|
Answer: The 'malloc()' is used to allocate memory dynamically,
while 'calloc()' is used to allocate memory dynamically and initialize all bytes to zero.
|
Question: Can you explain the difference between 'scanf()' and 'fgets()' functions in C regarding input handling?
|
Answer: The 'scanf()' function reads input until it encounters whitespace,
whereas 'fgets()' reads input until it encounters a newline or EOF (end-of-file).
Additionally, 'fgets()' allows specifying the maximum number of characters to read to prevent buffer overflow.
|
Question: What is the purpose of the 'static' keyword in C?
|
Answer: The 'static' keyword is used for variable and function declaration to limit
their scope to the file in which they are declared.
It also makes the variable retain its value between function calls.
|
Question: How do you pass arguments to a function by reference in C?
|
Answer: Arguments can be passed by reference in C by using pointers.
The address of the variable is passed to the function, and modifications made to the
parameter inside the function affect the original variable.
|
Question: What are the storage classes in C?
|
Answer: Storage class can be used for defining variables, functions or parameters. Following are the storage classes in C:
• 'auto': Variables declared within a function are by default auto.
• 'register': Variables are stored in CPU registers for faster access.
• 'static': Variables retain their values between function calls.
• 'extern': Used to declare variables or functions that are defined in other files.
|
Question: How do you dynamically allocate memory in C?
|
Answer: Memory can be dynamically allocated in C using functions like 'malloc()', 'calloc()', or 'realloc()'.
int *ptr;
ptr = (int *)malloc(sizeof(int));
Dynamic memory allocation is very useful for structures like trees that grow dynamically as new data is added.
It's also useful if you don't know how much data you will need to store, and don't want to waste memory.
|
Question: Can you explain the purpose of the 'const' keyword in C?
|
Answer: The 'const' keyword is used to define constants in C.
It specifies that the variable's value cannot be changed once assigned and also
helps make the code more readable.
|
Question: What is the difference between 'for' loop and 'while' loop in C?
|
Answer: The 'for' loop is typically used when the number of iterations is known beforehand,
while the 'while' loop is used when the number of iterations is not known.
|