This blog is under construction

Sunday, 6 May 2012

Array initialization at runtime in C

Arrays can be initialized at run time.  Below is an example for array initialization at run time.

Example: 
      int num[5];
      for (i = 0; i < 5; i++)
              num[i] = i * 10;


Example C program for array initialization at run time

#include <stdio.h> 
  int main() {
        int val[5], i;
        /* initializing array during run time */
        for (i = 0; i < 5; i++) {
                val[i] = (i+1) * 10;
                printf("%d\t", val[i]);
        }
        printf("\n");
        return 0;
  }

  Output:
  jp@jp-VirtualBox:~/$ ./a.out
  10    20    30    40    50   


No comments:

Post a Comment