C Arrays – How to Create, Access, and Modify the arrays in C

C-Arrays-How-to-Create-Access-and-Modify-the-arrays-in-C-language

C Arrays Introduction:

We have looked at the functions in earlier posts, In today’s article, We will look at the arrays in the C programming language. How to declare, Initialize, and access the one-dimensional array’s in C language.

Arrays in C Language:

The C Array is a collection of data items of a similar datatype. The array can store multiple values.

The data items of the array are called array elements. C language arrays can store any datatype values, Like int, char, float, and even the user-defined types( struct).

For example, If we want to store the age of 50 students, Then instead of creating the 50 integer variables, We can create an array that can store the 50 student’s age.

int ages[50]

The position of the element in the array is referred to as the Index. The arrays are Zero Indexed. and The array elements are stored in contiguous memory. So the First element of the array will be stored at Zeroth Index and the second value at 1st index, and so on, The last value is stored at the n-1th index. (where n is the size of the array).

Here is the graphical representation of an array with 10 elements.

C-Arrays-with-Example-Programs

We can access the array elements using the subscript operator [].

📢The C array must-have elements of the same data type.

If we need to find any element in the array, Then we need to traverse the array using a C loop (for loop, while loop, and do while loop).

📢The size of the array is fixed. So we can’t resize it.

The C language supports one-dimensional ( 1-D) arrays and multi-dimensional arrays(2-D array, 3-D array, etc)

In this tutorial, We are going to discuss the one-dimensional array ( 1-D Array).

The One-dimensional Arrays in C Programming language.

Let’s look at how we can create, initialize and modify the 1-D array’s in C language in detail.

C Array declaration:

Syntax:

To create an array, We can use the above syntax,

Where

  • array_name is the Name of your array. It can be any valid C Identifier
  • number_of_elements is the maximum number of elements the array_name array can store. We can also call it as length of the array.
  • Finally, The data_type is the data type of the array elements. (Datatype of the array elements.)

Few Examples of the C-array:

  • int arr[50];
    • The array arr can store 50 elements of Integer datatype
  • char name[30]
    • The array name can store a string which up to 30 characters.

Accessing the arrays:

To access the elements of the array we use the subscript [ ] operator with the element index.

As specified earlier, The array elements indexing starts from zero. So first element index is zero. The second element index is one(1), so one. The index of the last element is n-1.

To get the first element from the ages array, We can use ages[0].

similarly, ages[1] contains the second element,.. The ages[9] contain the last element(i.e tenth element) in the array.

Initializing array in C Language:

We can initialize the array at the time of declaration like below.

Here we have created ages array, which contains the age of five persons and assigned the { 13, 23, 30, 42, 62 }; values to it.

The elements of the array ages would be

ages[0] = 13

ages[1] = 23

ages[2] = 30

ages[3] = 42

ages[4] = 62

If we initialize the array, Then we no need to explicitly specify the length ( number of elements) of the array.

we have not specified the length of scores array explicitly, The compiler will automatically detect the length of the array based on the values passed during the initialization. So the number of elements( length) in the scores array will be automatically set to 4.

We can also initialize an array where the length of the array is greater than the number of values provided.

From the above example, We can see the length of the marks array is 10. But we only passed 5 values. So the remaining five values will be initialized with zeros.

📢 If the array is a local variable, Then by default the array elements contain the garbage values.

If the array is a global variable( static or extern storage class array), Then array elements contain the zeroes as default values.

If you want to initialize all the array elements to zeros. Then

The above initialization will assign zero to all elements of the marks array.

Modifying the array elements in C:

To modify the array elements use the subscript operator [ ] with the element index.

In the above example, We have declared an array arr of length 10. And assigned/modified the elements at the index 0 and 1 with values 39 and 90 respectively.

Input and Output Array Elements in C:

We can traverse the array’s using the C Loops(for loop, etc). This will helpful to display the array elements and collect input from the user.

Here is a program to demonstrate the Input and Output of the arrays.

Read and Print the array elements Program:

