C Arrays (With Examples)
C Arrays (With Examples)
C Flow Control In this tutorial, you will learn to work with arrays. You will learn to declare, initialize and
access elements of an array with the help of examples.
C Functions
A DV E RT I S E M E N T S
C Programming Arrays
C Programming Arrays
C Multi-dimensional Arrays
C Programming Pointers
C Programming Strings
C Programming Files
Additional Topics
A DV E RT I S E M E N T S
An array is a variable that can store multiple values. For example, if you want to store 100
integers, you can create an array for it.
int data[100];
For example,
Enter Email Address* Join
float mark[5];
A DV E RT I S E M E N T S
Here, we declared an array, mark , of floating-point type. And its size is 5. Meaning, it can
hold 5 floating-point values.
It's important to note that the size and type of an array cannot be changed once it is
declared.
Suppose you declared an array mark as above. The first element is mark[0] , the second
element is mark[1] and so on.
Few keynotes:
Arrays have 0 as the first index, not 1. In this example, mark[0] is the first element.
If the size of an array is n , to access the last element, the n-1 index is used. In this
example, mark[4]
Suppose the starting address of mark[0] is 2120d. Then, the address of the mark[1] will
be 2124d. Similarly, the address of mark[2] will be 2128d and so on.
This is because the size of a float is 4 bytes.
Here, we haven't specified the size. However, the compiler knows its size is 5 as we are
initializing it with 5 elements.
Here,
mark[0] is equal to 19
mark[1] is equal to 10
mark[2] is equal to 8
mark[3] is equal to 17
mark[4] is equal to 9
A DV E RT I S E M E N T S
// Program to take 5 values from the user and store them in an array
// Print the elements stored in the array
#include <stdio.h>
int main() {
int values[5];
Output
Enter 5 integers: 1
-3
34
0
3
Displaying integers: 1
-3
34
0
3
Here, we have used a for loop to take 5 inputs from the user and store them in an array.
Then, using another for loop, these elements are displayed on the screen.
#include <stdio.h>
int main()
{
int marks[10], i, n, sum = 0, average;
average = sum/n;
printf("Average = %d", average);
return 0;
}
Output
Enter n: 5
Enter number1: 45
Enter number2: 35
Enter number3: 38
Enter number4: 31
Enter number5: 49
Average = 39
int testArray[10];
Now let's say if you try to access testArray[12] . The element is not available. This may
cause unexpected output (undefined behavior). Sometimes you might get an error and some
other time your program may run correctly.
Hence, you should never access elements of an array outside of its bound.
Multidimensional arrays
In this tutorial, you learned about arrays. These arrays are called one-dimensional arrays.
In the next tutorial, you will learn about multidimensional arrays (array of an array).
A DV E RT I S E M E N T S
Related Tutorials
C Multidimensional Arrays Relationship Between Arrays Pass arrays to a function in C C Array and Pointer Examples
and Pointers
C# Tutorial
DSA Tutorial