problem with initiallisation

Discussion in 'C Programming' started by vim, May 15, 2006.

  1. vim

    vim Guest

    The expected output of the following C program is to print the elements
    in the array. But when actually run, it doesn't do so.
    #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;
    }
     
    vim, May 15, 2006
    #1
    1. Advertising

  2. vim

    KOFKS Guest

    It seems the problem of "d = -1".
    If you use d = 0 instead, all is ok.(Of course, you should modify other
    parts accordingly. )
    I do not know why either.
    Waiting for some expert to explain. TIA!
     
    KOFKS, May 15, 2006
    #2
    1. Advertising

  3. vim

    Eric Sosman Guest

    vim wrote On 05/15/06 11:20,:
    > The expected output of the following C program is to print the elements
    > in the array. But when actually run, it doesn't do so.
    > #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;
    > }


    TOTAL_ELEMENTS is of type size_t, which is an unsigned
    integer type, hence not the same type as the signed int d.
    When the comparison operator <= has operands of different
    types, it promotes one (or both) of them to a single common
    type, then compares the promoted values. In your case, the
    signed int d is being promoted to an unsigned type to match
    the size_t, and by the rules of unsigned arithmetic the
    promotion produces a very large number. The loop test fails
    on the very first attempt, and the loop never executes.

    Some compilers will warn about this sort of thing; read
    your compiler's documentation to learn how to crank up the
    warning levels. For gcc, I suggest "-W -Wall -ansi -pedantic",
    possibly replacing "-ansi" with a different Standard version.

    --
     
    Eric Sosman, May 15, 2006
    #3
  4. vim

    Bill Pursell Guest

    vim wrote:
    > The expected output of the following C program is to print the elements
    > in the array. But when actually run, it doesn't do so.
    > #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;
    > }


    Try:
    #define TOTAL_ELEMENTS (int)(sizeof(array) / sizeof(array[0]))

    In the test in the for loop, the first comparison fails,
    probably because the RHS is being cast as
    an unsigned int, so -1 is promoted to 0xffffffff which is greater than
    5.
     
    Bill Pursell, May 15, 2006
    #4
  5. vim

    Flash Gordon Guest

    vim wrote:
    > The expected output of the following C program is to print the elements
    > in the array.


    You may expect that but I don't.

    > But when actually run, it doesn't do so.
    > #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;
    > }


    Try searching the Google archive of this group and you will find your
    question has been answered many times in the past. Since you are using
    Google anyway that should not be beyond your abilities.
    --
    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
     
    Flash Gordon, May 15, 2006
    #5
  6. vim

    Flash Gordon Guest

    KOFKS wrote:

    Please provide Context. See the bit about Google at
    http://clc-wiki.net/wiki/Intro_to_clc and the sites it links to.

    > It seems the problem of "d = -1".
    > If you use d = 0 instead, all is ok.(Of course, you should modify other
    > parts accordingly. )
    > I do not know why either.
    > Waiting for some expert to explain. TIA!


    The experts have explained it many times in the past. Try searching.
    --
    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
     
    Flash Gordon, May 15, 2006
    #6
  7. vim

    KOFKS Guest

    I see. So, that is it!

    I review the textbook to find out that it is int being promoted to
    unsigned int in a mixed expression other than the opposite.
    I used to take it for granted that unsigned integer will be promoted to
    int in mixed expression. What a mistake!

    Appreciate you for the help and kindness!
    Have a good day!
     
    KOFKS, May 15, 2006
    #7
  8. vim

    vim Guest

    Yes U r correct .I got that output.
    Thanks for giving good concept
     
    vim, May 16, 2006
    #8
  9. "vim" <> writes:
    > Yes U r correct .I got that output.
    > Thanks for giving good concept


    If I were deliberately trying to compose a badly written followup, it
    would look very much like what you just wrote.

    It's "you", not "U". It's "are", not "r". This is not a chat room or
    a cell phone; take the time to spell out words so people can read
    them. Many people here don't have English as their first language,
    and have a lot of difficulty reading abbreviated pseudo-English. Most
    of the rest of us just find it annoying (not a good idea if you're
    asking for help).

    And I find it difficult to believe that you haven't already been told
    to read <http://cfaj.freeshell.org/google/>. If you haven't read it,
    do so now, and follow its advice.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, May 17, 2006
    #9
    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. Andreas Suurkuusk
    Replies:
    0
    Views:
    4,001
    Andreas Suurkuusk
    Jul 27, 2003
  2. Ted Miller
    Replies:
    0
    Views:
    5,179
    Ted Miller
    Sep 13, 2003
  3. Merek
    Replies:
    0
    Views:
    1,980
    Merek
    Dec 3, 2003
  4. Scott Meddows
    Replies:
    1
    Views:
    404
    John Saunders
    Jun 8, 2004
  5. Mike

    Problem problem problem :( Need Help

    Mike, May 7, 2004, in forum: ASP General
    Replies:
    2
    Views:
    558
    Bullschmidt
    May 11, 2004
Loading...

Share This Page