KEMBAR78
Arrays | PDF | Integer (Computer Science) | Computer Data
0% found this document useful (0 votes)
8 views6 pages

Arrays

The document explains the concept of arrays in programming, detailing how to create, access, and modify single-dimensional and two-dimensional arrays in C. It includes syntax examples for declaring arrays, accessing elements, changing values, and looping through arrays. Additionally, it covers the use of the sizeof operator to determine the size of an array and introduces multidimensional arrays for storing data in tabular form.

Uploaded by

pra605039
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views6 pages

Arrays

The document explains the concept of arrays in programming, detailing how to create, access, and modify single-dimensional and two-dimensional arrays in C. It includes syntax examples for declaring arrays, accessing elements, changing values, and looping through arrays. Additionally, it covers the use of the sizeof operator to determine the size of an array and introduces multidimensional arrays for storing data in tabular form.

Uploaded by

pra605039
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Arrays

Arrays are used to store multiple values in a single variable, instead of declaring
separate variables for each value.

To create an array, define the data type (like int) and specify the name of the
array followed by square brackets [].

To insert values to it, use a comma-separated list, inside curly braces:

int myNumbers[] = {25, 50, 75, 100};

Declaration Syntax of Array:

VariableType VariableName[Sequence of Elements];

Example 1: For integral value

int A[10];

Here 10 means, this array A can have 10 integer elements.

2 5 8 44 21 11 7 9 3 1

Example 2: For character value

char B[10];
This array B can have 10 character elements.
f d a b n j l s e y

Access the Elements of an Array


To access an array element, refer to its index number.

Array indexes start with 0: [0] is the first element. [1] is the second element,
etc.

This statement accesses the value of the first element [0] in myNumbers:

Example

#include <stdio.h>

int main() {
int myNumbers[] = {25, 50, 75, 100};
printf("%d", myNumbers[0]);

return 0;
}

Change an Array Element


To change the value of a specific element, refer to the index number:

#include <stdio.h>

int main() {
int myNumbers[] = {25, 50, 75, 100};
myNumbers[0] = 33;

printf("%d", myNumbers[0]);

return 0;
}
Loop Through an Array
You can loop through the array elements with the for loop.

The following example outputs all elements in the myNumbers array:

#include <stdio.h>

int main() {
int myNumbers[] = {25, 50, 75, 100};
int i;

for (i = 0; i < 4; i++) {


printf("%d\n", myNumbers[i]);
}

return 0;
}

Set Array Size


#include <stdio.h>

int main() {
// Declare an array of four integers:
int myNumbers[4];

// Add elements to it
myNumbers[0] = 25;
myNumbers[1] = 50;
myNumbers[2] = 75;
myNumbers[3] = 100;

printf("%d\n", myNumbers[0]);

return 0;
}

C Array Size
Get Array Size or Length
To get the size of an array, you can use the sizeof operator:

#include <stdio.h>

int main() {

int myNumbers[] = {10, 25, 50, 75, 100};

printf("%lu", sizeof(myNumbers));

return 0;

NOTE-:

It is because the sizeof operator returns the size of a type in bytes.

You learned from the Data Types chapter that an int type is usually 4 bytes, so
from the example above, 4 x 5 (4 bytes x 5 elements) = 20 bytes.

C Multidimensional Arrays

Multidimensional Arrays
In the previous chapter, you learned about arrays, which is also known
as single dimension arrays. These are great, and something you will use a lot
while programming in C. However, if you want to store data as a tabular form,
like a table with rows and columns, you need to get familiar
with multidimensional arrays.

A multidimensional array is basically an array of arrays.

Arrays can have any number of dimensions. In this chapter, we will introduce
the most common; two-dimensional arrays (2D).

Two-Dimensional Arrays
A 2D array is also known as a matrix (a table of rows and columns).

To create a 2D array of integers, take a look at the following example:

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

The first dimension represents the number of rows [2], while the second
dimension represents the number of columns [3]. The values are placed in row-
order, and can be visualized like this:

Access the Elements of a 2D Array


To access an element of a two-dimensional array, you must specify the index
number of both the row and column.

This statement accesses the value of the element in the first row (0) and third
column (2) of the matrix array.

#include <stdio.h>
int main() {

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

printf("%d", matrix[0][2]);

return 0;

You might also like