Introduction to Arrays

Introduction to Arrays

Varun Joshi's photo
Varun Joshi

Published on Aug 15, 2021

2 min read

What is an array?

An Array is a collection of the elements of Similar Data-Type stored one after another in the memory.

why do we use array ?

  • We can overcome the disadvantage of using many variables since variables are used to store more than one element of the same datatype.

Types of arrays.

  • 1D Array
  • 2D Array
  • 3D Array

Syntax to declare

Datatypes array_name[expression];

Quick Note.

ArraySize = No of elements * size of datatype

Array indexing starts from 0

A[3] (2).png

Different ways of array Initialization

    1. Initializing all the memory location

No of elements = Number of Elements

Int a[5] = { 10,20,30,40,50}
    1. Partial Array Initialization
      If the number of values initialized is less than the allocated memory then it is known as partial array initialization.
Int a[5] = { 10,20,30,40,70}
    1. Initialization without size

The number of elements has not been specified to values specified.

int a [ ] = {10,20,30};
    1. String Initialization Strings are defined as an array of characters with a null character in the end.
char str[50] = "GeeksforGeeks";

Program to Read Array and print it.

#include<stdio.h>


int main()
{
int i,arr[10], range;

printf("Enter the number of elements in array:\n");
scanf("%d", &range);

printf("Enter the array elements:\n");
    for(i=0; i<range; i++)
    {
    scanf("%d", &arr[i]);
    }

printf("aray elements are as follows:");
    for(i=0; i<range; i++)
    {
    printf("%d ", arr[i]);
    }

}
 
Share this