PREPROCESSING

  • Thread starter prasoonthegreat
  • Start date
P

prasoonthegreat

Look at the following code....

#include<stdio.h>

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

int main()
{
signed int d;
printf("Total Elements in the array are => %d\n",TOTAL_ELEMENTS);
for(d=-1;d <= (TOTAL_ELEMENTS-2);d++)
printf("%d\n",array[d+1]);

printf("Did u see the output..");
return 0;
}


Why is the for loop not running even once even though the value of
TOTAL_ELEMENTS is 7....

Prasoon
 
B

BartC

Richard Heathfield said:
(e-mail address removed) said:
signed int d;
for(d=-1;d <= (TOTAL_ELEMENTS-2);d++)
printf("%d\n",array[d+1]);
Why is the for loop not running even once even though the value of
TOTAL_ELEMENTS is 7....

d's value is being converted to an unsigned integral type when you
compare it to the value of unsigned integral type yielded by your
macro.

The fix is simple - change the loop:
signed int d;
for(d=0; d <= (TOTAL_ELEMENTS - 1); d++)
{
printf("%d\n",array[d]);
}

A simpler fix is to cast the loop limit: (signed int)(TOTAL-ELEMENTS-2).

That saves the error-prone updating of every use of d throughout the loop
body (assuming there is more to it than this and that there were good
reasons for starting the index at -1).
 
G

gw7rib

(e-mail address removed) said:
Look at the following code....
#include<stdio.h>

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

d's value is being converted to an unsigned integral type when you
compare it to the value of unsigned integral type yielded by your
macro.

I think you've been had. Have you tried googling for
"23,34,12,17,204,99,16" ?
 
R

Richard Tobin

Have you tried googling for
"23,34,12,17,204,99,16" ?
[/QUOTE]
It does seem to be a very popular sequence, doesn't it? I couldn't
discover any deeper significance to the numbers, however. (On the
grounds that there probably isn't one, I didn't look very hard.)

A Google book search reveals that this example appears in Peter
Van der Linden's "Expert C Programming".

-- Richard
 
A

amit.codename13

Look at the following code....

#include<stdio.h>

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

int main()
{
signed int d;
printf("Total Elements in the array are => %d\n",TOTAL_ELEMENTS);
for(d=-1;d <= (TOTAL_ELEMENTS-2);d++)
printf("%d\n",array[d+1]);

printf("Did u see the output..");
return 0;

}

Why is the for loop not running even once even though the value of
TOTAL_ELEMENTS is 7....

Prasoon
d's value is being converted to an unsigned integral type when you
compare it to the value of unsigned integral type yielded by your
macro.

that explains why prasoon doesn't get any output...

i would like to add that...
the behavior of the code is implementation defined as it depends on
how the implementation defines size_t

if the rank of size_t happens to be less than the rank of int the code
should work fine

correct me if m wrong...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,262
Messages
2,571,056
Members
48,769
Latest member
Clifft

Latest Threads

Top