0% found this document useful (0 votes)
3 views

C array

The document provides an overview of array declaration, initialization, access, and traversal in C programming. It includes syntax examples for one-dimensional and two-dimensional arrays, along with sample C programs demonstrating these concepts. Additionally, it explains how to access and modify array elements using loops.

Uploaded by

yvansh681
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)
3 views

C array

The document provides an overview of array declaration, initialization, access, and traversal in C programming. It includes syntax examples for one-dimensional and two-dimensional arrays, along with sample C programs demonstrating these concepts. Additionally, it explains how to access and modify array elements using loops.

Uploaded by

yvansh681
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/ 6

Syntax of Array Declaration

data_type array_name [size];


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

where N is the number of dimensions.

// C Program to illustrate the array declaration


#include <stdio.h>
int main()
{
// declaring array of integers
int arr_int[5];
// declaring array of characters
char arr_char[5];
return 0;
}
C Array Initialization
1. Array Initialization with Declaration
data_type array_name [size] = {value1, value2, ... valueN};

2. Array Initialization with Declaration without Size


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

// 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
float arr2[5];
for (int i = 0; i < 5; i++) {
arr2[i] = (float)i * 2.1;
}
return 0;
}
Access Array Elements
array_name [index];

// 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
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
C Array Traversal
Array Traversal using for Loop
for (int i = 0; i < N; i++) {
array_name[i];
}
// C Program to demonstrate the use of array
#include <stdio.h>
int main()
{
// array declaration and initialization
int arr[5] = { 10, 20, 30, 40, 50 };
// modifying element at index 2
arr[2] = 100;
// traversing array using for loop
printf("Elements in Array: ");
for (int i = 0; i < 5; i++) {
printf("%d ", arr[i]);
}
return 0;
}

Output
Elements in Array: 10 20 100 40 50

Types of Array in C
1. One Dimensional Array in C
Syntax of 1D Array in C
array_name [size];
// C Program to illustrate the use of 1D array
#include <stdio.h>
int main()
{
// 1d array declaration
int arr[5];
// 1d array initialization using for loop
for (int i = 0; i < 5; i++) {
arr[i] = i * i - 2 * i + 1;
}
printf("Elements of Array: ");
// printing 1d array by traversing using for loop
for (int i = 0; i < 5; i++) {
printf("%d ", arr[i]);
}
return 0;
}

2. Multidimensional Array in C
A. Two-Dimensional Array in C
Syntax of 2D Array in C
array_name[size1] [size2];
// C Program to illustrate 2d array
#include <stdio.h>
int main()
{
// declaring and initializing 2d array
int arr[2][3] = { 10, 20, 30, 40, 50, 60 };
printf("2D Array:\n");
// printing 2d array
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
printf("%d ",arr[i][j]);
}
printf("\n");
}
return 0;
}

Output
2D Array:
10 20 30
40 50 60

You might also like