[go: up one dir, main page]

0% found this document useful (0 votes)
6 views12 pages

Array Part1

An array in C is a fixed-size collection of similar data types stored in contiguous memory locations, declared with a specific syntax that includes the data type, name, and size. Arrays can be initialized during declaration or afterward using loops, and elements are accessed using the array subscript operator with zero-based indexing. The document provides examples of array declaration, initialization, and element access in C programming.

Uploaded by

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

Array Part1

An array in C is a fixed-size collection of similar data types stored in contiguous memory locations, declared with a specific syntax that includes the data type, name, and size. Arrays can be initialized during declaration or afterward using loops, and elements are accessed using the array subscript operator with zero-based indexing. The document provides examples of array declaration, initialization, and element access in C programming.

Uploaded by

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

ARRAY

What is Array in C?

An array in C is a fixed-size collection of similar data items stored in contiguous memory locations. It
can be used to store the collection of primitive data types such as int, char, float, etc., and also
derived and user-defined data types such as pointers, structures, etc.

C Array Declaration

In C, we have to declare the array like any other variable before using it. We can declare an array by
specifying its name, the type of its elements, and the size of its dimensions. When we declare an
array in C, the compiler allocates the memory block of the specified size to the array name.

Dr.Eman Haggag Page 1


Syntax of Array Declaration

data_type array_name [size];


or
data_type array_name [size1] [size2]...[sizeN];

where N is the number of dimensions.

Dr.Eman Haggag Page 2


The C arrays are static in nature, i.e., they are allocated memory at the compile time.

Dr.Eman Haggag Page 3


Example of Array Declaration

// C Program to illustrate the array declaration

#include <stdio.h>

int main()

// declaring array of integers

int x[5];

// declaring array of characters

char y[5];

Dr.Eman Haggag Page 4


return 0;

C Array Initialization

Initialization in C is the process to assign some initial value to the variable. When the array is
declared or allocated memory, the elements of the array contain some garbage value. So, we need to
initialize the array to some meaningful value. There are multiple ways in which we can initialize an
array in C.

1. Array Initialization with Declaration

Dr.Eman Haggag Page 5


In this method, we initialize the array along with its declaration. We use an initializer list to initialize
multiple elements of the array. An initializer list is the list of values enclosed within braces {
} separated by a comma.

data_type array_name [size] = {value1, value2, ... valueN};

Dr.Eman Haggag Page 6


2. Array Initialization with Declaration without Size

If we initialize an array using an initializer list, we can skip declaring the size of the array as the
compiler can automatically deduce the size of the array in these cases. The size of the array in these
cases is equal to the number of elements present in the initializer list as the compiler can
automatically deduce the size of the array.

data_type array_name[] = {1,2,3,4,5};

The size of the above arrays is 5 which is automatically deduced by the compiler.

3. Array Initialization after Declaration (Using Loops)

We can use for loop, while loop, or do-while loop to assign the value to each element of the array.

for (int i = 0; i < N; i++) {


array_name[i] = valuei;
}

Dr.Eman Haggag Page 7


Example of Array Initialization in C

// C Program to demonstrate array initialization

#include <stdio.h>

int main()

{ // array initialization using initialier list

int arr[5] = { 10, 20, 30, 40, 50 };

// array initialization using initializer list without

// specifying size

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

// array initialization using for loop

Dr.Eman Haggag Page 8


float arr2[5];

for (int i = 0; i < 5; i++) {

arr2[i] = i * 2.1;

return 0;

Dr.Eman Haggag Page 9


Access Array Elements

We can access any element of an array in C using the array subscript operator [ ] and the index
value i of the element.

array_name [index];

One thing to note is that the indexing in the array always starts with 0, i.e., the first element is at
index 0 and the last element is at N – 1 where N is the number of elements in the array.

Example of Accessing Array Elements using Array Subscript Operator


C

Dr.Eman Haggag Page 10


// C Program to illustrate element access using array

// subscript

#include <stdio.h>

int main()

// array declaration and initialization

int arr[5] = { 15, 25, 35, 45, 55 };

// accessing element at index 2 i.e 3rd element

printf("Element at arr[2]: %d\n", arr[2]);

// accessing element at index 4 i.e last element

Dr.Eman Haggag Page 11


printf("Element at arr[4]: %d\n", arr[4]);

// accessing element at index 0 i.e first element

printf("Element at arr[0]: %d", arr[0]);

return 0;

Output

Element at arr[2]: 35

Element at arr[4]: 55

Element at arr[0]: 15

Dr.Eman Haggag Page 12

You might also like