Day#11 [ Array in C ] 21 Days - C Programming

Today we are going to learn most important topics , that is Array .

What is Array ?

Array is a collection of similar data items stored at contiguous memory locations and elements can be accessed randomly using indices of an array , so you can get first array element which is having index value 0 .

( Remember Index in Array starts from 0 not 1 )

Why we need Array ?

Let’s say you have to store your classmate’s phone number , one way of doing is to create as many variable as you number of classmate but in this case if you have more than 100 friends it is very hard to create as many variables . Another way is by creating array of size number of classmate and then store their phone number .

Declare & Define Array in C

type name[size];

so we have three things - type, name and size

first is type of array , it can be array of integer , array of character or array of floating numbers .

second is name of array , give this array a name so you can access or modify it later by its name . ( Same as variable )

third is size of array , define size for your array .

Let’s see a Example -

Create a array of integer which stores 100 integer numbers

int num[100];

Another Example -

Create a array of 26 characters

char count[26];

Initialize Array in C

If you want to initialize all your elements of array with a default value .

int num[10] = {0};

all 10 elements will be initialize with 0.

What if you don’t define size at time of initialization ?

int num[] = {1,2};

it will create array of two numbers where first is 1 and second is 2.

Get value in Array

first let me remind you that array’s index starts from 0 and if your first element is on 0th index and your nth element is on (n-1)th index .

so you can access value using name[index] format . so arr[0] is your first element, arr[1] is your second element and arr[n-1] is your nth element .

Set value in Array

just assign name[index] a new value .

so if you want your first element to be 5 then do arr[0] = 5;

That’s all about array , it is most important topic, so do more practice until you fell confident .

Thank you …

If you find the content of this website interesting and helpful, use the “Buy HW Programmer a Coffee” link below to buy me a coffee. Your generosity will keep me caffeinated and Inspire me to keep the website up and running .

Thank you so much for your consideration.

https://ko-fi.com/hwprogrammer

Let’s Connect with us :

Instagram : https://www.instagram.com/vikasss_7663/

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Vikas Shekhawat

Vikas Shekhawat

63 Followers

Sole Master | Software engineer and a happy 😊 person | Former Irresponsible Boy !!!