C Array Declaration
You can declare arrays just like any other variables except for the fact that you need to use square brackets[ ] after the array name for each dimension of the array.
Rules you must follow while declaring arrays are:
- You should always provide the dimensions of rows, columns, etc., in square brackets when declaring an array without initialization.
- The dimensions should always be positive integral constants or constant expressions.
- If we don't initialize the array while declaring it, then the array elements contain garbage value.
Memory space is allocated to the array at the time of declaration itself.
It's important to note that an array does not change sizes later, so it is a type of static data structure.
Syntax and Declaration of Arrays in C Programming :
The general form of declaration of arrays in C programming is:
VariableType varName[dim1, dim2, ..., dimN];
Where VariableType is a data type like integer, float, etc. and varName is the array name.
Example
int i, j, intArray[20 ], number;
float floatArray[50];
int tableArray[2][ 5 ]; /* 2 rows and 5 columns */
const int NROWS = 100;
const int NCOLS = 200;
float matrix[ NROWS ][ NCOLS ];
Initialization of Array in C
You can initialize arrays at the time of declaration or later, just like any other variable.
We use curly braces {} to store the initialization data separated by commas after the equal sign while array declaration as shown below:
int a[5] = {1,2,3,4,5};
Let's take a look at some of the interesting points about the initialization of arrays:
- If you initialize 1-D arrays in C programming at the time of their declaration, you may not provide its dimension. The compiler determines the size of the array from the number of elements we supply for initialization.
- You can also initialize an array partially and provide fewer items than the array size.
The compiler initializes the remaining array elements with 0.
Example
int intArray[ 6 ] = { 1, 2, 3, 4, 5, 6 };
float floatArray[ 100 ] = { 1.0f, 5.0f, 20.0f };
double fractions[ ] = { 3.141592654, 1.570796327, 0.785398163 }; /* dimension not provided */
Types of Array in C
In the C programming language, several types of arrays can be used to store and manipulate collections of data. Let's see the main types of arrays in C:
1. One-dimensional Array (1D Array): A one-dimensional array is the simplest type of array in C. It is a linear collection of elements of the same data type. The elements are stored in contiguous memory locations and can be accessed using an index. The index starts from 0 and goes up to (size - 1), where size is the number of elements in the array. One-dimensional arrays are declared using the syntax: `data_type array_name[size];`.
Syntax of 1D Array in C:
`data_type array_name[size];`
Example:
int numbers[5];
2. Two-dimensional Array (2D Array): A two-dimensional array is an array of arrays. It is used to represent a table or a matrix-like structure. In a 2D array, elements are organized in rows and columns. The elements in a 2D array are stored in contiguous memory locations, with each row placed one after the other. Two-dimensional arrays are declared using the syntax: `data_type array_name[rows][columns];`.
Syntax of 2D Array in C:
`data_type array_name[rows][columns];`.
Example:
int matrix[3][4];
3. Multi-dimensional Array: C allows arrays with more than two dimensions, known as multi-dimensional arrays. These arrays are an extension of 2D arrays and can have any number of dimensions. The elements in a multi-dimensional array are accessed using multiple indices, one for each dimension. Multi-dimensional arrays are declared using the syntax: `data_type array_name[size1][size2]...[sizeN];`.
Syntax of Multi-dimensional Array in C:
`data_type array_name[size1][size2]...[sizeN];`.
Example:
int cube[3][3][3];
4. Jagged Array: A jagged array is an array of arrays where each inner array can have a different size. In other words, it is an array of pointers, where each pointer points to a separate one-dimensional array. Jagged arrays are useful when the number of elements in each row or column varies. To declare a jagged array, you need to use pointers and dynamically allocate memory for each inner array.
Example:
int *jagged[3];
jagged[0] = (int *)malloc(sizeof(int) * 2);
jagged[1] = (int *)malloc(sizeof(int) * 3);
jagged[2] = (int *)malloc(sizeof(int) * 4);
5. Variable-length Array (VLA): C99 introduced the concept of variable-length arrays, where the size of an array can be specified at runtime. VLAs allow the size of an array to be determined by a variable or an expression. The size of a VLA is not fixed at compile-time but is determined when the array is declared. VLAs can be one-dimensional or multi-dimensional.
Example:
int size = 5;
int vla[size];
Properties of Array
- Homogeneous Elements: All elements in an array are of the same data type.
- Contiguous Memory Allocation: Elements are stored in consecutive memory locations.
- Fixed Size: The size of an array is defined at the time of declaration and cannot be changed.
- Random Access: Elements can be accessed directly using their index, providing constant-time access.
- Indexed Elements: Elements are accessed using zero-based indices.
- Memory Efficiency: Arrays provide efficient use of memory for storing large amounts of data.
- Sequential Storage: Data is stored in a sequence, making traversal straightforward.
- Static and Dynamic Arrays: C supports both static arrays (fixed size) and dynamic arrays (size can be modified at runtime using pointers and dynamic memory allocation).
Why Do We Need Arrays?
- Efficient Data Storage and Access – Arrays store data in contiguous memory locations, making it easy to access elements using their index.
- Optimized Memory Utilization – By storing data sequentially, arrays minimize memory wastage and improve system performance.
- Simplifies Data Organization – Arrays provide a structured way to manage multiple elements, making operations like sorting and searching more efficient.
- Faster Computation and Processing – Arrays allow quick execution of mathematical operations like summation, averaging, and comparisons on large datasets.
- Essential for Real-World Applications – Used in various fields, such as databases, image processing, and scientific computing, to handle structured data effectively.
Access Array Elements
You can access an array element by using its index value.
The array indices start from 0, i.e., the index of the first element of an array is 0.
If their array size is n, then the indices range from 0 to n-1(both inclusive).
Example
int numbers[] = {25, 80, 75, 100};
printf("%d", numbers[0]); //prints 25
Change Value of Array Elements
You can change the value of an array element by using its index and assigning it to a new value. Changing the value of array elements means modifying the data stored in specific positions within an array.
Example
int numbers[] = {25, 80, 75, 100};
numbers[0] = 33;
printf("%d", numbers[0]);
Input and Output of Array Elements
Input and output of array elements talk about how we deliver data to an array and retrieve data from the array. Think of an array as a line of boxes in which each box stores data.
- Input Data into an Array: To add data into an array, we can use a loop or manually assign values to every box by index. For example, if we have an array to save the temperatures of every week, we can use a loop to enter every day's temperature in the array.
- Output Data from an Array: Getting data from an array is like opening the box to see what is present in the box. We can use a loop to undergo each box(element) and read its content material. We will loop via and print every day's temperature for our temperature array.
So, inputting is like filling the boxes in the array; outputting is like checking what is within the boxes. Arrays help us prepare and work with plenty of data conveniently.
Passing an Array to a Function in C
In C, you can pass an array to a function so that you can work with the array elements inside that function. When you pass an array to a function, you are actually passing a pointer to the first element of the array. This allows the function to access and modify the original array elements if needed.
For example :
#include <stdio.h>
// Function declaration
void printArray(int arr[], int size);
int main() {
int myArray[] = {1, 2, 3, 4, 5};
int n = sizeof(myArray) / sizeof(myArray[0]);
// Calling the function and passing the array and its size
printArray(myArray, n);
return 0;
}
// Function to print elements of the array
void printArray(int arr[], int size) {
printf("Array elements are:\n");
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}

