For working professionals
For fresh graduates
More
Explore C Tutorials: From Begi…
1. Introduction to C Tutorial
2. Addition of Two Numbers in C
3. Anagram Program in C
4. Armstrong Number in C
5. Array in C
Now Reading
12 mins read
6. Array of Pointers in C
7. Array of Structure in C
8. C Program to Find ASCII Value of a Character
9. Assignment Operator in C
10. Binary Search in C
11. Binary to Decimal in C
12. Bitwise Operators in C
13. Boolean in C
14. C Compiler for Mac
15. C Compiler for Windows
16. C Function Call Stack
17. C Language Download
18. Operators in C
19. C/C++ Preprocessors
20. C Program for Bubble Sort
21. C Program for Factorial
22. C Program for Prime Numbers
23. C Program for String Palindrome
24. C Program to Reverse a Number
25. Reverse a String in C
26. C string declaration
27. String Input Output Functions in C
28. Calculator Program in C
29. Call by Value and Call by Reference in C
30. Ceil Function in C
31. Coding Vs. Programming
32. Command Line Arguments in C/C++
33. Comments in C
34. Compilation process in C
35. Conditional Statements in C
36. Conditional operator in the C
37. Constant Pointer in C
38. Constants in C
39. Dangling Pointer in C
40. Data Structures in C
41. Data Types in C
42. Debugging C Program
43. Convert Decimal to Binary in C
44. Define And include in C
45. Difference Between Arguments And Parameters
46. Difference Between Compiler and Interpreter
47. Difference Between If Else and Switch
48. Do While Loop In C
49. Double In C
50. Dynamic Array in C
51. Dynamic Memory Allocation in C
52. Enumeration (or enum) in C
53. Evaluation of Arithmetic Expression
54. Factorial of A Number in C
55. Features of C Language
56. Fibonacci Series Program in C Using Recursion
57. File Handling in C
58. For Loop in C
59. Format Specifiers in C
60. Functions in C
61. Function Pointer in C
62. goto statement in C
63. C Hello World Program
64. Header Files in C
65. Heap Sort in C Program
66. Hello World Program in C
67. History of C Language
68. How to compile a C program in Linux
69. How to Find a Leap Year Using C Programming
70. Identifiers in C
71. If Else Statement in C
72. If Statement in C
73. Implementation of Queue Using Linked List
74. Increment and decrement operators in c
75. Input and Output Functions in C
76. How To Install C Language In Mac
77. Jump Statements in C
78. Lcm of Two Numbers in C
79. Length of an Array in C
80. Library Function in C
81. Linked list in C
82. Logical Operators in C
83. Macros in C
84. Matrix multiplication in C
85. Nested if else statement in C
86. Nested Loop in C
87. One Dimensional Array in C
88. Operator Precedence and Associativity in C
89. Overflow And Underflow in C
90. Palindrome Program in C
91. Pattern Programs in C
92. Pointer to Pointer in C
93. Pointers in C: A Comprehensive Tutorial
94. Pre-increment And Post-increment
95. Prime Number Program in C
96. Program for Linear Search in C
97. Pseudo-Code In C
98. Random Access Files in C
99. Random Number Generator in C
100. Recursion in C
101. Relational Operators in C
102. Simple interest program in C
103. Square Root in C
104. Stack in C
105. Stack Using Linked List 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
112. String Comparison in C
113. String Functions in C
114. String Length in C
115. String Pointer in C
116. strlen() in C
117. Structures in C
118. Structure of C Program
119. Switch Case in C
120. C Ternary Operator
121. Tokens in C
122. Toupper Function in C
123. Transpose of a Matrix in C
124. Two Dimensional Array in C
125. Type Casting in C
126. Types of Error in C
127. Unary Operator in C
128. Use of C Language
129. User Defined Functions in C
130. What is Variables in C
131. Is C language case sensitive
132. Fibonacci Series in C
Array in C refers to a method that combines multiple entities of identical type into a huge group. The data types of these elements or entities can be int, float, double, char, or user-defined data types as well. Rather than declaring separate variables for every value, arrays store multiple values in a single variable.
If you want to work on a huge number of variables, you can use an array in C to store them. For example, if you want to store the marks of all the students of class 2 of a school, then you can use an array in C++ or C. After defining the array of the required size, you can print array in C to display the values of each element.
Here are a few more reasons justifying the need to use arrays:
You can declare an array irrespective of its data type. The below ways help you to declare and initialise an array in C.
The array’s size mentions the maximum number of elements it can store. You can either declare an array by defining its size during declaration or allow a user-defined size.
Here’s an example demonstrating how to define the size of an array.
int newarray1[15]; |
The above C command declares an array named “newarray1” of int data type and size 15.
Note that if you declare an array without allocating a value, it will store a garbage value.
This method states that the compiler allocates an array of size equivalent to the number of elements. The array initialisation happens during the declaration time.
Here’s an example showing how to initialise the elements of an array:
int newarray1[] = {5, 10, 20, 30, 40} |
The above command creates an array of 5 elements. Although the array size is not specified, the compiler assigns a size of 5 integer elements to the array.
In this method. If the number of initialised elements is less than the array’s size, the compiler automatically initialises the remaining elements to 0. Here’s an example:
int newarray1[5] = {5, 10, 20, 30, 40}; |
you can also declare it as,
newarray1 = {5, 10, 20, 30, 40} |
Here's another example:
int newarray1[5] = {5, 10, 20}; |
you can also declare it as,
int newarray2 = {5, 10, 20, 0, 0} |
In the above examples, the size of the newarray1 array is 5. On the other hand, the size of the newarray2 array is 5, but only three elements are initialised. The compiler initialises the rest two elements to 0.
When you initialise an array using a loop, the loop iterates from 0 to (size - 1) to access all elements of the array. Here’s an example that uses for loop to initialise an array’s elements.
#include <stdio.h> |
#include <stdio.h>
int main() {
int newarray1[4];
int i;
for (i = 0; i < 4; i++) {
newarray1[i] = 2 * i;
}
printf("newarray1 = {");
for (i = 0; i < 4; i++) {
printf("%d", newarray1[i]);
if (i != 3) {
printf(", ");
}
}
printf("}\n");
return 0;
}
In the above example, an array of size 4 is declared first. The for loop then initialises it.
Any array’s elements range from”0” to “array_size – 1”. The indexing shows the array’s position. If you want to access an array element, you should refer to its index value. Specifically, [0] represents the first element. [1] represents the second element, etc. Here’s the syntax
array_name[index] |
Let’s look at an example to understand how to access array elements:
#include <stdio.h> |
Output: |
You can store array values by accepting input from the user and storing them in that array.
scanf("%d", &newarray1[0]); // inputs an integer element and stores it in 1st position of the array
The printf() method helps you to display the array elements. You can specify the index to indicate the position of the elements you want to print.
printf("%d", newarray1[0]); // prints the element stored at 0th index or 1st position
Advantages of Array in C
Disadvantages of Array in C:
The 2D array in C is the most common type of multidimensional array. In this form of multidimensional array in C, every element can be signified as a separate 1-D array. The total number of elements is the multiplication of the number of rows and columns. For example, if the array is newarray1[3][4] then the total number of elements is 12.
The processing of a 2D array in C needs a nested loop. The outer loop represents the number of rows, and the inner loop represents the columns of elements in each row.
Here’s a syntax to declare a 2D array in C.
datatype name_of_array[no_of_rows][no_of_columns]; |
Initializing 2D array in C:
While working with 1D arrays, if you initialise an array during its declaration, you can skip declaring its size. However, in the case of 2D arrays, you can only skip the first dimension; you must specify the second dimension during initialisation.
Ways to initialise a 2D array in C:
The conventional way involves defining the 1D arrays (with the number of elements equal to that of columns).
Arrays can work like pointers in several scenarios. For example, when an array is passed to a function, it is considered as a pointer. The pointer arithmetic applies to arrays. But both of them differ in several ways. Let’s look at their key differences.
Array | Pointer |
An array holds one or more values of an identical data type. | A pointer holds the address/memory location of a variable. |
It is defined as data type array_name[size]; | It is defined as data type *ptr_name; |
Sequential or contiguous memory is assigned to the elements of an array. | Any random available memory can be allocated to a pointer. |
Since arrays are static, you can’t alter their size. | Since pointers are dynamic, their memory size can be either freed or altered. |
A single array can save a huge number of elements. | A pointer points to only one variable’s memory location. |
The type of elements stored determines the data type of the array. | The type of the variable whose memory location a pointer points to determines its data type. |
When you pass an array to the sizeof() operator, the total size of all the stored elements is returned. | Passing a pointer to the sizeof() operator prints the size of the pointer. The same is identical for any type of pointer. |
Memory allocation to an array happens during the compilation process. | Memory allocation to a pointer happens during the program’s run time. |
Declaring, accessing, and sorting any required elements is simplified with the use of an array in C. Despite storing many elements at once, they save memory and improve the program’s efficiency.
In addition to practising through tutorials that help you reinforce your programming abilities, pursuing higher education can be an extraordinary addition to your skill set.
upGrad’s Master of Science in Computer Science, provided by Liverpool John Moores University, can accelerate your career to stay updated with the recent trends in the tech industry. Due to thorough guidance from leading faculties and industry experts, this course benefits learners with extensive support, projects, live sessions, assignments, videos, use cases, and more. After completing this course, you can discover outstanding job opportunities in software engineering.
Q. What are the types of arrays in C?
There are two types of array in C, i.e., one-dimensional array in C and multidimensional array in C.
Q. What is a string array in C?
The strings are arrays of characters, so the string array in C evolves into a two-dimensional array of characters.
Q. How can I declare an Array in C language?
The common syntax to declare an array in C programming is- datatype arrayName[arraySize];. For example, int numbers[7]; declares an integer array named numbers with a size of 7 elements.
Take a Free C Programming Quiz
Answer quick questions and assess your C programming knowledge
Author
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
+918045604032
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.