Siksha Sarovar

Siksha Sarovar (sikshasarovar.com) is a free educational web application that helps students in India learn programming and prepare for academic and competitive exams. The platform offers structured coding courses (C, C++, Python, Java, HTML, CSS, PHP, Power BI, AI, Machine Learning, Data Science), complete university curriculum notes for BCA/MCA students with previous year question papers, Class 10 and Class 12 CBSE/HBSE school notes, and dedicated preparation material for SSC, UPSC, Banking, Railway and other government exams. Browsing the site is completely free and requires no account. Users may optionally sign in with Google solely to save their learning progress, quiz scores and personal preferences across devices.

Privacy Policy | Terms of Service | Contact Siksha Sarovar | About Siksha Sarovar

v4.0.9 · PWA
Siksha Sarovar logo
Siksha Sarovar
Your Learning Universe

Siksha Sarovar is a free e-learning platform for coding courses, BCA university notes and competitive exam preparation. Optional Google sign-in saves your learning progress across devices.

Initializing knowledge base…
Compiling modules 0%

Unit 2 — Arrays in C

Lesson 12 of 32 in the free C Language notes on Siksha Sarovar, written by Rohit Jangra.

Arrays in C

An array is a collection of elements of the same data type stored in contiguous memory locations, accessed using an index.

---

1. One-Dimensional Arrays

Declaration:

data_type array_name[size];
int marks[5];

Initialisation:

int marks[5] = {85, 90, 78, 92, 88};
int zeros[5] = {0};            /* all elements 0 */
int arr[] = {1, 2, 3, 4, 5};  /* size inferred */

Accessing elements:

marks[0] = 85;   /* first element (index 0) */
marks[4] = 88;   /* last element (index 4) */

Traversal:

#include <stdio.h>

int main() {
    int arr[5] = {10, 20, 30, 40, 50};
    for (int i = 0; i < 5; i++) {
        printf("arr[%d] = %d\n", i, arr[i]);
    }
    return 0;
}

---

2. Two-Dimensional Arrays (Matrices)

Declaration:

int matrix[3][3];

Initialisation:

int matrix[2][3] = {
    {1, 2, 3},
    {4, 5, 6}
};

Matrix addition example:

int a[2][2] = {{1,2},{3,4}};
int b[2][2] = {{5,6},{7,8}};
int c[2][2];

for (int i = 0; i < 2; i++)
    for (int j = 0; j < 2; j++)
        c[i][j] = a[i][j] + b[i][j];

---

3. String as Character Array

char name[20] = "Alice";
char greeting[] = {'H','e','l','l','o','\0'};  /* null-terminated */

Common string functions (string.h):

FunctionPurpose
strlen(s)Length of string
strcpy(dest, src)Copy string
strcat(dest, src)Concatenate strings
strcmp(s1, s2)Compare strings
strupr(s)Convert to uppercase
strlwr(s)Convert to lowercase

---

Passing Arrays to Functions

void printArray(int arr[], int size) {
    for (int i = 0; i < size; i++) {
        printf("%d ", arr[i]);
    }
}

int main() {
    int data[] = {5, 10, 15, 20};
    printArray(data, 4);
    return 0;
}
Note: Arrays are always passed by reference — the function receives a pointer to the first element.