Array in C with best examples part 1

Array in C

What is array?

Array is an ordered collection of the same type of elements which are stored in contiguous memory locations and these elements can be accessed using indices of an array.
It is used to store elements of the same data types, elements can be of primitive data types such as int, char, float, double, etc. it can also store composite data types like structure and union.

arrays in C programming itvoyagers

Declaration of array

It is the same as other variables but we have to specify the size of an array next to the variable in square brackets”[ ]”.
Syntax :
data_type array_name[size_of_array];
int num[10];

Here we have created an array variable named num with size 10, which means it can store upto 10 numbers.

Initialization of array

We can initialize elements to array at the time of creation.
int num[] = {1,2,3,4,5,6};
char name[] = {‘I’,’T’,’V’,’o’,’y’,’a’,’g’,’e’,’r’,’s’};

During initialization of an array variable there is no need to specify size of an array because the compiler knows the exact number of elements which are initialized so it will allot size and memory accordingly.

Array indexing

Array index starts from 0 not from 1. Let us take an example.
char name[] = {‘I’,’T’,’V’,’o’,’y’,’a’,’g’,’e’,’r’,’s’};

If we want to access ‘I’ from array then we have to write name[0].
0 in square brackets indicates the position of the element in an array.
The 0th (a.k.a 0 base) element is the first element.
Index of the last element of an array is “n-1” where ‘n’ is the size of the array.
As we can see in the first image.

Memory allocation

All elements of array are in contiguous memory locations,
E.g. suppose name[0] is at 1001 then name[1] will be at 1002, name[2] will be at 1003 and so on. Here it will take only one byte because it’s a character array and size of char is 1 byte in C.

Change value of array

int main()
    char name[] = {‘I’,’T’,’V’,’o’,’y’,’a’,’g’,’e’,’r’,’s’};
    name[4] = ‘Z’; // It will change the 4th element to ‘Z’.
    name[8]= ‘A’; // It will change the 8th element to ‘A’.

/* If we want to access single element from array then we have to specify its index number in square bracket*/
    printf(“%c n”,name[4]);

    returns 0

Output :

Program to take values from user

int main()
    int nums[2],i;
    printf(“Enter first number : ”);

    printf(“Enter second number : ”);

    printf(“Entered numbers are : ”);
        printf(“%d ”,nums[i]);
    returns 0;

Output :
Enter first number :
Enter second number :
Entered numbers are :
10 20

Accessing outbound element from array

There is no index out of bound checking in C and C++. If we run the following code then we can see that it will get compiled without any error but it will generate unexpected output.

int main()
    int nums[5];
    printf(“%d “, nums[8]);
    return 0;

Output :

In C, if we initialize an array with more elements than specified size. Then the compiler will give a warning “[Warning] excess elements in array initializer” but it will not generate any error hence the program will run with warning.

int main()
    int nums[3] = {1,2,3,4,5,8};
    printf(“%d “, nums[1]);
    return 0;

Output :

Advantages of array

We can access any element from the array using its index.
We have to write less lines of code to store many elements.
Looping through the array is much easier; only one loop is enough.
It is easy to sort elements.

Disadvantages of array

It is not dynamic in nature which means we can change its size during execution. It has a fixed size which is decided at the time of deceleration.




Programs to understand the basic data types and I/O.
Programs on Operators and Expressions.
Programs on decision statements.
Programs on looping.
Programs on arrays.
Programs on functions.
Programs on structures and unions.
Programs on pointers.
Programs on string manipulations.
Practice Programs
1 – C program for leap year, easy and simple code
2 – Best C program to find sum of all digits
3 – Best C program to check Palindrome Number
4 – Best C program to check prime number
5 – Best C program to print Fibonacci series
6 – Best C program to reverse a number
7 – Best C program to reverse an array
8 – Best C program to find average of array
9 – Best C program to find frequency of array element
10 – Best C program to sort array in ascending order
11 – Best C program to find factorial using recursion
12 – Best C program to find sum of all numbers in array
13 – Best C program to find largest element in array
14 – Best C program to swap two numbers without third variable
15 – C Program to check if a number is even or odd
16 – C program to find Simple interest
17 – Best C program to add two matrices
18 – Best C program to count vowels and consonants in a string
19 – Best C program to find LCM
20 – Best C program to find HCF
21 – Best C program to swap two numbers using pointers
22 – Best C program to add two numbers using pointers
23 – Best C program to print Multiplication Table
24 – Best C program to check Armstrong number
25 – Best C program to print half pyramid of star(*)
26 – Best C program to print half pyramid of numbers
27 – Best C program to print reverse half pyramid of star
28 – Best C program to print reverse half pyramid of number
More coming soon…
We are aiming to explain all concepts of C in easiest terms as possible.

Leave a Comment