printing array elements

Discussion in 'C Programming' started by crazy, Oct 1, 2003.

  1. crazy

    crazy Guest

    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
     
    crazy, Oct 1, 2003
    #1
    1. Advertising

  2. crazy

    Mike Wahler Guest

    "crazy" <> 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
     
    Mike Wahler, Oct 2, 2003
    #2
    1. Advertising

  3. crazy

    Mike Wahler Guest

    "Mike Wahler" <> wrote in message
    news:0mJeb.11747$...
    > "crazy" <> 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
     
    Mike Wahler, Oct 2, 2003
    #3
  4. 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
     
    Andreas Kahari, Oct 2, 2003
    #4
  5. crazy

    crazy Guest

    "Andreas Kahari" <> wrote in message
    news:...
    > 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
     
    crazy, Oct 2, 2003
    #5
  6. "crazy" <> 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
     
    Peter Nilsson, Oct 2, 2003
    #6
  7. crazy

    Jack Klein Guest

    On Thu, 02 Oct 2003 00:02:58 GMT, "crazy" <>
    wrote in comp.lang.c:

    > "Andreas Kahari" <> wrote in message
    > news:...
    > > 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
     
    Jack Klein, Oct 2, 2003
    #7
  8. crazy

    Dan Pop Guest

    In <SWJeb.198212$> "crazy" <> 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:
     
    Dan Pop, Oct 2, 2003
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. diffused
    Replies:
    9
    Views:
    727
    Oscar kind
    Aug 1, 2004
  2. Shalini
    Replies:
    2
    Views:
    502
    Brian Genisio
    Jan 9, 2004
  3. P
    Replies:
    1
    Views:
    1,171
    Joe Kesselman
    Jul 7, 2006
  4. Replies:
    10
    Views:
    678
  5. Derek Basch

    Printing number of elements in an array

    Derek Basch, Apr 17, 2006, in forum: Perl Misc
    Replies:
    5
    Views:
    115
    Brian McCauley
    Apr 18, 2006
Loading...

Share This Page