Program Output:

Compile and run the C Program using the GCC compiler (Any compiler)

As you can see from the above output, We have declared an array named arr and Used a for loop to traverse the array and update the array elements based on the user input.

Finally, We used one more for loop to print the all array elements.

Let’s look at a couple of example programs to better understand the C Arrays.

C Arrays Example 1: Calculate the Sum of Array elements:

Here is a program to calculate the sum of elements of the array. The program will use a for loop to accept the input values from the user and then calculate the sum of all array elements.

Program to understand Arrays in C Programming Language:

Program Output:

Compile and Run the Program

sum-of-array-program-in-c-output

Sum of Array Program Explanation:

In the above program, We created an array called invoices that can hold 10 integers. We used a for loop to populate the invoices array with user input. We then created a variable called sum to store the sum of the array elements. Finally, we used another for loop to iterate through the invoices array and update the sum variable by adding each element to it. The updated value of sum is then the sum of all elements in the invoices array.

C Arrays Example 2: Find the Minimum number in the Array:

Write a C program to find the minimum number in the given array.

Program Output:

Compile and Run the program using the GCC compiler.

min-number-in-array-program-output

C Arrays – Conclusion:

In this article, We have learned about C Arrays. How to Declare, Initialize, Access, and modify the arrays in C programming language with example programs.

Venkatesh

Hi Guys, I am Venkatesh. I am a programmer and an Open Source enthusiast. I write about programming and technology on this blog.

You may also like...

29 Responses

  1. […] have looked at the one-dimensional arrays in our earlier article, In today’s article, We are going to look at Multi-Dimensional Arrays, […]

  2. […] looked at the one-dimensional arrays and two-dimensional arrays in our earlier posts, In today’s article, we are going to look at […]

  3. […] Introduction to C Arrays – How to Declare, Access, and Modify Arrays […]

  4. […] Introduction to C Arrays – How to Declare, Access, and Modify Arrays […]

  5. […] Introduction to C Arrays – How to Declare, Access, and Modify Arrays […]

  6. […] Introduction to C Arrays – How to Declare, Access, and Modify Arrays […]

  7. […] Arrays in C with example programs […]

  8. […] Introduction to C Arrays – How to Declare, Access, and Modify Arrays […]

  9. […] Introduction to C Arrays – How to Declare, Access, and Modify Arrays […]

  10. […] need to know the basics of the C Array, Functions, and Passing Arrays to […]

  11. […] Arrays in C Language with Example Programs […]

  12. […] is required to know the Arrays and Functions in C language to better understand the following […]

  13. […] What are Arrays in Programming – How to Create, Use Arrays in C Programming […]

  14. […] Introduction to C Arrays – How to Declare, Access, and Modify Arrays […]

  15. […] Arrays in C with example programs […]

  16. […] Introduction to C Arrays – How to Declare, Access, and Modify Arrays […]

  17. […] Introduction to C Arrays – How to Declare, Access, and Modify Arrays […]

  18. […] Introduction to C Arrays – How to Declare, Access, and Modify Arrays […]

  19. […] Introduction to C Arrays – How to Declare, Access, and Modify Arrays […]

  20. […] Arrays in C […]

  21. […] is recommended to know the basics of the C Arrays, Functions in C, and Mutli-dimentional Arrays or Matrices in C […]

  22. […] Introduction to C Arrays – How to Declare, Access, and Modify Arrays […]

  23. […] C Arrays […]

  24. […] Introduction to C Arrays – How to Declare, Access, and Modify Arrays […]

  25. […] have looked at the Arrays and multi-dimensional arrays in earlier articles. In today’s article, We will look at the Strings […]

  26. […] C Arrays […]

  27. […] is recommended to know the C-Strings, C-Arrays, and Functions in C to better understand the following […]

  28. […] is recommended to know the C-Strings, C-Arrays, and Functions in C to better understand the following […]

  29. […] Introduction to C Arrays – How to Declare, Access, and Modify Arrays […]

Leave a Reply