In C programming find the largest and smallest elements in array | learn c, c++ program beginners 😊

Опубликовано: 11 Май 2026
на канале: Coding decoding guru
14
1

In C programming find the largest and smallest elements in array | learn c, c++ program beginners 😊

#computerscience #coding #cprogramming #arrays #arrayInC #learnArrays #arraytutorial #smallestInArray #largestInarrays #computerscience #C #ArrayPractical




Why Do We Need Arrays?

If we have a small number of elements, let us say we want 3 variables, then we can declare them separately like var1, var2, and var3. But if we have a large number of variables then we can use arrays to store them. 

Let us take a real-life example. Suppose you want to make a program that prints 1-100 digits. Now in C language, you can achieve this by 2 methods. The first one is to make 100 variables and store the numbers from 1-100 in those variables separately and then print each digit. The second method is to create an array of size 100 and store the numbers in that array using a loop. These digits can be printed using a single loop in linear complexity. It is clear that the second method is more optimized and desirable than the first one as it is more convenient to store these values in a single array rather than creating 100 variables.

Declaration and Initialization of Array in C

There are various ways in which an array can be declared and initialized in various ways. You can declare an array of any data type (i.e. int, float, double, char) in C. The following ways can be used to declare and initialize an array in C. 

Array Declaration by Specifying the Size

Arrays can be declared by specifying the size or the number of array elements. The size of the array specifies the maximum number of elements that the array can hold. In the latest version of C, you can either declare an array by simply specifying the size at the time of the declaration or you can provide a user-specified size. The following syntax can be used to declare an array simply by specifying its size.

 // declare an array by specifying size in [].

int my_array1[20];

char my_array2[5];

// declare an array by specifying user defined size.

int size = 20;

int my_array3[size];

When an array is declared without allocating any value, then it stores a garbage value. If you access any uninitialized array value, then just like any uninitialized variable, it will give you a garbage value. 

Array Declaration by Initializing Elements

An array can be initialized at the time of its declaration. In this method of array declaration, the compiler will allocate an array of size equal to the number of the array elements. The following syntax can be used to declare and initialize an array at the same time.

// initialize an array at the time of declaration.

int my_array[] = {100, 200, 300, 400, 500}

In the above syntax, an array of 5 elements is created and even though the array size has not been specified here, the compiler will allocate a size of 5 integer elements