C Programming
Master the fundamentals of C programming with comprehensive tutorials, examples, and hands-on exercises
Dynamic Memory Allocation in C
Dynamic memory allocation allows you to allocate memory during runtime. This is essential for creating flexible programs that can handle varying amounts of data.
malloc() - Memory Allocation
Basic malloc() Usage
#include <stdio.h>
#include <stdlib.h>
int main() {
int n;
printf("Enter number of elements: ");
scanf("%d", &n);
// Allocate memory for n integers
int *arr = (int*)malloc(n * sizeof(int));
if (arr == NULL) {
printf("Memory allocation failed!\n");
return 1;
}
// Input elements
printf("Enter %d elements: ", n);
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
// Display elements
printf("You entered: ");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
// Free allocated memory
free(arr);
return 0;
}
calloc() - Initialized Allocation
calloc() vs malloc()
#include <stdio.h>
#include <stdlib.h>
int main() {
int n = 5;
// Using malloc (uninitialized)
int *arr1 = (int*)malloc(n * sizeof(int));
printf("malloc array (uninitialized): ");
for (int i = 0; i < n; i++) {
printf("%d ", arr1[i]); // Garbage values
}
printf("\n");
// Using calloc (initialized to zero)
int *arr2 = (int*)calloc(n, sizeof(int));
printf("calloc array (zero-initialized): ");
for (int i = 0; i < n; i++) {
printf("%d ", arr2[i]); // All zeros
}
printf("\n");
// Initialize malloc array manually
for (int i = 0; i < n; i++) {
arr1[i] = i + 1;
}
printf("Initialized malloc array: ");
for (int i = 0; i < n; i++) {
printf("%d ", arr1[i]);
}
printf("\n");
free(arr1);
free(arr2);
return 0;
}
realloc() - Resize Memory
Dynamic Array Resizing
#include <stdio.h>
#include <stdlib.h>
int main() {
int *arr;
int size = 3;
int newSize = 6;
// Initial allocation
arr = (int*)malloc(size * sizeof(int));
// Initialize with values
for (int i = 0; i < size; i++) {
arr[i] = i + 1;
}
printf("Original array: ");
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
printf("\n");
// Resize array
arr = (int*)realloc(arr, newSize * sizeof(int));
if (arr == NULL) {
printf("Reallocation failed!\n");
return 1;
}
// Initialize new elements
for (int i = size; i < newSize; i++) {
arr[i] = i + 1;
}
printf("Resized array: ");
for (int i = 0; i < newSize; i++) {
printf("%d ", arr[i]);
}
printf("\n");
free(arr);
return 0;
}
Dynamic 2D Array
2D Dynamic Array
#include <stdio.h>
#include <stdlib.h>
int main() {
int rows = 3, cols = 4;
// Allocate memory for array of pointers
int **matrix = (int**)malloc(rows * sizeof(int*));
// Allocate memory for each row
for (int i = 0; i < rows; i++) {
matrix[i] = (int*)malloc(cols * sizeof(int));
}
// Initialize matrix
int value = 1;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
matrix[i][j] = value++;
}
}
// Display matrix
printf("Dynamic 2D Array:\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("%3d ", matrix[i][j]);
}
printf("\n");
}
// Free memory
for (int i = 0; i < rows; i++) {
free(matrix[i]);
}
free(matrix);
return 0;
}
Memory Management Rules:
- Always check if allocation was successful (pointer != NULL)
- Always free dynamically allocated memory with free()
- Don't use freed memory (dangling pointers)
- Don't free the same memory twice
- Set pointers to NULL after freeing
Frequently Asked Questions
C is a powerful, general-purpose programming language that's been around since the 1970s. It's the foundation for many modern languages and is essential for system programming, embedded systems, and understanding how computers work at a low level. Learning C gives you a solid foundation in programming fundamentals.
No prior programming experience is required! Our C programming tutorial is designed for complete beginners. We start with the basics and gradually progress to more advanced topics. However, having some basic computer literacy and logical thinking skills will be helpful.
You need a C compiler to run C programs. For beginners, we recommend using our online C compiler which requires no installation. For local development, you can use GCC (GNU Compiler Collection) which is available on Windows (via MinGW), macOS (via Xcode), and Linux. IDEs like Code::Blocks, Dev-C++, or Visual Studio Code are also helpful.
The time to learn C depends on your dedication and prior experience. With consistent practice (1-2 hours daily), you can grasp the basics in 2-4 weeks and become proficient in 2-3 months. Mastering advanced concepts like pointers, memory management, and data structures may take 6-12 months of regular practice.
C programming skills open doors to various career paths including system programming, embedded systems development, operating system development, device driver programming, game development, and firmware development. Many companies in automotive, aerospace, telecommunications, and IoT sectors actively seek C programmers.
Absolutely! C remains highly relevant and is consistently ranked among the top programming languages. It's essential for system programming, embedded systems, IoT devices, and performance-critical applications. Many modern languages are built on C, and understanding C helps you become a better programmer overall.