Dynamic memory allocation in C is done using the malloc() function. It reserves a block of memory of the specified size and returns a pointer to the allocated memory. After using the dynamically allocated memory, it should be freed using the free() function to prevent memory leaks.
Example: Dynamic memory allocation
int main() {
int *ptr = (int *)malloc(sizeof(int)); // Dynamically allocate memory for an integer
if (ptr != NULL) {
*ptr = 10; // Store a value in the dynamically allocated memory
printf("Value: %d\n", *ptr); // Output: 10
free(ptr); // Free the dynamically allocated memory
}
return 0;
}