Posts

Showing posts from March, 2026

Structures in C Programming with Examples

  Structures in C Programming A structure in C is a user-defined data type that allows you to group different types of data together. It is useful when you want to store related information in a single unit. Why Use Structures? Store multiple data types together Represent real-world entities (Student, Employee, Book, etc.) Organize complex data easily Structure Declaration Syntax: struct structure_name { data_type member1; data_type member2; }; Example: struct Student { int roll; char name[50]; float marks; }; Creating Structure Variables struct Student s1; Accessing Structure Members Use the dot (.) operator to access members. s1.roll = 101; strcpy(s1.name, "Rahul"); s1.marks = 85.5; Complete Example #include <stdio.h> #include <string.h> struct Student { int roll; char name[50]; float marks; }; int main() { struct Student s1; s1.roll = 1; ...

Dynamic Memory Allocation in C Programming with Examples

Dynamic Memory Allocation in C Programming Dynamic Memory Allocation (DMA) allows memory to be allocated at runtime. It is useful when the size of data is not known at compile time. Why Use Dynamic Memory? Efficient memory usage Allocate memory when needed Free memory after use Useful for large data structures Header File Required #include <stdlib.h> 1. malloc() Allocates memory but does not initialize it. Syntax: ptr = (type*) malloc(size); Example: int *ptr; ptr = (int*) malloc(5 * sizeof(int)); 2. calloc() Allocates memory and initializes it to zero. Syntax: ptr = (type*) calloc(number, size); Example: int *ptr; ptr = (int*) calloc(5, sizeof(int)); 3. realloc() Resizes previously allocated memory. Syntax: ptr = realloc(ptr, new_size); 4. free() Frees the allocated memory. Syntax: free(ptr); Complete Example #include <stdio.h> #include <stdlib.h> int main() {...