A comprehensive deep-dive into C programming, memory optimization, dynamic memory allocation, pointers, data structures, and production-grade coding standards.
malloc/calloc) reserves space on the heap, which must be manually released via free().C is a procedural, statically typed, compiled programming language developed by Dennis Ritchie at Bell Labs in 1972. Originally created to write the UNIX operating system, it provides close-to-metal access while maintaining a structured code syntax.
C defines several fundamental primitive types:
char (1 byte): Stores single characters or small integers.int (typically 4 bytes): Stores signed integers.float (4 bytes): Single-precision floating-point numbers.double (8 bytes): Double-precision floating point numbers.#include <stdio.h>
int main() {
int age = 21;
double balance = 4500.75;
char grade = 'A';
printf("Age: %d, Balance: %.2f, Grade: %c\n", age, balance, grade);
return 0;
}
+, -, *, /, %==, !=, <, >, <=, >=&& (AND), || (OR), ! (NOT)& (AND), | (OR), ^ (XOR), ~ (NOT), << (Left Shift), >> (Right Shift)C provides standard constructs for controlling path execution:
if-else blocks and switch statements for branching.for, while, and do-while loops for repetitions.#include <stdio.h>
int main() {
// For loop demonstration
for (int i = 0; i < 5; i++) {
if (i % 2 == 0) {
printf("%d is even\n", i);
} else {
printf("%d is odd\n", i);
}
}
return 0;
}
Functions are the building blocks of C logic. Execution parameters are pushed onto the system stack frame.
#include <stdio.h>
// Function prototype
int add(int a, int b);
int main() {
int result = add(15, 30);
printf("Sum: %d\n", result);
return 0;
}
int add(int a, int b) {
return a + b;
}
An array is a contiguous memory collection of elements of the same data type.
int scores[5] = {90, 85, 95, 88, 92};
In C, strings are simply null-terminated (\0) character arrays.
#include <stdio.h>
#include <string.h>
int main() {
char name[] = "Ajit Dev";
printf("String length: %lu\n", strlen(name));
return 0;
}
Pointers are variables that store the memory addresses of other variables.
Address Variable Value
0x7ff1 x 10
0x7ff5 ptr 0x7ff1 <-- ptr points to x
#include <stdio.h>
int main() {
int x = 10;
int *ptr = &x; // ptr holds address of x
printf("Value of x: %d\n", x);
printf("Address of x: %p\n", (void*)&x);
printf("Value stored in ptr: %p\n", (void*)ptr);
printf("Value pointed to by ptr: %d\n", *ptr); // dereferencing
return 0;
}
Pointers increment and decrement based on the byte width of their declared base type.
ptr points to an integer (4 bytes) at address 0x1000, ptr + 1 points to address 0x1004.Standard dynamic routines live in <stdlib.h>:
malloc(size): Allocates uninitialized memory on the heap.calloc(num, size): Allocates memory and clears all bits to zero.realloc(ptr, new_size): Resizes an existing heap allocation.free(ptr): Releases heap memory back to the operating system.#include <stdio.h>
#include <stdlib.h>
int main() {
int n = 5;
int *arr = (int*)malloc(n * sizeof(int));
if (arr == NULL) {
printf("Memory allocation failed!\n");
return 1;
}
for (int i = 0; i < n; i++) {
arr[i] = i * 10;
}
// Release memory
free(arr);
arr = NULL; // prevent dangling pointer
return 0;
}
A structure allows grouping variables of different types under a single name.
#include <stdio.h>
struct Student {
char name[50];
int age;
float gpa;
};
int main() {
struct Student s1 = {"Ajit Dev", 21, 3.8};
printf("Student: %s, GPA: %.2f\n", s1.name, s1.gpa);
return 0;
}
A union shares the same memory space for all its fields. The size of the union is the size of its largest member.
union Data {
int i;
float f;
char str[20];
};
File access involves opening, reading/writing, and closing streams via the FILE handle.
#include <stdio.h>
int main() {
FILE *fp = fopen("output.txt", "w");
if (fp == NULL) {
printf("Error opening file!\n");
return 1;
}
fprintf(fp, "Writing C roadmaps to disk\n");
fclose(fp);
return 0;
}
NULL to avoid wild pointer faults.free() for every successful heap allocation to prevent leaks.strcpy vs strncpy).malloc() and calloc()?
malloc allocates uninitialized memory blocks, whereas calloc clears all allocated elements to zero.volatile keyword in C?