C Program to print array

Simple C program to print an array. Array is secondary data type and a collection of same data types. For example an int array is a collection of int data type.
#include<stdio.h>
int main()
{
 int arr[8]={1,2,3,4,5,6,7,8};//array
 int i;
 for(i=0;i<8;i++)
 {
  printf("%d ",arr[i]);
 }
 return 0;
}
Another C program to get input from keyboard and print array
#include<stdio.h>
int main()
{
 int n;
 /* array size is n */
 printf("Enter array size: ");
 scanf("%d",&n);
 
 int arr[n],i;
 printf("Enter array elements: ");
 for(i=0;i<n;i++)
 {
  scanf("%d",&arr[i]);
 }
 printf("Array elements are: ");
 for(i=0;i<n;i++)
 {
  printf("%d ",arr[i]);
 }
 return 0;
}
Simple C Program to print array using pointer
#include<stdio.h>
int main()
{
 int arr[7]={3,12,9,54,67,34,21};
 int i;
 for(i=0;i<7;i++)
 {
  printf("%d ",*(arr+i));
 }
 return 0;
}
OR
#include<stdio.h>
int main()
{
 int arr[7]={3,12,9,54,67,34,21};
 int i;
 int *ptr=arr;
 for(i=0;i<7;i++)
 {
  printf("%d ",*(ptr+i));
 }
 return 0;
}
OR
#include<stdio.h>
int main()
{
 int arr[7]={3,12,9,54,67,34,21};
 int i;
 int *ptr=arr;
 for(i=0;i<7;i++,ptr++)
 {
  printf("%d ",*ptr);
 }
 return 0;
}



Popular posts from this blog