Start your programming journey with one of the most powerful and foundational languages — C. This comprehensive course is designed for absolute beginners as well as intermediate learners who want to build a solid understanding of programming using the C language. Whether you're preparing for college-level programming, cracking technical interviews, or planning to explore systems or embedded development, this course covers everything step-by-step. Through hands-on examples, real-world practice problems, and structured explanations, you’ll learn how to write clean and efficient C code — from your first printf() to advanced data structures and memory management.
In C programming, static memory allocation limits flexibility — memory is assigned at compile time. To create flexible programs that adjust to runtime requirements, we use dynamic memory allocation. This is achieved using malloc, calloc, realloc, and free.
malloc stands for memory allocation. It allocates a single block of memory and returns a void pointer.
int *ptr;
ptr = (int *)malloc(5 * sizeof(int)); // allocates memory for 5 integers
calloc stands for contiguous allocation. It initializes allocated memory to zero.
int *ptr;
ptr = (int *)calloc(5, sizeof(int)); // allocates and initializes 5 integers to 0
| malloc() | calloc() |
|---|---|
| Allocates memory block | Allocates & initializes to 0 |
| Does not initialize memory | Initializes memory to zero |
| Syntax: malloc(bytes) | Syntax: calloc(elements, size) |
Reallocate previously allocated memory block to a new size.
ptr = realloc(ptr, 10 * sizeof(int));
Frees the memory that was dynamically allocated, preventing memory leaks.
free(ptr);
Imagine UdaanPath allocates space to store student scores entered dynamically at runtime.
#include <stdio.h>
#include <stdlib.h>
int main() {
int *marks;
int n, i;
printf("Enter number of students: ");
scanf("%d", &n);
marks = (int *)malloc(n * sizeof(int));
for(i = 0; i < n; i++) {
printf("Enter marks for student %d: ", i + 1);
scanf("%d", &marks[i]);
}
printf("\nMarks entered on UdaanPath:\n");
for(i = 0; i < n; i++) {
printf("Student %d: %d\n", i + 1, marks[i]);
}
free(marks); // release memory
return 0;
}
free()free()In the next chapter, we will cover Structures and Unions — to combine variables of different types under one name.