The sum of the array variable elements C program example



Compiler: Visual C++ Express Edition 2005

Compiled on Platform: Windows XP Pro SP2

Header file: Standard

Additional library: none/default

Additional project setting: Set project to be compiled as C

Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C Code (/TC)

Other info: none

To do: The sum of the array elements C program example

To show: How to manipulate the C array variable type, sum up the array elements



// Compute the sum of the elements of an array

#include <stdlib.h>

#include <stdio.h>

#define SIZE 12


int main(void)


// declare and initialize the array named a with size SIZE

int a[SIZE] = {1,3,5,4,7,2,99,16,45,67,89,45};

// declare two normal variables

int i, total = 0;

// do the loop for the array...

for(i = 0; i <= (SIZE-1); i++)


// display the array and its element...

printf("\n a[%d]= %d", i, a[i]);

// total up the array

// total = total + a[i]

total += a[i];


printf("\nThe sum of the array elements is %d\n", total);


return 0;



Output example:


a[0]= 1

a[1]= 3

a[2]= 5

a[3]= 4

a[4]= 7

a[5]= 2

a[6]= 99

a[7]= 16

a[8]= 45

a[9]= 67

a[10]= 89

a[11]= 45

The sum of the array elements is 383

Press any key to continue . . .



C and C++ Programming Resources | C & C++ Code Example Index