15 July 2010

Be ware when using macros

//avg_float.c

#include <stdio.h>

#define LENGTH (sizeof(d_arr)/sizeof(double))
//#define LENGTH sizeof(d_arr)/sizeof(double) // what would happen if we use this LENGTH macro ??

int main(void){


double d_arr[] = {10.50, 3.30, 50.32, 12.53, 23.3, 345.65, 434, 3.12}, ret=0.0;
int i;

for (i=0; i<LENGTH; i++)
ret += d_arr[i];


printf("%lf\n", ret/LENGTH);

return 0;
}

Post a Comment