1. ✅ Functions in C
Functions are reusable blocks of code that perform a specific task.
🔹 User-defined Functions:
You create your own functions apart from the built-in ones.
📘 Example:
cCopyEdit#include <stdio.h>
void greet() {
printf("Hello, Keshab!\n");
}
int main() {
greet(); // Function call
return 0;
}
📘 Function with parameters and return:
cCopyEditint add(int a, int b) {
return a + b;
}
int main() {
int result = add(3, 5);
printf("Sum = %d\n", result);
return 0;
}
2. 🔁 Recursive Functions
A function that calls itself.
📘 Example: Factorial using recursion
cCopyEditint factorial(int n) {
if(n == 0)
return 1;
else
return n * factorial(n - 1);
}
int main() {
printf("Factorial of 5 = %d\n", factorial(5));
return 0;
}
3. 🔢 Arrays
Arrays are collections of elements of the same data type stored in continuous memory locations.
🔹 1D Array:
cCopyEditint marks[5] = {90, 85, 88, 92, 80};
📘 Example: Sum of elements in 1D array
cCopyEditint sum = 0, i;
int arr[5] = {1, 2, 3, 4, 5};
for(i = 0; i < 5; i++) {
sum += arr[i];
}
printf("Sum = %d\n", sum);
🔹 2D Array:
Used to store table-like data.
📘 Example: 2D Array to store marks
cCopyEditint marks[2][3] = {{80, 90, 85}, {75, 70, 95}};
4. 📝 Strings and String Handling Functions
A string in C is an array of characters ending with a \0 (null character).
cCopyEditchar name[] = "Keshab";
🔹 Common string functions (from <string.h>):
strlen()– Finds lengthstrcpy()– Copies one string to anotherstrcmp()– Compares stringsstrcat()– Concatenates strings
📘 Example:
cCopyEdit#include <stdio.h>
#include <string.h>
int main() {
char str1[20] = "Hello ";
char str2[] = "World";
strcat(str1, str2);
printf("Result: %s\n", str1); // Output: Hello World
return 0;
}
5. 📍 Pointers
A pointer is a variable that stores the memory address of another variable.
🔹 Declaration:
cCopyEditint x = 10;
int *ptr = &x;
📘 Example: Print value and address
cCopyEditint x = 10;
int *p = &x;
printf("Value = %d\n", *p); // Dereferencing
printf("Address = %p\n", p); // Address
🔹 Pointers with Functions:
You can pass addresses to modify values in the original variable (pass-by-reference).
📘 Example:
cCopyEditvoid update(int *a) {
*a = *a + 5;
}
int main() {
int x = 10;
update(&x);
printf("Updated x = %d\n", x);
return 0;
}
6. 📦 Structures and Unions
✅ Structures
Used to group different data types under a single name.
📘 Example:
cCopyEditstruct Student {
int id;
char name[20];
float marks;
};
int main() {
struct Student s1 = {101, "Keshab", 95.5};
printf("ID: %d, Name: %s, Marks: %.2f\n", s1.id, s1.name, s1.marks);
return 0;
}
✅ Unions
Like structures but shares memory for all members (efficient for memory).
7. ⚙️ Intermediate-level Programs
📘 Bubble Sort:
cCopyEditvoid bubbleSort(int arr[], int n) {
for(int i = 0; i < n-1; i++)
for(int j = 0; j < n-i-1; j++)
if(arr[j] > arr[j+1]) {
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
📘 Linear Search:
cCopyEditint linearSearch(int arr[], int n, int key) {
for(int i = 0; i < n; i++)
if(arr[i] == key)
return i;
return -1;
}
✅ Summary Table
| Topic | Description | Example Function |
|---|---|---|
| User-defined Functions | Break code into reusable parts | int add(int a, int b) |
| Recursive Functions | Function calling itself | factorial(n) |
| 1D & 2D Arrays | Store multiple values | int arr[5] = {...} |
| Strings | Array of characters | strcat(str1, str2) |
| Pointers | Store addresses, enable dynamic behavior | int *p = &x; |
| Structures & Unions | Group different data types | struct Student {...} |
| Sorting & Searching | Logic-based intermediate programs | bubbleSort(arr, n) |