#include <stdio.h>
#define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
int main()
{
int array[] = {23,34,12,17,204,99,16};
for(int d = 0; d < ARRAY_SIZE(array); ++d)
{
printf("%d\n", array[d]);
}
return 0;
}
Or use a variable
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#include <stdio.h>
int main()
{
int array[] = {23,34,12,17,204,99,16};
int total_elems = sizeof(array) / sizeof(array[0]); // even better as const
for(int d = 0; d < total_elems; ++d)
{
printf("%d\n", array[d]);
}
return 0;
}
Thanks, fixed. I also removed by first code listing.
My problem with the use of the #define in the original post is that it hides the use of the variable array "inside" TOTAL_ELEMENTS, making it less clear to read.
Moving the #define inside the main function did make it easier to see what going on, but I don't think the way the #define was being used was good wherever it was placed.
@MikeyBoy: TOTAL_ELEMENTS gives the correct number when I test it. And he already stated there is no output. He gave enough details - a short program with output and the statement that it doesn't output anything - I consider that enough info.