Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > printing array elements

Reply
Thread Tools

printing array elements

 
 
crazy
Guest
Posts: n/a
 
      10-01-2003
Hi
It seems something really simple, but can't figure out.
It's not my homework , was just looking at some C problems on the web
and came across this one.
why doesn't the following code print anything?

#include <stdio.h>

#define TOTAL_ELEMENTS (sizeof(array) / sizeof(array[0]))
int array[] = {1,2,3,4,5,6,7};

int main()
{
int d;
for(d=-1;d <= (TOTAL_ELEMENTS - 2);d++)
printf("%d\n",array[d+1]);
return 0;
}

crazy



 
Reply With Quote
 
 
 
 
Mike Wahler
Guest
Posts: n/a
 
      10-01-2003
"crazy" <(E-Mail Removed)> wrote in message
news:qCIeb.477856$cF.162833@rwcrnsc53...
> Hi
> It seems something really simple, but can't figure out.
> It's not my homework , was just looking at some C problems on the web
> and came across this one.
> why doesn't the following code print anything?
>
> #include <stdio.h>
>
> #define TOTAL_ELEMENTS (sizeof(array) / sizeof(array[0]))
> int array[] = {1,2,3,4,5,6,7};
>
> int main()
> {
> int d;
> for(d=-1;d <= (TOTAL_ELEMENTS - 2);d++)


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

> printf("%d\n",array[d+1]);
> return 0;
> }


Read about 'signed', 'unsigned', and 'conversions'.

IMO it's poor practice to use negative offsets
with arrays like this. Or was this just a 'puzzle'
to be solved?

-Mike


 
Reply With Quote
 
 
 
 
Mike Wahler
Guest
Posts: n/a
 
      10-01-2003

"Mike Wahler" <(E-Mail Removed)> wrote in message
news:0mJeb.11747$(E-Mail Removed) link.net...
> "crazy" <(E-Mail Removed)> wrote in message
> news:qCIeb.477856$cF.162833@rwcrnsc53...
> > Hi
> > It seems something really simple, but can't figure out.
> > It's not my homework , was just looking at some C problems on the web
> > and came across this one.
> > why doesn't the following code print anything?
> >
> > #include <stdio.h>
> >
> > #define TOTAL_ELEMENTS (sizeof(array) / sizeof(array[0]))
> > int array[] = {1,2,3,4,5,6,7};
> >
> > int main()
> > {
> > int d;
> > for(d=-1;d <= (TOTAL_ELEMENTS - 2);d++)

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

>
> Read about 'signed', 'unsigned', and 'conversions'.
>
> IMO it's poor practice to use negative offsets
> with arrays like this. Or was this just a 'puzzle'
> to be solved?


Additonal hint: 'sizeof' returns an (impelemenation-
defined) unsigned type.

-Mike


 
Reply With Quote
 
Andreas Kahari
Guest
Posts: n/a
 
      10-01-2003
In article <qCIeb.477856$cF.162833@rwcrnsc53>, crazy wrote:
> Hi
> It seems something really simple, but can't figure out.
> It's not my homework , was just looking at some C problems on the web
> and came across this one.
> why doesn't the following code print anything?
>
> #include <stdio.h>
>
> #define TOTAL_ELEMENTS (sizeof(array) / sizeof(array[0]))
> int array[] = {1,2,3,4,5,6,7};
>
> int main()
> {
> int d;
> for(d=-1;d <= (TOTAL_ELEMENTS - 2);d++)
> printf("%d\n",array[d+1]);
> return 0;
> }


Cast "(TOTAL_ELEMENTS - 2)" to int and all will be fine.

To explore further, run this:

#include <stdio.h>
#include <stddef.h>

int
main()
{
size_t i = 10; /* unsigned integer type */
int j = -1; /* signed integer type */

if (j < i) { /* what gets converted to what? */
printf("j < i\n"); /* will not print */
} else {
printf("j >= i\n"); /* will print */
}

return 0;
}

C99 says, "if the operand that has unsigned integer type has
rank greater or equal to the rank of the type of the other
operand, then the operand with signed integer type is converted
to the type of the operand with unsigned integer type."

So, size_t must have the same or greater rank than int. In my
smaller example, the -1 in j gets converted into an unsigned
integer type. Try printing -1 cast to size_t and see what it
is.

--
Andreas Kähäri
 
Reply With Quote
 
crazy
Guest
Posts: n/a
 
      10-02-2003
"Andreas Kahari" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) g...
> In article <qCIeb.477856$cF.162833@rwcrnsc53>, crazy wrote:


----snipped----

>
> Cast "(TOTAL_ELEMENTS - 2)" to int and all will be fine.
>
> To explore further, run this:
>
> #include <stdio.h>
> #include <stddef.h>
>
> int
> main()
> {
> size_t i = 10; /* unsigned integer type */
> int j = -1; /* signed integer type */
>
> if (j < i) { /* what gets converted to what? */
> printf("j < i\n"); /* will not print */
> } else {
> printf("j >= i\n"); /* will print */
> }
>
> return 0;
> }
>


