C - Use sizeof operator with for loop to process all the elements in an array

Description

Use sizeof operator with for loop to process all the elements in an array

Demo

#include<stdio.h>

int main(void)
{
  double values[5] = { 1.5, 2.5, 3.5, 4.5, 5.5 };
  double sum = 0.0;
  for (unsigned int i = 0; i < sizeof(values) / sizeof(values[0]); ++i)
    sum += values[i];//from  ww w.  j  a  v  a2  s .com
  printf("The sum of the values is %.2f", sum);

  return 0;
}

Result

This loop totals the values of all the array elements.

Related Topic