In this tutorial you will learn about the C Program to Find Length or Size of an Array and its application with practical example.
C Program to Find Length or Size of an Array
In this tutorial, we will learn to create a C program that will Find the Length or Size of an Array in C programming.
Prerequisites
Before starting with this tutorial, we assume that you are the best aware of the following C programming topics:
- Operators in C Programming.
- Basic Input and Output function in C Programming.
- Basic C programming.
- For loop in C programming.
- Conditional Statements in C programming.
- Array in C programming.
What is An array?
The array is a collection of similar data types. An array can store multiple values with different indexes in memory by using a single variable. The array can be both single and multidimensional.
Algorithm:-
1 2 3 4 5 6 7 8 9 |
1. Declare the variables for the program. 2. Take the array elements from the user. 3. Using the size of function to finding the array size. 4. Printing the size of the array. 5. End the program. |
Find Length or Size of an Array:-
As we all know, the array is a collection of similar data type elements. In an array, only one variable is declared which can store multiple values. First, will take the number of elements of an array from the user. Then will Find the Length or Size of an Array. And at last, we will print the Length or Size of an Array.
With the help of this program, we can print the Length or Size of an Array.
Program Code:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
//C Program to Find Length or Size of an Array. #include <stdio.h> int main() { //declaring the required variable for the program int arr[] = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100}; //arr[] = it will hold the arrays elements. int num; /* num = it will store the integer value for the size of the array. */ //Finding the size of the array which was pre defined in the program. num = sizeof(arr); //Printing the size of the array. printf("The Total Number of Array Items = %d", num); return 0; } |
Output:-
In the above program, we have first initialized the required variable.
- num = it will hold the integer value.
- arr[] = it will hold the integer value.
Finding the length of the array.
Printing the length of the array.