×

C Array

An array is a collection of elements that are used to hold the fixed number of values of the same type. We cannot change the size and type of array after its declaration.

Why should we use an Array?

We should use an array because of its properties:

  • It reduces the code while using the array.
  • It is easy to traverse the data.
  • It is easy to short the element of an array.
  • It is easy to access the data randomly.

There are two types of arrays in C language:

  • 1-D Array
  • 2-D Array

One Dimensional Array:

A list of items can be given one variable name using only one subscript and such a variable is called one-dimensional array.

Declaration of C Array

We can declare an array by using the following syntax.

data_type array_name[array_size];

Initialization of Array

There are two ways to initialize the array:

  1. First way: We can initialize the array of elements inside the bracket with a comma in one line.
Example : char vowel[6]={‘a’,’e’,’i’,’o’,’u’};
  1. Second way: We can initialize the array of the elements one by one. It is easy to access the elements in comparison to the first way.

Example

vowel[0]=’a’;
vowel[1]=’e’;
vowel[2]=’i’;
vowel[3]=’o’;
vowel[4]=’u’;

 

Let us consider an example of an array;

#include <stdio.h>
int main(){
int i;
char vowel[5]={'a','e','i','o','u'};
for(i=0;i<5;i++){
printf("[%d]=%c\n",i,vowel[i]);
}
return 0;
}

Output

[0]=a
[1]=e
[2]=i
[3]=o
[4]=u

Two Dimensional Array

Two-dimensional arrays are used in the form of row and columns. It is also known as matrices

The 2D, 3D or other dimension is also known as a multidimensional array.

Declaration of two-dimensional arrays

The given following syntax to declare the 2D:

Syntax:

data_type  array_name [row-size1][column-size2];

Let us consider an example:

int add[3][3];

Initialization of 2D Array in C

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

Example:

#include<stdio.h>
int main(){
int i=0,j=0;
int arr[3][3]={
{1,2,3}, {2,3,4}, {3,4,5}
};
for(i=0;i<3;i++){
for(j=0;j<3;j++){
printf("arr[%d][%d]=%d\n",i,j,arr[i][j]);
}
}
return 0;
}

Output

arr[0][0]=1
arr[0][1]=2
arr[0][2]=3
arr[1][0]=2
arr[1][1]=3
arr[1][2]=4
arr[2][0]=3
arr[2][1]=4
arr[2][2]=5

There are following some important points while using an array:

  • We must use the same data element or data at the time of initialization.
  • Array index will start with ‘0’ and end with ‘size-1’.

It uses static memory allocation at the time of working with an array.


Related Topics

Selection sort in C

In the C standard, the selection sorting technique exists where the smallest among the unsorted elements of the array is selected at each time and is then inserted into its...

3 minutes read.

Simple hash() function in C

Introduction In this context, we briefly discuss HASH FUNCTION, HASHING or HASH TABLE in C. It is a function used to map data and mapped arbitrary sizes to the fixed-size values. The...

7 minutes read.

Multiplication Table in C

Displaying table up to 10 #include <stdio.h> int main() {     int num, i = 1;     printf("     Enter any Number:");     scanf("%d", &num);     printf("Multiplication table of %d: ", num);  ...

1 minute read.

Array to function in C

Array to function in C We can create functions that receive an array as an argument. To pass an array into a function, we need to write the name of the...

4 minutes read.

C Math Library

C Math Library The <math.h> header defines various mathematical functions and one macro. Many functions are available in this library to take double as an argument and then return double as...

4 minutes read.

fork() in C

Introduction To create a new function in the system, there is a need for a system call, i.e. fork system call. It is also known as the child process. These child...

2 minutes read.

Flow Chart of Do while loop in C

This is a flowchart that represents the process of executing the Do while loop in the C programming language Generally, as we know there are three main components of Do While...

3 minutes read.

First Fit Program in C

This is one of the easiest ways to allocate memory. The main goal is to divide the memory into several fixed sizes. In C, there is only one process for...

3 minutes read.

Run Time Polymorphism in C

What is polymorphism? Polymorphism refers to the existence of various forms. Polymorphism can be simply defined as a message's capacity to be presented in multiple forms. An individual who can have...

4 minutes read.

How to lock top row in Excel

How to lock top row in excel While working with an Excel spreadsheet, the user utilizes the rows and columns to enter the details under various headings. Sometimes while scrolling the...

4 minutes read.

Merge Sort in C

The merge sort follows the principle of the divide and conquers algorithm. Firstly it divides the input of an array into two halves and then calls itself for the two...

4 minutes read.

Heap Sort in C

In this tutorial, we will learn about heap sorting in C language, but before going to Heap sort, we have to know the concept of Complete Binary Tree. What is Complete...

8 minutes read.

CRC Program in C

CRC (Cyclic Redundancy Check) is an error-detection algorithm that is used to detect any errors that may have occurred during the transmission or storage of data. The basic idea behind...

4 minutes read.

Union in C

Union is a user-defined data type that is used to hold the different types of elements like structure. In union, all members share the same memory location. Syntax: union [union name] { ...

1 minute read.

C Decision control statement

Decision Making C conditional statements are used to specify one or more conditions. The statements will be executed if the condition becomes true otherwise alternative statement will be executed if the...

3 minutes read.

Program to Find Mode of an Array in C

Mode is the highest occurring value or number in a given set of data elements. In a group of data values, a value that occurs most frequently is considered to...

3 minutes read.

User Defined Functions in C

C Functions: A function is defined as a block of code that is used to perform some specific task. In functions, we use flower brackets ({}) which contain the set of programming...

5 minutes read.

Float in islower() in C

Introduction: This article briefly discusses the float in islower() in C. The islower() function checks if the input character passed inside the function is lowercase. Lowercase letters include (a-z). The islower()...

4 minutes read.

Snake Game in C

Snake game in C is basic desktop console program which was created in 1970’s. It is an old classic game which was played by every child across the world. This...

4 minutes read.

Math functions in C

Math functions in C: In the C programming language, the compiler allows the developer to perform mathematical operations through the header’s functions, represented by <math.h>. The <math.h> header defines various mathematical...

3 minutes read.