#define

what's wrong in this according to me this should print array elements but it isn't?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
  #include<stdio.h>

  #define TOTAL_ELEMENTS (sizeof(array) / sizeof(array[0]))
  int array[] = {23,34,12,17,204,99,16};

  int main()
  {
      int d;

      for(d=-1;d <= (TOTAL_ELEMENTS-2);d++)
          printf("%d\n",array[d+1]);

      return 0;
  }
Last edited on
Don't make global variables and don't write crazy for loops like that: http://ideone.com/8Alujm
Last edited on
what is wrong in that loop
its range is also size of array? i am asking mistake in my code! ty
To be honest, I can't figure out exactly what the issue is (I'm better at C++ than C). But rewriting the code in a better way makes it work.
If you want to use a #define, then

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#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;
}


Andy

Edit: corrected cut&paste-ism
Last edited on
In the third example you forgot to change "x" to "array" ;)
Last edited on
@ L B

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.

Andy
Last edited on
no one is telling what's wrong in that code exactly ?
IDEone refuses to run certain variants of the code and I don't have access to a compiler so I have no way to tell you what exactly is wrong with it.
What result are you getting for TOTAL_ELEMENTS?

What output are you seeing from your program?

If you won't give us details, we're not going to waste our time looking.
@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.
Last edited on
The issue is the signed/unsigned comparison

This DOESN'T do anything

1
2
3
4
5
6
7
8
9
10
11
12
13
#include<stdio.h>

int main()
{
    int array[] = {23,34,12,17,204,99,16};

    int d;

    for(d=-1;d <= (sizeof(array) / sizeof(array[0])-2);d++)
        printf("%d\n",array[d+1]);

    return 0;
}


But this does (note the (int) cast in the for loop condition)

1
2
3
4
5
6
7
8
9
10
11
12
13
#include<stdio.h>

int main()
{
    int array[] = {23,34,12,17,204,99,16};

    int d;

    for(d=-1;d <= (int)(sizeof(array) / sizeof(array[0])-2);d++)
        printf("%d\n",array[d+1]);

    return 0;
}


Andy
Last edited on
Ah, heh - this is why I hate implicit casts :) and why anti-unsigned advocates have steam :(
Last edited on
okay gotcha thanks andy and L B
Topic archived. No new replies allowed.