For working professionals
For fresh graduates
More
5. Array in C
13. Boolean in C
18. Operators in C
33. Comments in C
38. Constants in C
41. Data Types in C
49. Double In C
58. For Loop in C
60. Functions in C
70. Identifiers in C
81. Linked list in C
83. Macros in C
86. Nested Loop in C
97. Pseudo-Code In C
100. Recursion in C
103. Square Root in C
104. Stack in C
106. Static function in C
107. Stdio.h in C
108. Storage Classes in C
109. strcat() in C
110. Strcmp in C
111. Strcpy in C
114. String Length in C
115. String Pointer in C
116. strlen() in C
117. Structures in C
119. Switch Case in C
120. C Ternary Operator
121. Tokens in C
125. Type Casting in C
126. Types of Error in C
127. Unary Operator in C
128. Use of C Language
Debugging a C program often reveals the struggle of managing several related variables. When storing values one by one, the code becomes repetitive, hard to maintain, and prone to mistakes. As the number of variables increases, the logic becomes harder to trace, especially inside loops or conditionals. Managing such situations without a structured approach leads to longer debug sessions and messy logic flow. You can also explore our in-depth Software engineering courses.
This is where the Array in C plays a critical role. It offers a clean, scalable way to store multiple values of the same type using a single variable name. With arrays, you can iterate, modify, and access elements efficiently. Whether handling student scores, temperature logs, or sensor data, arrays simplify the process and bring consistency to your logic.
An array in C is a fixed-size collection of elements that share the same data type and are stored in contiguous memory locations. Instead of creating separate variables for each value, arrays allow you to use a single name with indexed access. This reduces code complexity and makes it easier to handle large sets of data, especially when looping through values or performing batch operations. Arrays play a vital role in organizing structured data, enabling efficient storage, quick access, and predictable behavior during execution. They're fundamental to mastering more advanced concepts like pointers, structures, and dynamic memory in C.
Elevate your expertise with these top programs:
Declaring an array reserves space in memory for multiple values of the same data type. Initialization gives those spaces default or specific values.
data_type array_name[array_size];
Example:
int marks[5];
This line declares an integer array of size 5.
Arrays can be initialized at declaration:
int marks[5] = {90, 85, 78, 92, 88};
Or without specifying the size:
int marks[] = {90, 85, 78, 92, 88};
You can also assign values individually:
marks[0] = 90;
marks[1] = 85;
Uninitialized elements in static/global arrays default to 0.
When working with arrays in C, you may encounter the concept of an array of pointers, which allows dynamic data handling.
Arrays use indexing to access individual elements. The index starts at 0.
printf("%d", marks[2]); // prints 78
You can update values like this:
marks[2] = 80; // changes value at index 2
Looping through arrays allows processing in a structured way:
for(int i = 0; i < 5; i++) {
printf("%d ", marks[i]);
}
This approach is ideal for reading, printing, or modifying arrays. An array of structures is another advanced technique where each element in the array is a structure.
Arrays follow a set of predictable rules:
Understanding these helps avoid bugs and undefined behaviors during execution.
Must Read: Function Pointer in C
Despite drawbacks, arrays remain a foundational tool for structured data in C.
What Are the Different Types of Array in C?
C supports multiple array types, the most common being:
1D Example:
int scores[3] = {75, 82, 91};
2D Example:
int matrix[2][3] = {
{1, 2, 3},
{4, 5, 6}
};
To access an element in a 2D array:
printf("%d", matrix[1][2]); // prints 6
Use nested loops to traverse 2D arrays:
for(int i = 0; i < 2; i++) {
for(int j = 0; j < 3; j++) {
printf("%d ", matrix[i][j]);
}
}
Multidimensional arrays follow the same principles but increase complexity.
Arrays and pointers are closely related in C. The array name acts as a pointer to the first element.
Feature | Array | Pointer |
Memory allocation | Static (fixed at compile time) | Dynamic (can change using malloc) |
Access syntax | arr[i] | *(ptr + i) |
Reassignment | Not allowed | Allowed |
Example:
int arr[3] = {10, 20, 30};
int *ptr = arr;
printf("%d", *(ptr + 1)); // prints 20
Both arr[1] and *(ptr + 1) give the same result.
You can also define pointers to entire arrays:
int arr[5] = {1, 2, 3, 4, 5};
int (*p)[5] = &arr;
printf("%d", (*p)[2]); // prints 3
This is useful when passing arrays to functions or handling dynamic data. In more advanced scenarios, you may use dynamic memory allocation to create arrays with dynamic memory.
Arrays are often passed to functions for modularity and code reuse.
void display(int arr[], int size) {
for(int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
}
Call the function like this:
int data[3] = {7, 8, 9};
display(data, 3);
Alternatively, you can use pointer syntax:
void display(int *arr, int size)
Both forms are valid and perform the same.
You can’t return an array directly. Instead, return a pointer or use static arrays.
int* createArray() {
static int arr[3] = {10, 20, 30};
return arr;
}
Note: Use static to ensure the array persists after the function returns.
int findMax(int arr[], int size) {
int max = arr[0];
for(int i = 1; i < size; i++) {
if(arr[i] > max)
max = arr[i];
}
return max;
}
int sum = 0;
for(int i = 0; i < 5; i++) {
sum += arr[i];
}
for(int i = 0, j = size - 1; i < j; i++, j--) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
These examples showcase how powerful and flexible arrays are in real C programs.
Mastering the array in C is essential for writing clean, efficient, and organized code. Arrays provide a structured way to store and manage collections of data without cluttering your program with repetitive variables. This not only improves code readability but also enhances performance during loops, calculations, and data manipulation tasks.
Beyond the basics, arrays act as a stepping stone toward more advanced C concepts. Understanding how to pass them to functions, work with multidimensional arrays, and connect them with pointers lays the groundwork for deeper programming logic. With consistent practice, using arrays becomes second nature in solving both simple and complex programming problems.
An array in C helps store multiple values of the same type using one variable. This reduces redundancy, simplifies memory management, and makes it easier to process data in loops and functions.
You can declare an array by specifying the type, name, and size. For example: int numbers[5]; creates an array that stores five integers in contiguous memory locations.
No, arrays in C can only store elements of the same data type. To store different types together, you need structures or unions, not arrays.
You can initialize an array during declaration using curly braces: int nums[3] = {1, 2, 3};. You can also partially initialize it; remaining values will be set to zero.
Elements are accessed using index notation. For example, arr[0] accesses the first element. Indexing starts from zero and goes up to one less than the size.
A 2D array stores data in rows and columns. It's useful for matrix-related problems. You declare it like int matrix[3][4]; to hold three rows and four columns.
No, once declared, the size of a static array in C cannot be changed. For dynamic resizing, you need to use pointers with dynamic memory functions like malloc() or realloc().
For statically declared arrays, memory is allocated on the stack. For dynamically allocated arrays using malloc() or calloc(), memory is allocated on the heap.
Yes, arrays can be passed to functions by passing the array name. This passes the base address, allowing the function to access or modify the original array elements.
Functions cannot directly return arrays. However, you can return a pointer to an array by allocating memory dynamically and returning its base address.
An array name represents the address of its first element. You can use pointers to traverse arrays, and pointer arithmetic enables efficient element access in loops.
Arrays have fixed size, can only store one data type, and lack bounds checking. Mistakes like accessing out-of-bounds elements can lead to undefined behavior or program crashes.
Arrays in C include one-dimensional, two-dimensional, and multi-dimensional arrays. These help represent simple lists, matrices, and complex data structures, respectively.
Yes, if you provide values at the time of declaration, C can infer the size. For example, int arr[] = {1, 2, 3}; creates an array of size 3 automatically.
Arrays reduce repetitive code and improve clarity. When working with data collections, arrays make loops, searches, and computations more manageable and consistent than handling individual variables.
Take a Free C Programming Quiz
Answer quick questions and assess your C programming knowledge
Author|900 articles published
Previous
Next
Start Learning For Free
Explore Our Free Software Tutorials and Elevate your Career.
Talk to our experts. We are available 7 days a week, 9 AM to 12 AM (midnight)
Indian Nationals
1800 210 2020
Foreign Nationals
+918068792934
1.The above statistics depend on various factors and individual results may vary. Past performance is no guarantee of future results.
2.The student assumes full responsibility for all expenses associated with visas, travel, & related costs. upGrad does not provide any a.