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

Find the output ab, cd, ef, g for the input a,b,c,d,e,f,g in Javascript and Python

In this tutorial, we will write a program to find a pairs of elements from an array such that for the input [a,b,c,d,e,f,g] we will …
Read More

String Pattern Programs in C

In this tutorial, we will write various C pattern programs for String. Before that, you may go through the following topics in C. for loop …
Read More

Java Program to Find pair of Integers in Array whose sum is given Number

In this tutorial, we will write a program to find a pair of elements from an array whose sum equals a given number in java …
Read More

Program to Print Diamond Alphabet Patterns in C

In this tutorial, we will learn to write a C program to print Diamond patterns using alphabets/characters. However, in this tutorial, we will create a …
Read More

Half Diamond Pattern in C using Alphabets

In this tutorial, we will learn and code the half diamond alphabet patterns in C programming language. However, in this tutorial, we will create a …
Read More

Half Pyramid of Alphabets in C

In this tutorial, we will learn and code alphabet patterns in C programming language specifically the Half pyramid of alphabets in C programming. However, in …
Read More