By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,915 Members | 1,280 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,915 IT Pros & Developers. It's quick & easy.

can someone explane to the behaviour

P: n/a
Hi,
i checked this piece of code with both gcc as well as vc++
compilers. Behaviour is same. I don't understand the behaviour. Can
some1 please explain the reason of the result.

#include<stdio.h>
#define TOTAL_ELEMENTS (sizeof(array) / sizeof(array[0]))
int array[] = {23,34,12,17,204,99,16};
int main()
{
int d = -1;

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

return 0;
}

It never enters into the loop.

or even if it is simply like this

int main()
{
int d = -1;
if ( d < sizeof(int) )
printf (" Success ");
}

it never prints, any explanation.

Jul 13 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
sr*************@gmail.com wrote:
Hi,
i checked this piece of code with both gcc as well as vc++
compilers. Behaviour is same. I don't understand the behaviour. Can
some1 please explain the reason of the result.
(fx:snip)
or even if it is simply like this

int main()
{
int d = -1;
if ( d < sizeof(int) )
printf (" Success ");
}

it never prints, any explanation.
`d` gets converted to whatever unsigned type `size_t` is.
That makes the bit-pattern for -1 denote a BIG number,
much much much bigger than sizeof(int) is ever likely to be.

--
Chris "seeker" Dollin
"Who are you? What do you want?" /Babylon 5/

Jul 13 '06 #2

P: n/a
turn on all your compiler's warnings and you'll probably see a message:
warning: comparison of signed and unsigned ints. Follow that hint.

Jul 13 '06 #3

P: n/a
sr*************@gmail.com wrote:
Hi,
i checked this piece of code with both gcc as well as vc++
compilers. Behaviour is same. I don't understand the behaviour. Can
some1 please explain the reason of the result.

#include<stdio.h>
#define TOTAL_ELEMENTS (sizeof(array) / sizeof(array[0]))
int array[] = {23,34,12,17,204,99,16};
int main()
{
int d = -1;

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

return 0;
}

It never enters into the loop.

or even if it is simply like this
int main()
{
int d = -1;
if ( d < sizeof(int) )
printf (" Success ");
}
it never prints, any explanation.
Strictly speaking the above code should read
#include <stdio.h>

int main(int argc,char *argv[])
{
int d = -1;
if ( d < sizeof(int) )
printf (" Success ");

return 0;
}

Missing header (can) cause trouble.

Anyway - sizeof yields a value of type size_t , which is an unsigned
integer type.
The other side of the comparison (your variable d) is then promoted to
an unsigned integer (your system can't represent all values of a size_t
type as an int so this promotion is done.) - and -1 interpreted as an
unsigned type is a large number.

The first piece of code has the same issue.

if ( d < (int)sizeof(int) ) should yield different results.
Jul 13 '06 #4

P: n/a
sr*************@gmail.com said:
Hi,
i checked this piece of code with both gcc as well as vc++
compilers. Behaviour is same. I don't understand the behaviour. Can
some1 please explain the reason of the result.

#include<stdio.h>
#define TOTAL_ELEMENTS (sizeof(array) / sizeof(array[0]))
int array[] = {23,34,12,17,204,99,16};
int main()
{
int d = -1;

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

return 0;
}

It never enters into the loop.
I try very hard to steer clear of integer promotion questions, but this one
looks easy enough, so I'll have a crack at it (but keep an eye out for a
correction from others here).

It seems to me that you've been bitten by promotion rules. The sizeof
operator yields a size_t value, which is of course unsigned. It appears
that d is being promoted to size_t for the purposes of the comparison, and
(size_t)-1 is absolutely huge.

Solution: use size_t for d rather than int, and start it at 0 rather than
-1, and change d+1 to d in the loop body.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Jul 13 '06 #5

P: n/a
Hi,
thank U all for this clarification. Yes it is truly the behaviour.
Singed gets promoted to unsigned.
Richard Heathfield wrote:
sr*************@gmail.com said:
Hi,
i checked this piece of code with both gcc as well as vc++
compilers. Behaviour is same. I don't understand the behaviour. Can
some1 please explain the reason of the result.

#include<stdio.h>
#define TOTAL_ELEMENTS (sizeof(array) / sizeof(array[0]))
int array[] = {23,34,12,17,204,99,16};
int main()
{
int d = -1;

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

return 0;
}

It never enters into the loop.

I try very hard to steer clear of integer promotion questions, but this one
looks easy enough, so I'll have a crack at it (but keep an eye out for a
correction from others here).

It seems to me that you've been bitten by promotion rules. The sizeof
operator yields a size_t value, which is of course unsigned. It appears
that d is being promoted to size_t for the purposes of the comparison, and
(size_t)-1 is absolutely huge.

Solution: use size_t for d rather than int, and start it at 0 rather than
-1, and change d+1 to d in the loop body.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Jul 13 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.