This example definitely helped understand what was wrong.

My mistake was printing d as %d and not as %u.
The moment I did that, it became very clear what was happening.


> C99 says, "if the operand that has unsigned integer type has
> rank greater or equal to the rank of the type of the other
> operand, then the operand with signed integer type is converted
> to the type of the operand with unsigned integer type."


What is meant by rank here? what is the rank precedence?


> So, size_t must have the same or greater rank than int. In my
> smaller example, the -1 in j gets converted into an unsigned
> integer type. Try printing -1 cast to size_t and see what it
> is.
>
> --
> Andreas Kähäri



-crazy



 
Reply With Quote
 
Peter Nilsson
Guest
Posts: n/a
 
      10-02-2003
"crazy" <(E-Mail Removed)> wrote in message news:<qCIeb.477856$cF.162833@rwcrnsc53>...
> why doesn't the following code print anything?
>
> #include <stdio.h>
>
> #define TOTAL_ELEMENTS (sizeof(array) / sizeof(array[0]))
> int array[] = {1,2,3,4,5,6,7};
>
> int main()
> {
> int d;
> for(d=-1;d <= (TOTAL_ELEMENTS - 2);d++)
> printf("%d\n",array[d+1]);
> return 0;
> }


Because size_t has equal or higher rank than an int on your machine.
Try experimenting with (-1 < 1u).

--
Peter
 
Reply With Quote
 
Jack Klein
Guest
Posts: n/a
 
      10-02-2003
On Thu, 02 Oct 2003 00:02:58 GMT, "crazy" <(E-Mail Removed)>
wrote in comp.lang.c:

> "Andreas Kahari" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) g...
> > In article <qCIeb.477856$cF.162833@rwcrnsc53>, crazy wrote:

>
> ----snipped----
>
> >
> > Cast "(TOTAL_ELEMENTS - 2)" to int and all will be fine.
> >
> > To explore further, run this:
> >
> > #include <stdio.h>
> > #include <stddef.h>
> >
> > int
> > main()
> > {
> > size_t i = 10; /* unsigned integer type */
> > int j = -1; /* signed integer type */
> >
> > if (j < i) { /* what gets converted to what? */
> > printf("j < i\n"); /* will not print */
> > } else {
> > printf("j >= i\n"); /* will print */
> > }
> >
> > return 0;
> > }
> >

>
> This example definitely helped understand what was wrong.
>
> My mistake was printing d as %d and not as %u.
> The moment I did that, it became very clear what was happening.
>
>
> > C99 says, "if the operand that has unsigned integer type has
> > rank greater or equal to the rank of the type of the other
> > operand, then the operand with signed integer type is converted
> > to the type of the operand with unsigned integer type."

>
> What is meant by rank here? what is the rank precedence?
>
>
> > So, size_t must have the same or greater rank than int. In my
> > smaller example, the -1 in j gets converted into an unsigned
> > integer type. Try printing -1 cast to size_t and see what it
> > is.
> >
> > --
> > Andreas Kähäri

>
>
> -crazy


The integer types have a ranking in C, such that each higher-ranked
integer type must be able to contain at least the range of values of
lesser ranked types, if not more. This is also subject to the minimum
allowable absolute range for each type.

Signed or unsigned short int must be able to hold all the valuer of
signed and unsigned char, respectively.

(un)signed int must be able to hold all the values of (un)signed
short.

(un)signed long must be able to hold all the values of (un)signed int.

(un)signed long long must be able to hold all the values of (un)signed
long.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
 
Reply With Quote
 
Dan Pop
Guest
Posts: n/a
 
      10-02-2003
In <SWJeb.198212$(E-Mail Removed) t> "crazy" <(E-Mail Removed)> writes:

>My mistake was printing d as %d and not as %u.


Nope, that was NOT your mistake: d has type int, therefore it CANNOT be
printed with %u, which expects an unsigned int!

>The moment I did that, it became very clear what was happening.


To do that properly, you must use (unsigned)d.

>> C99 says, "if the operand that has unsigned integer type has
>> rank greater or equal to the rank of the type of the other
>> operand, then the operand with signed integer type is converted
>> to the type of the operand with unsigned integer type."

>
>What is meant by rank here? what is the rank precedence?


At your current level, you can safely ignore the C99 lingo. Simply
read about the arithmetic conversions in your favourite C book.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
brochure printing,online yearbook,printing,books printing,publishing elie Computer Support 2 11-27-2010 12:12 PM
brochure printing,online yearbook,printing,books printing,publishing elie Computer Support 0 08-21-2007 05:52 AM
brochure printing,online yearbook,printing,books printing,publishing elie Computer Support 0 08-21-2007 05:50 AM
brochure printing,online yearbook,printing,books printing,publishing elie Computer Support 0 08-21-2007 05:28 AM
brochure printing,online yearbook,printing,books printing,publishing elie Computer Support 0 08-18-2007 10:11 AM



Advertisments