Table of contents
1.
Introduction
2.
What is an Array?
3.
What are the Types of Dimensional Arrays?
4.
What is One Dimensional Array in C?
5.
Syntax of One Dimensional Array in C
6.
Example of One-Dimensional Array in C
7.
Declaration, Initialization and Access of One Dimensional Array in C
7.1.
Array Declaration
7.1.1.
Syntax
7.1.2.
Example
7.2.
Array Initialization
7.2.1.
Syntax 
7.2.2.
Example
7.3.
Array Accessing
7.3.1.
Syntax
7.3.2.
Example
8.
Rules for Declaring a One Dimensional Array in C
9.
How to Initialize One Dimensional Array in C? 
9.1.
Compile-Time Initialization
9.1.1.
Example
9.2.
Run-Time Initialization
9.2.1.
Example
10.
Copying One-Dimensional Arrays in C
10.1.
Using Loops
10.1.1.
Implementation
10.1.2.
Output
10.2.
Using Built-in Functions
10.2.1.
Implementation
10.2.2.
Output
11.
Important Points to Remember About Array in C
12.
Frequently Asked Questions
12.1.
What is single dimensional array in C with example?
12.2.
Can we change the size of a one-dimensional array once it is declared in C?
12.3.
How can we access elements of a one-dimensional array in C?
12.4.
Can we initialise a one-dimensional array with values of different data types in C?
12.5.
Can we pass a one-dimensional array to a function in C as an argument?
13.
Conclusion
Last Updated: Mar 27, 2024
Easy

One Dimensional Array in C

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

One-dimensional arrays in C programming are used to store a group of identical data-type elements in a linear order(contiguous block of memory). Array indices start at 0 and end at size-1, where size represents the number of elements in the array. 

One Dimensional Array in C

This article will briefly discuss the one-dimensional array in C. We will discuss its definition, syntax, declaration and initialisation, along with some examples. So without further ado, let’s get started!

What is an Array?

An array is a data structure in computer programming that stores a collection of elements, typically of the same data type, in contiguous memory locations. Elements are accessed by their index, and arrays are used for efficient storage and retrieval of data, such as lists, matrices, or tables.

What are the Types of Dimensional Arrays?

There are mainly three types of arrays - 

1. One Dimensional (1D)  Array

2. Two Dimension (2D) Array

3. Multidimensional Array


Check out the related article in detailTypes of Array in C

What is One Dimensional Array in C?

A one-dimensional array is a form of data structure used to store components of the same data type, such as characters, floating-point values, or integers, in a continuous block of memory.
A one-dimensional array's components are accessed by their index, which starts at zero and goes up one for each succeeding element. For instance, the first element in an array of integers can be accessed with index 0, the second element with index 1, the third element with index 2, and so on.

One-dimensional arrays are flexible and strong data formats that may be applied to various data storage and manipulation tasks. They are utilised in various applications and are an essential building block of many C programs.

One Dimensional Array in C

Syntax of One Dimensional Array in C

The following is the syntax for declaring and initialising a one-dimensional array in C:

data_type array_name[array_size] = {value1, value2, value3,…};
You can also try this code with Online C Compiler
Run Code

 

Parameters:

Following are the parameters used in the above syntax.

  • data_type represents the data type of the elements that the array will store.
     
  • array_name is the array's name.
     
  • array_size is the number of elements the array can hold.
     
  • value1, value2, value3,…  are the values of each element in the array.

Example of One-Dimensional Array in C

Here's an example of a one-dimensional array in C that stores integers:

##include <stdio.h>
int main() {
    int numbers[5]; // Declare an array of integers with 5 elements
    // Initialize the array
    numbers[0] = 10;
    numbers[1] = 20;
    numbers[2] = 30;
    numbers[3] = 40;
    numbers[4] = 50;
    // Access and print elements
    for (int i = 0; i < 5; i++) {
        printf("Element %d: %d\n", i, numbers[i]);
    }
    return 0;
}


This program declares and initializes a one-dimensional integer array numbers with 5 elements and prints their values.

Declaration, Initialization and Access of One Dimensional Array in C

Array Declaration

An array in C can be declared by its data type and size. The maximum number of elements that the array can hold denotes its size. 

Let’s see the syntax and example of Array declaration.

Syntax

Data_type name[size];

Example

int arr[4];

Array Initialization

The array can be initialized by either specifying the values at the time of declaration or later. 

The syntax and example of array initialization are:

Syntax 

Data_type name[size] = {value1, value2, value3, ….. , value n};

Example

int arr[4] = { 1, 2, 3, 4 };
You can also try this code with Online C Compiler
Run Code

Array Accessing

The array can be accessed by referring to its index positions; the indexing of an array usually starts with 0.

The syntax and example of array accessing are:

Syntax

name[index];

Example

int x = arr[2];
You can also try this code with Online C Compiler
Run Code

Rules for Declaring a One Dimensional Array in C

Before using an array variable in a program, it must be declared. We can do the declaration by the below rules:

  • A data type (int, float, char, double, etc.), variable name, and subscript must be specified in the declaration.
     
  • The subscript represents the array's size. For example, when the size is set to 10, programmers can store ten elements.
     
  • An array index always begins with 0. For example, if an array variable is declared as array[10], the index can take values between 0 and 9.
     
  • Each array element is kept in its memory location.

