One Dimensional Arrays in C-Programming

Learn via video course
FREE
View all courses
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
Topics Covered

What is an Array?

An array is a collection of one or more values of the same data type stored in contiguous memory locations. The data type can be user-defined or even any other primitive data-type. Elements of an array can be accessed with the same array name by specifying the index number as the location in memory.

Types of Arrays

Arrays in C are classified into three types:

  • One-dimensional arrays
  • Two-dimensional arrays
  • Multi-dimensional arrays

Introduction to One Dimensional Array in C

We can visualize a one-dimensional array in C as a single row to store the elements. All the elements are stored at contiguous memory locations. Now, we will see how to declare, initialize and access array elements: Introduction to One Dimensional Array in C

Array Declaration

While declaring a one-dimensional array in C, the data type can be of any type, and also, we can give any name to the array, just like naming a random variable. Syntax:

Array Initialization

In static uninitialized arrays, all the elements initially contain garbage values, but we can explicitly initialize them at their declaration.

Syntax:

Here, parameterized values are constant values separated by a comma.

We can skip the writing size of the array within square brackets if we initialize array elements explicitly within the list at the time of declaration. In that case, it will pick elements list size as array size.

Example:

If we want to initialize all elements of an integer array to zero, we could simply write:

Array Accessing

In one-dimensional arrays in C, elements are accessed by specifying the array name and the index value within the square brackets. Array indexing starts from 0 and ends with size-1. If we try to access array elements out of the range, the compiler will not show any error message; rather, it will return some garbage value.

Syntax:

Example:

C Program to illustrate declaration, initialization, and accessing of elements of a one-dimensional array in C:

Output:

In this C programming code, we have initialized an array at the time of declaration with size 3 and array name as arr. At the end of the code, we are trying to print the array values by accessing its elements.

Rules for Declaring One Dimensional Array in C

  • Before using and accessing, we must declare the array variable.
  • In an array, indexing starts from 0 and ends at size-1. For example, if we have arr[10] of size 10, then the indexing of elements ranges from 0 to 9.
  • We must include data type and variable name while declaring one-dimensional arrays in C.
  • We can initialize them explicitly when the declaration specifies array size within square brackets is not necessary.
  • Each element of the array is stored at a contiguous memory location with a unique index number for accessing.

Initialization of One-Dimensional Array in C

After declaration, we can initialize array elements or simply initialize them explicitly at the time of declaration. One-Dimensional arrays in C are initialized either at Compile Time or Run Time.

Compile-Time Initialization

Compile-Time initialization is also known as static-initialization. In this, array elements are initialized when we declare the array implicitly.

Syntax:

Example:

C Program to illustrate Compile-Time Initialization:

Output:

In this C program code, we have initialized an array nums of size 3 and elements as 0,1 and 2 in the list. This is compile-time initialization, and then at the end, we have printed all its values by accessing index-wise.

Run-Time Initialization

Runtime initialization is also known as dynamic-initialization. Array elements are initialized at the runtime after successfully compiling the program.

Example:

C Program to illustrate Run-Time Initialization:

Input

Output:

To demonstrate runtime initialization, we have just declared an array nums of size 5 in this C programming code. After that, within a loop, we ask the user to enter the array values to initialize them after compiling the code. In the end, we have printed its values by accessing them index-wise.

Copying One-Dimensional Arrays in C

If we have two arrays - array1 and array2, one is initialized and another array is just declared, and suppose, if we have to copy array1 elements to array2 then we can't simply just write:

The primary condition to copy an array is that the copy array's size should be less than the original array.

Program to illustrate copying of elements of a one-dimensional array in C

Output:

In this C programming code, we have taken two arrays: array1 and array2. array1 has been initialized at the time of declaration, and to illustrate the concept of copying array elements, we are assigning array1 values to array2 within a loop. At the end, we printed the values of both arrays.

Common Operations and Functions on One Dimensional Arrays