You can also try this code with Online C Compiler
Run Code
Output:
Array elements are:
1 2 3 4 5
In this code :
- Function Declaration: The function printArray is declared with two parameters: an array and the size of the array.
- Main Function: We create an array called myArray with 5 elements. We calculate the number of elements in the array using sizeof(myArray) / sizeof(myArray[0]), which gives us the size of the array.
- Function Call: We call printArray and pass the array myArray and its size n as arguments.
- Inside the Function: The function printArray receives the array and its size. It uses a loop to print all the elements of the array.
Return an Array from a Function in C
In C, you cannot directly return an array from a function because arrays decay into pointers when passed to functions. However, you can achieve similar functionality by either returning a pointer to an array that is defined statically, dynamically allocating memory for an array inside the function and returning a pointer to it, or using structures to encapsulate the array.
Let's see a simple example to show how to return an array from a function using dynamic memory allocation, which allows the array to persist beyond the scope of the function:
#include <stdio.h>
#include <stdlib.h>
// Function to create and fill an array, then return it
int* createArray(int size) {
// Dynamically allocate memory for 'size' integers
int *arr = malloc(size * sizeof(int));
// Check if memory allocation was successful
if (arr == NULL) {
printf("Memory allocation failed\n");
exit(1); // Exit program if memory allocation fails
}
// Initialize elements
for (int i = 0; i < size; i++) {
arr[i] = i * i; // Assign square of index as value
}
return arr; // Return the pointer to the array
}
int main() {
int *myArray;
int size = 5;
// Call function to create and return an array
myArray = createArray(size);
// Print the array elements
printf("Array elements are:\n");
for (int i = 0; i < size; i++) {
printf("%d ", myArray[i]);
}
printf("\n");
// Free the dynamically allocated memory
free(myArray);
return 0;
}

