The return statement is used inside a function to end its execution and (optionally) send a value back to the caller.
Two Main Uses:
In a function with a non-`void` return type (like int or float), return sends a value back.
int add(int a, int b) {
return a + b; // Returns the calculated sum
}
In a void function (which returns nothing), return; can be used to exit the function early.
void printNumber(int n) {
if (n < 0) {
printf("Number is negative.\n");
return; // Exits the function here
}
printf("Number is: %d\n", n);
}
An array is a collection of data items of the same data type, stored in contiguous (one after another) memory locations. This allows you to store a list of values (like 100 student scores) under a single variable name.
A 1D array is a simple list of elements.
Declaration:
data_type array_name[size];
Example: int scores[5]; (This reserves space for 5 integers).
Initialization:
int scores[5] = { 98, 87, 92, 79, 100 };
Accessing Elements (0-based Indexing):
Array elements are accessed using an index in square brackets []. The index always starts at 0.
scores[0] is 98scores[1] is 87scores[4] is 100Arrays are most powerfully used with for loops to iterate over the elements.
for (int i = 0; i < 5; i++) {
printf("Element %d is %d\n", i, scores[i]);
}
A multi-dimensional array is an "array of arrays." The most common is a 2D array, which is perfect for representing a matrix or a grid.
Declaration:
data_type array_name[rows][columns];
Example: int matrix[2][3]; (A matrix with 2 rows and 3 columns).
Initialization:
int matrix[2][3] = {
{ 1, 2, 3 }, // Row 0
{ 4, 5, 6 } // Row 1
};
Accessing Elements (row, col):
You need two indices: matrix[row][col].
matrix[0][0] is 1matrix[0][2] is 3matrix[1][1] is 52D arrays are traversed using nested for loops (one for rows, one for columns).
// Loop through each row
for (int i = 0; i < 2; i++) {
// Loop through each column in that row
for (int j = 0; j < 3; j++) {
printf("Element (%d, %d) is %d\n", i, j, matrix[i][j]);
}
}
#include <stdio.h>
#define SIZE 5
int main() {
int arr[SIZE] = { 5, 1, 4, 2, 8 };
int i, j, temp;
printf("Original array: ");
for (i = 0; i < SIZE; i++) printf("%d ", arr[i]);
printf("\n");
// Bubble Sort Algorithm
for (i = 0; i < SIZE - 1; i++) {
for (j = 0; j < SIZE - i - 1; j++) {
if (arr[j] > arr[j + 1]) { // For ascending order
// Swap elements
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
printf("Sorted array: ");
for (i = 0; i < SIZE; i++) printf("%d ", arr[i]);
printf("\n");
return 0;
}
#include <stdio.h>
#define ROWS 2
#define COLS 3
int main() {
int A[ROWS][COLS] = { {1, 2, 3}, {4, 5, 6} };
int B[ROWS][COLS] = { {9, 8, 7}, {6, 5, 4} };
int C[ROWS][COLS]; // Result matrix
int i, j;
// Add corresponding elements
for (i = 0; i < ROWS; i++) {
for (j = 0; j < COLS; j++) {
C[i][j] = A[i][j] + B[i][j];
}
}
// Print the result matrix C
printf("Resulting Matrix C (A+B):\n");
for (i = 0; i < ROWS; i++) {
for (j = 0; j < COLS; j++) {
printf("%d\t", C[i][j]);
}
printf("\n"); // Newline after each row
}
return 0;
}
#include <stdio.h>
int main() {
int A[3][3];
long det;
int i, j;
printf("Enter the 9 elements of the 3x3 matrix:\n");
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
printf("Enter element A[%d][%d]: ", i, j);
scanf("%d", &A[i][j]);
}
}
// Calculate determinant by expanding along Row 0
// det = a(ei - fh) - b(di - fg) + c(dh - eg)
det = A[0][0] * (A[1][1] * A[2][2] - A[1][2] * A[2][1])
- A[0][1] * (A[1][0] * A[2][2] - A[1][2] * A[2][0])
+ A[0][2] * (A[1][0] * A[2][1] - A[1][1] * A[2][0]);
printf("The determinant of the matrix is: %ld\n", det);
return 0;
}
arr[5], has elements arr[0], arr[1], arr[2], arr[3], and arr[4]. There is no such thing as arr[5]. Accessing it is a major bug (a "buffer overflow").for loop is the natural partner for an array.
for (int i = 0; i < SIZE; i++) is the standard, error-free way to loop through a 1D array.
i). The inner loop iterates the columns (index j).
for (i = 0; i < ROWS; i++) { for (j = 0; j < COLS; j++) { ... A[i][j] ... } }