Javatpoint Logo
Javatpoint Logo

One dimensional array in C

Arrays are a fundamental concept in programming, and they come in different dimensions. One-dimensional arrays, also known as single arrays, are arrays with only one dimension or a single row. In this article, we'll dive deep into one-dimensional arrays in C programming language, including their syntax, examples, and output.

Syntax of One-Dimensional Array in C

The syntax of a one-dimensional array in C programming language is as follows:

  • dataType specifies the data type of the array. It can be any valid data type in C programming language, such as int, float, char, double, etc.
  • arrayName is the name of the array, which is used to refer to the array in the program.
  • arraySize specifies the number of elements in the array. It must be a positive integer value.

Example of One-Dimensional Array in C

Let's take a simple example of a one-dimensional array in C programming language to understand its syntax and usage.

Output:

numbers[0] = 10
numbers[1] = 20
numbers[2] = 30
numbers[3] = 40
numbers[4] = 50

Explanation:

In the above example, we have declared a one-dimensional array of integers named numbers. The array contains five elements, and each element is initialized with a value.

We have used a for loop to iterate over the elements of the array and print their values using the printf function.

Accessing Elements of One-Dimensional Array in C

In a one-dimensional array, each element is identified by its index or position in the array. The index of the first element in the array is 0, and the index of the last element is arraySize - 1.

To access an element of a one-dimensional array in C programming language, we use the following syntax:

  • arrayName is the name of the array.
  • index is the index of the element we want to access.

Example of Accessing Elements of One-Dimensional Array in C

Let's take an example to understand how to access elements of a one-dimensional array in C programming language.

Output:

The first element of the array is: 10
The third element of the array is: 30

Explanation:

In the above example, we have declared a one-dimensional array of integers named numbers. We have accessed the first element of the array using the index 0 and the third element of the array using the index 2.

Accessing Elements of One-Dimensional Arrays

We can access individual elements of a one-dimensional array using their index, which is an integer value that represents the position of the element in the array. The index of the first element in the array is 0, and the index of the last element is arraySize-1.

The syntax to access an element of a one-dimensional array in C is as follows:

  • arrayName is the name of the array.
  • index is the index of the element we want to access.

Example of Accessing Elements of One-Dimensional Array in C

Output:

The third element of the array is 30

Explanation:

In the above example, we have declared a one-dimensional array of integers named numbers and initialized it with the values {10, 20, 30, 40, 50}. We have used the printf function to print the third element of the array, which is accessed using the index 2.

Modifying Elements of One-Dimensional Arrays

We can modify the value of individual elements of a one-dimensional array using their index. To modify an element, we simply assign a new value to it using the assignment operator =.

Example of Modifying Elements of One-Dimensional Array in C

Output:

The third element of the array is 30 
The third element of the array is now 35

Explanation:

In the above example, we have declared a one-dimensional array of integers named numbers and initialized it with the values {10, 20, 30, 40, 50}. We have used the printf function to print the third element of the array, which is accessed using the index 2.

After that, we modified the value of the third element by assigning a new value of 35 to it. Finally, we have used the printf function again to print the new value of the third element.

Initializing One Dimensional Array in C

In C programming language, we can initialize a one-dimensional array while declaring it or later in the program. We can initialize a one-dimensional array while declaring it by using the following syntax:

- "dataType' specifies the data type of the array.

- "arrayName' is the name of the array.

- "arraySize' specifies the number of elements in the array.

- "{element1, element2, ..., elementN}' specifies the values of the elements in the array. The number of elements must be equal to "arraySize'.

Example of Initializing One Dimensional Array in C

Let's take an example to understand how to initialize a one-dimensional array in C programming language.

Output of the code:

numbers[0] = 10 
numbers[1] = 20 
numbers[2] = 30 
numbers[3] = 40 
numbers[4] = 50

Explanation:

In the above example, we have declared a one-dimensional array of integers named numbers and initialized it with the values {10, 20, 30, 40, 50}. We have used a for loop to iterate over the elements of the array and print their values using the printf function.

We can also initialize a one-dimensional array later in the program by assigning values to its elements using the following syntax:

- arrayName is the name of the array.

- index is the index of the element we want to assign a value to.

- value is the value we want to assign to the element.

Example of Initializing One Dimensional Array in C

Let's take an example to understand how to initialize a one-dimensional array later in the program in C programming language.

Output of the code:

numbers[0] = 10 
numbers[1] = 20 
numbers[2] = 30 
numbers[3] = 40 
numbers[4] = 50

Explanation:

In the above example, we have declared a one-dimensional array of integers named numbers. We have initialized the elements of the array later in the program by assigning values to them. We have used a for loop to iterate over the elements of the array and print their values using the printf function.

Conclusion:

In this article, we have covered one-dimensional arrays in C programming language, including their syntax, examples, and output. One-dimensional arrays are an essential concept in programming, and they allow us to store multiple values of the same data type in a single variable. We can access, initialize, and manipulate one-dimensional arrays using various techniques and operations, such as loops and assignments. By mastering one-dimensional arrays, we can solve a wide range of programming problems and implement many useful algorithms and data structures.

Overall, understanding one-dimensional arrays in C is a fundamental building block of programming, and it is a skill that every aspiring programmer should possess. By applying the principles and techniques covered in this article, you can become proficient in working with one-dimensional arrays and apply this knowledge to more complex programming challenges.

Here is the full code example from the article :

Output:

numbers[0] = 10 
numbers[1] = 20 
numbers[2] = 30 
numbers[3] = 40 
numbers[4] = 50 






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA