1.It is a collection of data of same data type.
2.It is used to store group of data simultaneously.
3.It can store data of same data type means an integer array can store
only integer value ,character array can store only character value and
so on.
4.We can not fetch data from array directly therefore we use index point.
5.The indexing of array always start with 0.
6.Index value is always an integer number.
7.Array may be of any data type like int, char, float etc.
Syntax
1.Here a is the name of array.
2.int is the data type of array.
3.Size of array is 5 means we can store maximum 5 values in this array.
int a[5]; a[0]=45; a[1]=23; a[2]=89; a[3]=12; a[4]=78;
#include<stdio.h> int main() { int a[5]={ 45,23,89,12,78 }; printf("value at a[0]=%d\n",a[0]); printf("value at a[1]=%d\n",a[1]); printf("value at a[2]=%d\n",a[2]); printf("value at a[3]=%d\n",a[3]); printf("value at a[4]=%d\n",a[4]); } ### Output ### value at a[0]=45 value at a[1]=23 value at a[2]=89 value at a[3]=12 value at a[4]=78
Here in the above program if the array elements increases then number of printf statement will also increase. Here we are using only 5 array elements so you will find that it is very simple to print all elements of array but this
method of printing array elements is very time consuming and is for
beginners, if you want to print all array elements using very few line
then go for second method which is below.
Here
i am going to use for loop to print all the elements of array and this
is the best method to access all the elements of array. Here in this
case if the array elements increases then only we have to increase loop
count and remain code will be same.
#include<stdio.h> int main() { int a[5]={ 45,23,89,12,78 }; for(int i=0;i<=4;i++) { printf("value at a[%d]=%d\n",i,a[i]); } } /* ### Output ### value at a[0]=45 value at a[1]=23 value at a[2]=89 value at a[3]=12 value at a[4]=78 */
This
method of taking user input in array is very time consuming but if you
are beginner then you can use it otherwise go for user input using loop
which is discussed after this topic.
#include<stdio.h> int main() { int a[3];//declaring array printf("Enter element 1="); scanf("%d",&a[0]);//taking input at index 0 printf("Enter element 2="); scanf("%d",&a[1]);//taking input at index 1 printf("Enter element 3="); scanf("%d",&a[2]);//taking input at index 2 for(int i=0;i<=2;i++) { printf("value at a[%d]=%d\n",i,a[i]); } } /* ### Output ### Enter element 1=12 Enter element 2=45 Enter element 3=17 value at a[0]=12 value at a[1]=45 value at a[2]=17 */
To
assign elements in array by taking user input is also very simple in
array because using loop we can simply store many elements in array
using very few line of code.
#include<stdio.h> int main() { int a[3];//declaring array for(int j=0;j<=2;j++) { printf("Enter element %d=",(j+1)); scanf("%d",&a[j]);//taking user input } for(int i=0;i<=2;i++) { printf("value at a[%d]=%d\n",i,a[i]); } } /* ### Output ### Enter element 1=12 Enter element 2=45 Enter element 3=17 value at a[0]=12 value at a[1]=45 value at a[2]=17 */
Here
I am requesting if you found this post was helpful for you then let me
know by your comment in below comment box and share it with your friend.
Also you can contact me on Instagram @rajusunagar_
If
you have not subscribed my website then please subscribe my website.
Try to learn something new and teach something new to other.
Thank you!!.......
0 Comments