You can also try this code with Online C Compiler
Run Code
Output
Array elements are:
0 1 4 9 16

You can also try this code with Online C Compiler
Run Code
In this code :
- Function createArray: This function takes the desired size of the array, allocates memory dynamically using malloc, initializes the array elements, and returns a pointer to the first element of the array.
- Memory Allocation: malloc is used to allocate memory for the array dynamically. This ensures that the memory remains valid even after the function returns.
- Returning the Array: The function returns a pointer to the dynamically allocated memory, which can be treated like an array in the calling function (main).
- Main Function: Calls createArray() to obtain an array, prints the array, and then frees the allocated memory to avoid memory leaks.
Properties of Arrays in C
- The size of an array is defined at the time of declaration and cannot be changed dynamically.
- Elements are stored in consecutive memory locations for efficient access.
- All elements in an array must be of the same data type.
- Each element can be accessed using an index starting from zero.
- Arrays allow quick iteration using loops, making data processing faster.
- Arrays can be single-dimensional, two-dimensional (2D arrays), or multi-dimensional.
- The address of any element can be computed using the base address and index.
Examples of Arrays in C
1. Declaring and Accessing an Array
#include <stdio.h>
int main() {
int numbers[5] = {10, 20, 30, 40, 50};
// Accessing array elements
printf("First element: %d\n", numbers[0]);
printf("Third element: %d\n", numbers[2]);
return 0;
}

You can also try this code with Online C Compiler
Run Code
Output
First element: 10
Third element: 30
2. Array Traversal Using Loops
#include <stdio.h>
int main() {
int numbers[5] = {1, 2, 3, 4, 5};
// Loop through array elements
printf("Array elements: ");
for (int i = 0; i < 5; i++) {
printf("%d ", numbers[i]);
}
return 0;
}

You can also try this code with Online C Compiler
Run Code
Output
Array elements: 1 2 3 4 5
3. Sum of Array Elements
#include <stdio.h>
int main() {
int numbers[] = {10, 20, 30, 40, 50};
int sum = 0;
// Calculate sum of elements
for (int i = 0; i < 5; i++) {
sum += numbers[i];
}
printf("Sum of array elements: %d\n", sum);
return 0;
}

You can also try this code with Online C Compiler
Run Code
Output
Sum of array elements: 150
Advantages of Array in C
The advantages of using arrays in C programming are as follows:
- It helps to optimize the code by reducing the number of variables needed to store items of the same data type.
- It makes it easy to access elements using their indices in O(1) time.
- You can order the array elements by sorting.
- Accessing elements by index has a consistent time complexity, allowing for reliable performance analysis.
- Enables efficient loops for processing multiple data items.
- If a user wants to store multiple values of similar type, then the Array can be used and efficiently utilized.
- An array can be easily transformed into pointers.
Disadvantages of Array in C
The disadvantages of using arrays in C programming are as follows:
- The biggest drawback of arrays is their fixed size i.e.once declared; you can't resize the array. That is why it is a static data structure.
- Insertion and deletion of elements are difficult due to contiguous memory allocation.
- Arrays cannot be resized dynamically.
- If your data is sparse (with many empty or null values), arrays can be inefficient.
- For complex data structures like trees or graphs, arrays may not be the best choice due to their simplicity.
Frequently Asked Questions
What do you mean by array decay in C?
The loss of type and dimensions of an array is known as array decay. It occurs when we pass the array into a function by pointer or value. The first address is sent to the array, which is a pointer. That is why the size of the array is not the original one.
What is the relationship between the pointer and array in C?
Arrays in C store elements of the same types, whereas Pointers are address variables that store the address of a variable. The array variable also has an address that a pointer can point to, and arrays can be navigated using a pointer.
How to declare array type in C?
In C, you declare an array by specifying the data type of its elements, followed by the name of the array and the number of elements in square brackets. For example, `int myArray[10];` declares an array of ten integers.
Conclusion
In this article, we learned about arrays in C programming, how to declare and initialize arrays in C, advantages and disadvantages. We also learned their use cases and examples, along with C codes.
We hope this blog has helped you enhance your knowledge of arrays in C programming.
Check out these useful blogs on -