output

  • Thread starter Abhishek Parwal
  • Start date
A

Abhishek Parwal

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

Robert Gamble

Abhishek said:
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...0c524f8fd3/12f96e862940333b?#12f96e862940333b
http://groups.google.com/group/comp...c3ce757cfa/b91a62b1532e620d?#b91a62b1532e620d
http://groups.google.com/group/comp...ae7962f381/2118e466cd0ae5c5?#2118e466cd0ae5c5

Robert Gamble
 
M

Mark Odell

Abhishek said:
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().
 
S

shishir

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

Robert Gamble

shishir said:
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
 
M

Mark Odell

shishir said:
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.
 
C

Chris Dollin

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?
 
F

Flash Gordon

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

Dik T. Winter

> > 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...0c524f8fd3/12f96e862940333b?#12f96e862940333b
> http://groups.google.com/group/comp...c3ce757cfa/b91a62b1532e620d?#b91a62b1532e620d
> http://groups.google.com/group/comp...ae7962f381/2118e466cd0ae5c5?#2118e466cd0ae5c5

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

Robert Gamble

Dik said:
Abhishek said:
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...0c524f8fd3/12f96e862940333b?#12f96e862940333b
http://groups.google.com/group/comp...c3ce757cfa/b91a62b1532e620d?#b91a62b1532e620d
http://groups.google.com/group/comp...ae7962f381/2118e466cd0ae5c5?#2118e466cd0ae5c5

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
 
P

Peter Shaggy Haywood

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"?
 

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
473,770
Messages
2,569,583
Members
45,072
Latest member
trafficcone

Latest Threads

Top