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

C Program to search an element in an array using Pointers

A separate function( search_function()) will be created where the array pointer will be declared and the searched element along with the size of an array …

C Program to find the sum of the digits of a number using recursion function

This C program calculates the sum of digits of a given number using recursion. Here’s a concise explanation: Function Definition: sumDigits(int n) This function calculates …

C program to find factorial of a number using Ternary operator with Recursion

Recursion refers to the function calling itself directly or in a cycle. Before we begin, you should have the knowledge of following in C Programming: …

C Program to Add Two Numbers Using Call by Reference

The program takes the two numbers from the user and passes the reference to the function where the sum is calculated. You may go through …

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 …

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 …