Posts

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() {...

Pointers in C Programming for Beginners with Examples

Pointers in C Programming A pointer in C programming is a variable that stores the memory address of another variable. Pointers are powerful features of C that allow direct memory access. Why Use Pointers? Efficient memory management Pass arguments by reference Work with arrays and functions Used in dynamic memory allocation Declaration of a Pointer Syntax: data_type *pointer_name; Example: int *ptr; Address Operator (&) The & operator is used to get the address of a variable. int num = 10; int *ptr = &num; Dereferencing Operator (*) The * operator is used to access the value stored at the address. printf("%d", *ptr); Complete Example #include <stdio.h> int main() { int num = 25; int *ptr = &num; printf("Value of num = %d\n", num); printf("Address of num = %p\n", &num); printf("Value using pointer = %d", *ptr); return ...

Functions in C Programming for Beginners with Examples

Functions in C Programming A function in C programming is a block of code that performs a specific task. Functions help divide a large program into smaller and manageable parts. Why Use Functions? Reduce code repetition Improve readability Make debugging easier Organize large programs Structure of a Function A function in C has three main parts: Function Declaration (Prototype) Function Definition Function Call 1. Function Declaration Syntax: return_type function_name(parameters); Example: int add(int, int); 2. Function Definition int add(int a, int b) { return a + b; } 3. Function Call #include <stdio.h> int add(int, int); int main() { int result = add(5, 3); printf("Sum = %d", result); return 0; } int add(int a, int b) { return a + b; } Types of Functions in C Library Functions (printf(), scanf(), etc.) User-defined Functions Advantages of Functions ...

Strings in C Programming for Beginners with Examples

Strings in C Programming A string in C programming is a collection of characters stored in an array. Strings are used to store text such as names, messages, and sentences. What is a String? In C, a string is an array of characters that ends with a special character called null character (\0) . Declaration of a String Syntax: char string_name[size]; Example: char name[20]; Initialization of a String char name[] = "Anand"; Here, C automatically adds the null character at the end. Printing a String #include <stdio.h> int main() { char name[] = "Programming"; printf("%s", name); return 0; } The %s format specifier is used to print strings. Common String Functions To use string functions, include: #include <string.h> Function Purpose strlen() Find length of string strcpy() Copy one string to another strcat() Concatenate two strings ...

Arrays in C Programming for Beginners with Examples

Arrays in C Programming An array in C programming is used to store multiple values of the same data type in a single variable. Instead of creating many separate variables, arrays help organize data efficiently. What is an Array? An array is a collection of elements stored in continuous memory locations. Each element is accessed using an index number. Index in C starts from 0 . Declaration of an Array Syntax: data_type array_name[size]; Example: int numbers[5]; This declares an integer array that can store 5 values. Initialization of an Array int numbers[5] = {10, 20, 30, 40, 50}; Here: - numbers[0] = 10 - numbers[1] = 20 - numbers[2] = 30 - numbers[3] = 40 - numbers[4] = 50 Accessing Array Elements #include <stdio.h> int main() { int numbers[3] = {5, 10, 15}; printf("%d\n", numbers[0]); printf("%d\n", numbers[1]); printf("%d\n", numbers[2]); return 0; } ...

break and continue Statements in C Programming

break and continue Statements in C Programming In C programming, break and continue are loop control statements. They are used to change the normal flow of loops like for, while, and do-while. 1. break Statement The break statement immediately stops the loop and exits from it. Syntax: break; Example: #include <stdio.h> int main() { int i; for(i = 1; i This program prints 1 and 2, then stops when i becomes 3. 2. continue Statement The continue statement skips the current iteration and moves to the next loop cycle. Syntax: continue; Example: #include <stdio.h> int main() { int i; for(i = 1; i This program prints 1, 2, 4, and 5. It skips printing 3. Difference Between break and continue break continue Stops the loop completely Skips only one iteration Exits the loop Moves to next iteration Conclusion The break and continue statements give better control over loops....