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

output

P: n/a
o/p of this is nothing .. can u explain why.. i know its gonna
something to do with signed and unsigned..

#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;
}

Apr 7 '06 #1
Share this Question
Share on Google+
10 Replies


P: n/a
Abhishek Parwal wrote:
o/p of this is nothing .. can u explain why.. i know its gonna
something to do with signed and unsigned..

#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;
}


Please search the archives before posting, this specific question has
been discussed several time before:

http://groups.google.com/group/comp....f96e862940333b
http://groups.google.com/group/comp....1a62b1532e620d
http://groups.google.com/group/comp....18e466cd0ae5c5

Robert Gamble

Apr 7 '06 #2

P: n/a
Abhishek Parwal wrote:
o/p of this is nothing .. can u explain why.. i know its gonna
something to do with signed and unsigned..

#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++) <<< sizeof is type size_t, not int for(d=-1;d <=(int) (TOTAL_ELEMENTS-2);d++) printf("%d\n",array[d+1]);

return 0;
}


So here, size_t appears to be unsigned thus 'd' gets promoted to
unsigned for the comparison and -1 is a very big positive number,
bit-wise. You could use a little indenting too. Also, array can be
defined within main().
--
- Mark

Apr 7 '06 #3

P: n/a
No, it has nothing to do with the unsigned or signed because int is by
default taken by the compiler as signed ( this however maybe
implementation dependent or compile dependent).
You can check this by prefixing the declaration with signed keyword.

The problem lies with the # define statement in you code which is an
expression. Thereby it cannot be compared in the for loop with the
variable d, resulting in no loop processing.

Apr 7 '06 #4

P: n/a
shishir wrote:
No, it has nothing to do with the unsigned or signed because int is by
default taken by the compiler as signed ( this however maybe
implementation dependent or compile dependent).
You can check this by prefixing the declaration with signed keyword.

The problem lies with the # define statement in you code which is an
expression. Thereby it cannot be compared in the for loop with the
variable d, resulting in no loop processing.


1. Please read <http://cfaj.freeshell.org/google/> before posting
another followup message.

2. You might consider some knowlege of C a prerequisite to answering
questions about C. Every single statement in your response is
incorrect:

a. It has everything to do with signed/unsigned
b. Whether int is signed or unsigned is not implementation dependent,
it is always signed
c. There is nothing wrong with the #define statement, your explanation
is nonsense.

Robert Gamble

Apr 7 '06 #5

P: n/a
shishir wrote:
No, it has nothing to do with the unsigned or signed because int is by
default taken by the compiler as signed ( this however maybe
implementation dependent or compile dependent).


Yes it does. Read my post and understand integer promotion.

--
- Mark

Apr 7 '06 #6

P: n/a
shishir wrote:

Learn to /quote/, for heaven's sake. Your message is incomprehensible
without its context. The OPs code was:

#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;
}
No, it has nothing to do with the unsigned or signed
Wrong.
because int is by default taken by the compiler as signed
By /definition/, not default.
( this however maybe implementation dependent or compile dependent).
No. Plain `int` is signed.
You can check this by prefixing the declaration with signed keyword.

The problem lies with the # define statement in you code which is an
expression. Thereby it cannot be compared in the for loop with the
variable d,


Completely wrong. Why do you think expressions cannot be compared in
a for-loop?

--
Chris "not a Tuvela" Dollin
"To say that the human is thus and so is almost always to lie
automatically."
Apr 7 '06 #7

P: n/a
shishir wrote:

Please provide context when replying. See
http://cfaj.freeshell.org/google/ and
http://www.safalra.com/special/googlegroupsreply/ for details.
No, it has nothing to do with the unsigned or signed
Wrong. It has everything to do with signed and unsigned types.
because int is by
default taken by the compiler as signed
True.
( this however maybe
implementation dependent or compile dependent).
If you don't even know the basics, why are you offering advice? int is
signed by definition.
You can check this by prefixing the declaration with signed keyword.
That checks nothing.
The problem lies with the # define statement in you code which is an
expression.
Wrong. The #define
#define TOTAL_ELEMENTS (sizeof(array) / sizeof(array[0]))
is a perfectly standard C technique.
Thereby it cannot be compared in the for loop with the
variable d, resulting in no loop processing.


Complete rubbish. You can use as complex an expression as you like.

This question has been correctly answered here many times in the past, I
suggest you read the other posts in this thread (if you see them) or
search the archive.
--
Flash Gordon, living in interesting times.
Web site - http://home.flash-gordon.me.uk/
comp.lang.c posting guidelines and intro:
http://clc-wiki.net/wiki/Intro_to_clc
Apr 7 '06 #8

P: n/a
In article <11*********************@v46g2000cwv.googlegroups. com> "Robert Gamble" <rg*******@gmail.com> writes:
Abhishek Parwal wrote:
o/p of this is nothing .. can u explain why.. i know its gonna
something to do with signed and unsigned..

#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;
}


Please search the archives before posting, this specific question has
been discussed several time before:

http://groups.google.com/group/comp....f96e862940333b
http://groups.google.com/group/comp....1a62b1532e620d
http://groups.google.com/group/comp....18e466cd0ae5c5


Interestingly, this is the fourth time this exact same program comes up
here. Is it a quiz question?
--
dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
Apr 7 '06 #9

P: n/a
Dik T. Winter wrote:
In article <11*********************@v46g2000cwv.googlegroups. com> "Robert Gamble" <rg*******@gmail.com> writes:
> Abhishek Parwal wrote:
> > o/p of this is nothing .. can u explain why.. i know its gonna
> > something to do with signed and unsigned..
> >
> > #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;
> > }

>
> Please search the archives before posting, this specific question has
> been discussed several time before:
>
> http://groups.google.com/group/comp....f96e862940333b
> http://groups.google.com/group/comp....1a62b1532e620d
> http://groups.google.com/group/comp....18e466cd0ae5c5


Interestingly, this is the fourth time this exact same program comes up
here. Is it a quiz question?


http://www.gowrikumar.com/c/cquestions.html

Second question from the top.

Robert Gamble

Apr 7 '06 #10

P: n/a
Groovy hepcat Abhishek Parwal was jivin' on 7 Apr 2006 05:49:37 -0700
in comp.lang.c.
output's a cool scene! Dig it!
o/p of this is nothing .. can u explain why.. i know its gonna
something to do with signed and unsigned..
Right.
#include<stdio.h>

#define TOTAL_ELEMENTS (sizeof(array) / sizeof(array[0]))
sizeof returns a size_t. The expression in this macro, therefore,
yields a size_t. Aritmetic on two size_t objects yields a size_t,
naturally. Now, size_t is an unsigned integral type.
int array[] = {23,34,12,17,204,99,16};

int main()
{
int d;
for(d=-1;d <=(TOTAL_ELEMENTS-2);d++)


When you campare two objects of differing types, they must be
converted to a common type, according to certain rules called "usual
arithmetic conversion". The result of comparing a size_t to an int is
size_t. The negative int is converted, therefore, to a positive
unsigned value greater than TOTAL_ELEMENTS, according to another set
of rules. The comparison always returns false, therefore.

--

Dig the even newer still, yet more improved, sig!

http://alphalink.com.au/~phaywood/
"Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
I know it's not "technically correct" English; but since when was rock & roll "technically correct"?
Apr 9 '06 #11

This discussion thread is closed

Replies have been disabled for this discussion.