---
title: "Creating 1D and 2D arrays"
subject: "Computer Programming"
chapter: 7
key: "semester_1/computer_programming:one-two-dim-arrays-created"
source: https://www.bctengineer.com/q/semester_1/computer_programming/one-two-dim-arrays-created
site: BCT Engineering
---

# Creating 1D and 2D arrays

Computer Programming · Chapter 7: Arrays and Strings

- Asked in: 2070 Asar, 2 marks; 2074 Asoj, 2 marks (asked 2 times)
- Video: [#20 C Multidimensional Arrays | C Programming For Beginners](https://www.youtube.com/watch?v=ATA6dYDz954) — Programiz
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_1/computer_programming/one-two-dim-arrays-created
- Whole chapter: https://www.bctengineer.com/md/semester_1/computer_programming/chapter-7

## Question

How are one dimensional and two dimensional arrays created in C? Explain with examples.

## Short answer

A 1D array is created as type name[size]; e.g., int a[5] = {10,20,30,40,50};. A 2D array is type name[rows][columns]; e.g., int b[2][3] = {{1,2,3},{4,5,6}};. Elements are accessed with zero-based indices and stored in row-major order.

## Answer

An **array** is a collection of elements of the same data type stored in consecutive memory locations and accessed by an index that starts from 0.

#### One dimensional array
Syntax: `data_type name[size];`

It is created by giving the data type, name and size. It can be initialised at the time of declaration.

```c
int marks[5];                     /* 5 integers, garbage values */
int a[5] = {10, 20, 30, 40, 50};  /* initialised */
```
`a[0]` is 10 and `a[4]` is 50. The compiler reserves 5 × sizeof(int) bytes.

#### Two dimensional array
Syntax: `data_type name[rows][columns];`

It is a table (matrix) of rows and columns, stored row by row in memory. Two indices are needed to reach an element.

```c
int b[2][3] = {{1, 2, 3}, {4, 5, 6}};  /* 2 rows, 3 columns */
```
`b[1][2]` is 6. The array holds 2 × 3 = 6 elements.

#### Working example

```c
#include <stdio.h>

int main(void)
{
    int a[5] = {10, 20, 30, 40, 50};          /* 1D, initialised */
    int b[2][3] = {{1, 2, 3}, {4, 5, 6}};     /* 2D: 2 rows, 3 columns */
    int c[3];                                 /* 1D, values given at run time */
    for (int i = 0; i < 3; i++)
        c[i] = i * i;

    printf("a[2] = %d, b[1][2] = %d, c[2] = %d\n", a[2], b[1][2], c[2]);
    return 0;
}
```

Output:

```text
a[2] = 30, b[1][2] = 6, c[2] = 4
```

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. What is an array in C?
   A. Same-type elements in consecutive memory accessed by an index starting from 0
   B. Mixed-type elements accessed by name
   C. Elements stored in random memory locations
   D. A collection indexed starting from 1
   Answer: A. An array stores same-type elements contiguously and indexing starts at 0. (Key point: Array definition)

2. Which declaration creates a 2D array with 2 rows and 3 columns?
   A. int b[3][2];
   B. int b[2,3];
   C. int b[2][3];
   D. int b[6];
   Answer: C. The syntax is data_type name[rows][columns]. (Key point: 2D declaration)

3. In int b[2][3] = {{1,2,3},{4,5,6}}, what is b[1][2]?
   A. 6
   B. 3
   C. 5
   D. 4
   Answer: A. Row index 1 is the second row {4,5,6}, and column index 2 is its third element, 6. (Key point: Element access)

## Related in Computer Programming

- [Two dimensional arrays in FORTRAN](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Afortran-2d-array-syntax) — Computer Programming, chapter 11
- [Array overflow/underflow; add two arrays](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Aarray-overflow-underflow-add) — Computer Programming, chapter 7
- [Compile time initialization of arrays](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Acompile-time-init-2d-array) — Computer Programming, chapter 7

## Related in other subjects

- [Linear shape functions for one-dimensional element](https://www.bctengineer.com/question.md?key=semester_8%2Ffinite_element_method%3Alinear-bar-shape-functions) — Finite Element Method, chapter 5
- [Variational approach for one-dimensional heat conduction](https://www.bctengineer.com/question.md?key=semester_8%2Ffinite_element_method%3Avariational-approach-heat-conduction) — Finite Element Method, chapter 5
- [Derive one-dimensional heat equation](https://www.bctengineer.com/question.md?key=semester_4%2Fapplied_mathematics%3Aheat-eq-derive) — Applied Mathematics, chapter 3, asked 5×
- [Derive one-dimensional wave equation](https://www.bctengineer.com/question.md?key=semester_4%2Fapplied_mathematics%3Awave-eq-derive) — Applied Mathematics, chapter 3, asked 2×
