×

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

Floyd’s triangle in C

Floyd’s triangle in C Floyd’s triangle is named after a person Robert Floyd. It is a right-angled triangle that is of natural numbers starting from 1 and consecutively selects the upcoming...

4 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.

Armstrong program in C using function

In this article we will learn how to find Armstrong of a number using function in C. An Armstrong number is a three-digit number that is the sum of its separate...

1 minute read.

Types of Array in C

What is an Array? One value can be stored in a variable at once. How many variables will you need if you have 100 values? Well, the answer isn't 100. It...

14 minutes read.

C Function Argument and Return Values

Functions in the C programming language are declared to avoid repeatedly writing a performable block of code; it is the same in any programming language. When it comes to the...

3 minutes read.

Array Of Structures in C

The C programming language allows us to store multiple elements of the same type in arrays. We can use strings to store multiple elements of a character data type. Still,...

4 minutes read.

Sum of N numbers in C using For loop

Before we move on the program of sum of N numbers, first we have to know about the For Loop statement. The syntax of ‘for’ loop in C programming language is...

3 minutes read.

Classification of Programming language

 Classification of Programming language  The programming language represents a set of instructions compiled along with each other to perform a specific task provided by the CPU (Central Processing Unit). A programming...

3 minutes read.

Flow Chart of For loop in C

This is a flowchart that represents the process of executing the for loop in the C programming language. Generally, as we know there are three main components of for loop:1. The...

3 minutes read.

#undef in C

#undef in C In the C programming language, the #undef directive is used to undefine the macro or any constant, which will be defined by the preprocessor directive #define. The #undef...

3 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.

Fahrenheit to Celsius in C

Before going into conversion, we have to know what Fahrenheit and Celsius mean, these are both units to measure the temperature.In our daily life, we use both Fahrenheit and Celsius,...

1 minute 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.

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.

Stdio.h in C

Header files are used to make the programmer’s efforts a lot easier. In order to make the programming simple, there are a number of libraries which are included as predefined...

4 minutes read.

Character Set in C

Introduction Every language has some basic elements which are used to represent information. The English language includes alphabets which together shape a sentence, after which the sentences are used to shape...

3 minutes read.

Socket Programming in C

Socket programming is a process that connects the two or more nodes on a networking communication with respective to each other. One of the sockets that determines the socket (I.e.,...

5 minutes read.

File Operations in C

Why do I need the file? All data will be lost when the program exits. Saving data to a file keeps it safe even if the program stops working. If there...

6 minutes read.

Pi() Function in C

Pi: Mathematic constants are not defined in the c or c++ programming languages. To use the Mathematical constants firstly, we have to define the _USE_MATH_DEFINES and then declare the cmath and...

3 minutes read.

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...

2 minutes read.