Defining an array in C

  • Syntax to define an array in C:

          dataType  arrayName[ arraySize ];              
      

  • Example: the definition double a[10] will reserve memory for 10 consecutive double type variables:

Using an array in C

  • Accessing array elements in C uses the same syntax as Java:

       arrayName[ i ]   is the ith element in the array "arrayName"

  • Example program using an array:

     #include <stdio.h>
    
     int main( int argc, char *argv[] )
     {
        int A[10], i;
    
        for (i = 0; i < 10; i++ )
           A[i] = i*i;           // Assign a value to A[i] 
    
        for (i = 0; i < 10; i++ )
           printf("%d\n", A[i]); // Using the value in A[i] 
     }  

DEMO: demo/C/set1/array1.c