473,499 Members | 1,610 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

can someone explane to the behaviour

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
5 1400
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2911
by: Joona I Palaste | last post by:
AFAIK the C standard divides behaviour of different things into four classes. 1) Defined behaviour. The implementation must do exactly what the standard says. 2) Implementation-defined behaviour....
25
3051
by: Nitin Bhardwaj | last post by:
Well, i'm a relatively new into C( strictly speaking : well i'm a student and have been doing & studying C programming for the last 4 years).....and also a regular reader of "comp.lang.c" I...
31
2587
by: DeltaOne | last post by:
#include<stdio.h> typedef struct test{ int i; int j; }test; main(){ test var; var.i=10; var.j=20;
3
1736
by: Guy Penfold | last post by:
Hi all, I have an asp.net application exhibiting rather alarming behaviour. Among other things, the application provides actors with an online profile that allows them to showcase their skills,...
9
1155
by: M. Posseth | last post by:
i have 3 forms Public Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim frm As New Form2 frm.Show(Me) End Sub...
14
1346
by: Peter Morris [Droopy eyes software] | last post by:
object a = 5; object b = 5; if (a != b) throw new InvalidOperationException("a != b"); Why is the exception thrown? I guessed it is something to do with boxing because the following does not...
26
2157
by: Frederick Gotham | last post by:
I have a general idea of the different kinds of behaviour described by the C Standard, such as: (1) Well-defined behaviour: int a = 2, b = 3; int c = a + b; (Jist: The code will work...
6
962
by: pigeonrandle | last post by:
Hi, I have been trying to find an answer, even a hint, but despite posting the following question on various forums i haven't even had a single reply for nearly two weeks :o(. "Why do...
285
8623
by: Sheth Raxit | last post by:
Machine 1 : bash-3.00$ uname -a SunOS <hostname5.10 Generic_118822-30 sun4u sparc SUNW,Sun-Fire-280R bash-3.00$ gcc -v Reading specs from /usr/local/lib/gcc-lib/sparc-sun-solaris2.8/2.95.3/...
0
7132
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7009
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7390
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4919
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4602
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3103
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1427
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
665
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
302
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.