Which library function is used to allocate memory dynamically in C?

Which library function is used to allocate memory dynamically in C?

a) malloc()
b) scanf()
c) free()
d) printf()

Answer:

a) malloc()

Explanation:

The malloc() function in C is used to allocate memory dynamically. It allocates a specified number of bytes and returns a pointer to the allocated memory. For example, int *ptr = (int *)malloc(sizeof(int) * 10); allocates memory for an array of 10 integers. The allocated memory is uninitialized, and it is the programmer’s responsibility to initialize it and eventually free it using the free() function.

Understanding dynamic memory allocation using malloc() is essential for managing memory efficiently in C programming.

Reference links:

https://www.rameshfadatare.com/learn-c-programming/

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top