Use the input of a user to access a single array element's value. - C Array

C examples for Array:Array Element

Description

Use the input of a user to access a single array element's value.

Demo Code

#include <stdio.h> 
int main() {  /* www . j  a  v a  2 s .c om*/
   int x; 
   int iIndex = -1; 
   int iArray[5]; 
   for ( x = 0; x < 5; x++ ) 
      iArray[x] = (x + 5); 
   do { 
      printf("\nEnter a valid index (0-4): "); 
      scanf("%d", &iIndex); 
   }  while ( iIndex < 0 || iIndex > 4 ); 
   printf("\nThe value of index %d is %d\n", iIndex, iArray[iIndex]); 
}

Result


Related Tutorials