How to Initialize One Dimensional Array in C? 

There are various approaches to initialising a one-dimensional array in C. 

Related Article - How do you Initialize an Array in C

Compile-Time Initialization

The compile time initialization is used to initialize the array with the fixed values at the time of declaration. It's called compile time since the array is initialized before the program is compiled.

An example of initializing an array named arr with 5 integers is:

Example

int arr[5] = { 1, 2, 3, 4, 5 };
You can also try this code with Online C Compiler
Run Code

Run-Time Initialization

The run-time initialization is used to initialize the array with values at run-time when the program is in the execution phase. 

An example of initializing the array by taking input from users at run time using the scanf() function is:

Example

int arr[5];
for (int i = 0; i < 5; i++) {
	scanf("%d", &arr[i]);
}
You can also try this code with Online C Compiler
Run Code

Copying One-Dimensional Arrays in C

We can copy the contents of a one-dimensional array into another array using inbuilt functions or loops in C. Let’s discuss them one by one.

Using Loops

We can use any of the while, do-while, or for loops to copy the contents. The basic idea is to traverse the original array and copy the contents to the duplicate array. 

Let’s see an example of the same.

Implementation

#include <stdio.h>
#include<string.h>

int main() {
	int arr[5] = {1, 2, 3, 4, 5};

	printf("The original array is: ");
	for (int i = 0; i < 5; i++) {
		printf("%d ", arr[i]);
	}
	printf("\n");

	int duplicate[5];

	for (int i = 0; i < 5; i++) {
		duplicate[i] = arr[i];
	}

	printf("The duplicate array is: ");
	for (int i = 0; i < 5; i++) {
		printf("%d ", duplicate[i]);
	}
	printf("\n");
	return 0;
}
You can also try this code with Online C Compiler
Run Code

Output

The original array is: 1 2 3 4 5 
The duplicate array is: 1 2 3 4 5
You can also try this code with Online C Compiler
Run Code

Using Built-in Functions

The string.h library contains a function memcpy(), which can be used to copy the content of an array. It takes 3 arguments, the destination array, the source array and the size of the source array.

Let’s understand it with an example.

Implementation

#include <stdio.h>
#include<string.h>

int main() {
	int arr[5] = {1, 2, 3, 4, 5};

	printf("The original array is: ");
	for (int i = 0; i < 5; i++) {
		printf("%d ", arr[i]);
	}
	printf("\n");

	int duplicate[5];

	memcpy(duplicate, arr, sizeof(arr));

	printf("The duplicate array is: ");
	for (int i = 0; i < 5; i++) {
		printf("%d ", duplicate[i]);
	}
	printf("\n");
	return 0;
}
You can also try this code with Online C Compiler
Run Code

Output

The original array is: 1 2 3 4 5 
The duplicate array is: 1 2 3 4 5 
You can also try this code with Online C Compiler
Run Code


Also read,  Short int in C Programming

Important Points to Remember About Array in C

  • Fixed Size: Arrays have a fixed size determined at declaration.
  • Zero-Based Index: Array elements are accessed using indices starting from 0.
  • Homogeneous: Arrays store elements of the same data type.
  • Declaration: Declare an array by specifying its data type and size (e.g., int numbers[5]).
  • Initialization: You can initialize arrays at declaration or later using curly braces (e.g., int numbers[5] = {1, 2, 3, 4, 5}).
  • Accessing Elements: Access elements using square brackets (e.g., numbers[2]).
  • Bound Checking: C arrays do not have built-in boundary checks, so be cautious to avoid accessing out-of-bounds indices.
  • Passing to Functions: Arrays are passed to functions by reference, allowing modifications within the function to affect the original array.

Frequently Asked Questions

What is single dimensional array in C with example?

A single-dimensional array in C stores elements in a linear sequence. Example: int numbers[5] = {1, 2, 3, 4, 5}; stores 5 integers in an array named "numbers."

Can we change the size of a one-dimensional array once it is declared in C?

No, once declared, we cannot change the size of a one-dimensional array in C. We can, however, make a new array of varying sizes and copy the elements from the original array to the new one.

How can we access elements of a one-dimensional array in C?

In C, one-dimensional arrays enable element access by providing the array name followed by an index value in square brackets. Indexing starts at 0 and goes up to size-1.

Can we initialise a one-dimensional array with values of different data types in C?

In C, one-dimensional arrays can be initialized statically at compile-time or dynamically at runtime. When declaring such arrays, use square brackets to specify the data type, array variable name, and size

Can we pass a one-dimensional array to a function in C as an argument?

Function arguments can accept both one-dimensional arrays and multidimensional arrays.

Conclusion

This article briefly discussed the topic of the one-dimensional array in C. We discussed its definition, syntax, declaration, and initialisation. We hope this article helped you understand the topic of the one-dimensional array in C.

For further reading, refer to our articles on similar topics:

Recommended problems -

 

Refer to our Guided Path on Coding Ninjas Studio to upskill yourself in Data Structures and AlgorithmsJavaScriptCompetitive Programming and many more! If you wish to test your competency in coding, check out the mock test series and take part in the contests hosted on Coding Ninjas Studio! 

Nevertheless, you may consider our paid courses to give your career an edge over others!

Live masterclass