C Program to Read and Print Elements of an Array

  • Write a C program to read and print elements of an array using for loop.
  • How to take input of array elements using scanf function and for loop.

Required Knowledge

  • C printf and scanf functions
  • For loop in C
  • Accessing Array Elements in C

Reading Array Elements
We can use scanf function to take a number as input from user and store it in integer array at index i as follows.

scanf("%d", &inputArray[i]);

We will use a for loop to take N inputs from user and store them in array from location 0 to N-1.

Printing Array Elements
We can use printf function to print an array element at index i as follows.

printf("%d ", inputArray[i]);

We will use a for loop to traverse an array from index 0 to N-1 and print the elements at corresponding indexes.

C program to read and print array elements using scanf, printf and for loop

C Program to Read and Print Elements of an Array

/*
* C Program to read array elemnts and print 
* array elements on screen  
*/
#include <stdio.h>
#include <conio.h>
  
int main(){
    int inputArray[500], elementCount, counter;
      
    printf("Enter Number of Elements in Array\n");
    scanf("%d", &elementCount);
    printf("Enter %d numbers \n", elementCount);
     
    /* Read array elements one by one using for loop and 
 stores then in adjacent locations starting form index 0*/
    for(counter = 0; counter < elementCount; counter++){
        scanf("%d", &inputArray[counter]);
    }
        
    /* Print array */
    printf("Array Elements\n");
    for(counter = 0; counter < elementCount; counter++){
        printf("%d ", inputArray[counter]);
    }
          
    getch();
    return 0;
}

Output

Enter Number of Elements in Array
5
Enter 5 numbers
8
3
5
1
6
Array Elements
8 3 5 1 6