Operation/FunctionDescription
Example
SearchingFinding a particular element in the array.Linear Search Example:
int searchVal = 4;
int index = -1;
for (int i=0; i<5; i++) {
if (Array[i] == searchVal) {
index = i;
break;
}
}
SortingArranging the elements in a particular order (ascending/descending).Bubble Sort Example:
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4 - i; j++) {
if (myArray[j] > myArray[j + 1]) {
int temp = myArray[j];
myArray[j] = myArray[j + 1];
myArray[j + 1] = temp;
}
}
}
Modifying ElementsChanging the value of an element at a specific index.myArray[2] = 7;
Mathematical CalculationsPerforming calculations like sum, average, or product on array elements.Sum and Average Example:
int sum = 0;
float average = 0.0;
for (int i = 0; i < 5; i++) {
sum += myArray[i];
}
average = (float)sum / 5;

Points to Remember About Array in C

  • Arrays in C are a collection of similar data-type elements stored at contiguous memory locations.
  • In arrays in C, all the elements have the same data type and can be accessed by their unique index value.
  • Array indexing starts from 0 and ends with size-1.
  • One-dimensional arrays in C can be initialized statically (during compile-time) or dynamically (during runtime).
  • We must include the data type, variable name for the array, and size of the array in square brackets while declaring one-dimensional arrays in C.

Importance of One Dimensional Arrays in Computer Programming

The importance of one-dimensional arrays in computer programming:

  1. Structured Data Storage: One-dimensional arrays provide a way to store a sequence of elements in a linear format, ensuring that data is organized and easily accessible.

  2. Efficient Memory Usage: Arrays are stored in contiguous memory locations, which ensures that accessing any element in the array is a constant-time operation, providing predictable and fast performance.

  3. Ease of Iteration and Manipulation: iterating through elements is straightforward, requiring only a single loop.

  4. Simplicity in Implementation: It is an excellent tool for beginners to learn about memory organization and data structures.

one-dimensional arrays are a fundamental tool in computer programming, providing a simple yet powerful way to organize, store, and manipulate data. Their efficient use of memory, ease of access, and straightforward implementation make them an indispensable part of any programmer's toolkit.

FAQ

Q. What is a single-dimensional array? Please provide an example.

A. A single-dimensional array is a linear structure of elements in C, where each element stores data of the same data type and can be accessed using an index. For example, int numbers[5] = {1, 2, 3, 4, 5}; declares an integer array named 'numbers' with a size of 5 elements, containing the values 1 to 5.

Q. How do you write a 1D array?

A. To write a 1D array in C, first, you declare the array specifying the data type, such as int or float, followed by the array name and the size inside square brackets. Next, initialize the array elements either individually or in a single line using curly braces. For example, int myArray[5] = {1, 2, 3, 4, 5}; creates a 1D integer array with five elements.

Q. How do I declare a 1D array in C?

A. To declare a 1D array in C, you need to specify the data type, followed by the array name and the size of the array in square brackets. For example, to declare an integer array named 'numbers' with a size of 5, you would write: int numbers[5];

Q. How many dimensional arrays are present in C?

A. In C, arrays can have multiple dimensions, with no specific limit on the number of dimensions. However, practical limitations such as memory and performance constraints usually dictate the choice. One-dimensional arrays are the simplest type, but multi-dimensional arrays (e.g., two-dimensional, three-dimensional) also exist, depending on the programmer's needs.

Conclusion

  • Arrays in C are derived data types containing similar data-type elements.
  • In one-dimensional arrays in C, indexing starts from 0 and ends at size-1, and if we try to access an element out of range, it will return garbage value.
  • We must include a data type, variable name for array, and array size in square brackets while declaring one-dimensional arrays in C.
  • One-dimensional arrays in C can be initialized statically (during compile-time) or dynamically (during runtime).
  • All the elements of an array are stored at contiguous memory locations, and therefore, we can access them using their unique index number.