can someone explane to the behaviour

S

srinivas.satish

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.
 
C

Chris Dollin

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.
 
A

Ancient_Hacker

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

=?ISO-8859-1?Q?=22Nils_O=2E_Sel=E5sdal=22?=

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.
 
R

Richard Heathfield

(e-mail address removed) 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.
 
S

srinivas.satish

Hi,
thank U all for this clarification. Yes it is truly the behaviour.
Singed gets promoted to unsigned.


Richard said:
(e-mail address removed) 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)
 

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

No members online now.

Forum statistics

Threads
474,181
Messages
2,570,970
Members
47,537
Latest member
BellCorone

Latest Threads

Top