C – One Dimensional Array2 min read

What is One Dimensional Array in C?

An array with only one subscript is known as a one-dimensional array.
Example: int arr[10].

Syntax:

Declaration of 1D Array in C:

For declaration, the programmer needs to specify the type of element and number of elements in an array.

Certain rules for declaring One Dimensional Array

  • The declaration must have a data type(int, float, char, double, etc.), variable name, and subscript.
  • The subscript represents the size of the array. If the size is declared as 10, programmers are allowed to store only 10 elements (index 0 to 9).
  • An array index always starts from 0 as its first element. For example, if an array variable is declared as arr[10], then it ranges from 0 to 9.
  • Each array element stored in a separate memory location.

The data-type must be a valid C data type, a unique name must be specified to each array and the arraySize must be of an integer constant. Let see an example for integer array with 10 elements:

Initialization of 1D array in C:

Array can be initialize by using a single statement or one by one. The Initialization is done within the curly braces {}.

On second one the array size is not declared so that you can initialize as much value as you want.

Users can also directly assign the value to the particular element in an array by specifying the index number such as: below shows how to assign the value 23 to the 5th element of an array.

NOTE:
In the above example number 4 means the 5th element as the array index starts from 0.

Accessing a 1D Array Elements:

An array can be accessed by using the specific index number. It can be achieved by placing the particular index number within the bracket []. Such as:

We can also assign the value particular index number to other variable but their data type must be same such as:

In the above, the value of 5th element from an array arr[] is assigned to the numb variable.

Example of 1D Array in C.

Declaring and assigning 5 elements in an array and display them accordingly.

Output:


MORE

Keith Number Program in C

In this tutorial, we will write a Keith number in C. It is one of the most asked questions in an interview. Before that, you …
Read More

Keith Number Program in C++

In this tutorial, we will write a Keith number in C++. It is one of the most asked questions in an interview. Before that, you …
Read More

Singly linked list Program C++

In this tutorial, we will write a C++ program to implement singly linked list. In this program, we will create a separate function for each …
Read More

Fascinating Number Program in C

In this tutorial, we will write a C program to check whether the given number is a Fascinating number or not. You may go through …
Read More

Peterson Number in C

In this tutorial, we will learn about the Peterson number and check if the number is Peterson or not in C. What is Peterson Number? …
Read More

Peterson Number in C++

In this tutorial, we will learn about the Peterson number and write a C++ program to check if the given number is a Peterson number …
Read More