An array is a fixed-size, sequential collection of elements of the same data type. Elements are stored in contiguous memory locations and are accessed using an index.
int marks[30]; // Declares an array named 'marks' that can hold 30 integers float prices[10]; // Declares an array to hold 10 floats
// Size is explicitly given
int numbers[5] = {10, 20, 30, 40, 50};
// Size is inferred from the list
int numbers[] = {10, 20, 30}; // Compiler creates an array of size 3[]. C is 0-indexed.
int val = numbers[0]; // Accesses the first element (10) numbers[2] = 35; // Changes the third element to 35
int arr[5], the valid indices are 0, 1, 2, 3, 4. Accessing arr[5] is an error that C will not stop, and it will corrupt your program's memory.
A function is a self-contained block of code that performs a specific task. You have used built-in functions like printf; C also lets you create your own.
calculateAverage()) and call it many times from different places.main().A function has three parts:
int add(int a, int b); // This is a function prototype
int add(int a, int b) { // Function header
// Function body
int sum = a + b;
return sum;
}main() or another function.
int main() {
int result = add(5, 10); // This is a function call
return 0;
}A function can send a single value back to the code that called it using the return keyword.
return type (e.g., int, float) must match the value being returned.void return type does not return any value.Functions can be categorized based on whether they take arguments (input) or return a value (output).
void printHello() {
printf("Hello!");
}void printNumber(int n) {
printf("The number is %d", n);
}int add(int a, int b) {
return a + b;
}int getAge() {
int age;
printf("Enter age: ");
scanf("%d", &age);
return age;
}When you pass an array to a function, C does not create a copy. Instead, it passes a pointer (the address) to the first element.
This means the function can modify the original array in main().
// 'arr' is actually a pointer (int*)
void doubleArray(int arr[], int size) {
for (int i = 0; i < size; i++) {
arr[i] = arr[i] * 2; // Modifies the original array
}
}
int main() {
int myNums[] = {1, 2, 3};
doubleArray(myNums, 3);
// myNums is now {2, 4, 6}
return 0;
}
Recursion is a process where a function calls itself, either directly or indirectly. It's a powerful alternative to loops for solving problems that can be broken into smaller, similar sub-problems.
A recursive function must have:
// Example: Factorial
long factorial(int n) {
if (n == 0) {
return 1; // Base Case
} else {
return n * factorial(n - 1); // Recursive Step
}
}
A pointer is a special variable that stores the memory address of another variable.
There are two key pointer operators:
& (Address-of operator): Gets the memory address of a variable.* (Dereference/Indirection operator): Gets the value *at* a given memory address.
int x = 10; // A normal variable
int *p; // 'p' is a pointer to an integer
p = &x; // Assign the ADDRESS of 'x' to 'p'
// Now 'p' "points to" 'x'
printf("Value of x: %d\n", x); // Prints 10
printf("Address of x: %p\n", &x); // Prints the address (e.g., 0x7ffc1234)
printf("Value of p: %p\n", p); // Prints the same address (0x7ffc1234)
printf("Value at p: %d\n", *p); // Prints 10 (goes to address, gets value)
*p = 20; // Go to the address 'p' holds and change the value
printf("New value of x: %d\n", x); // Prints 20