Thread: Whats wrong with this program?

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    83

    Whats wrong with this program?

    Code:
    #include<stdio.h>
    #defineTOTAL_ELEMENTS (sizeof(array) /sizeof(array[0]))intarray[] = {23,34,12,17,204,99,16};
    intmain()
      {
    intd;
    for(d=-1;d<= (TOTAL_ELEMENTS-2);d++)
    printf("%d\n",array[d+1]);
    return0;
      }


  2. #2
    Registered User
    Join Date
    Nov 2011
    Posts
    32
    You are using an identifier before declaring it.

    Identifiers must be first declared before using them in a function.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Swoorup
    Whats wrong with this program?
    According to my compiler:
    Code:
    test.c:2:2: invalid preprocessing directive #defineTOTAL_ELEMENTS
    test.c:4: warning: return type defaults to `int'
    test.c: In function `intmain':
    test.c:5: error: `intd' undeclared (first use in this function)
    test.c:5: error: (Each undeclared identifier is reported only once
    test.c:5: error: for each function it appears in.)
    test.c:6: error: `d' undeclared (first use in this function)
    test.c:6: error: `TOTAL_ELEMENTS' undeclared (first use in this function)
    test.c:7: error: `array' undeclared (first use in this function)
    test.c:8: error: `return0' undeclared (first use in this function)
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Nov 2011
    Posts
    83
    Sorry the code turned awkward when copying and pasting it.
    This is the one:
    Code:
    #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;
    }
    Still does not work though

  5. #5
    Registered User
    Join Date
    Nov 2011
    Posts
    32
    read the above post.

  6. #6
    Registered User
    Join Date
    Nov 2011
    Posts
    83
    No, it works though if this line
    for(d=-1;d <= (TOTAL_ELEMENTS-2);d++)
    is replace by for(d=0;d <= (TOTAL_ELEMENTS-2);d++)

    I just don't understand why d=-1 does not make the whole program loop and print the elements

  7. #7
    Registered User
    Join Date
    Nov 2011
    Posts
    32
    Dude, you won't get any output even if you change d=-1 to d=0.

  8. #8
    Registered User
    Join Date
    Nov 2011
    Posts
    83
    You don't believe me at all

    if d=0
    this is the output:
    34
    12
    17
    204
    99
    16
    Press any key to continue...

    And with d=-1
    Press any key to continue...

    Please do see for yourself. I had found the code somewhere but forgot where! It was somewhere from a c puzzle like site. But today I just got to stumble across it

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > I just don't understand why d=-1 does not make the whole program loop and print the elements
    Because the comparison is int <= size_t

    But since size_t is an unsigned number, the int gets promoted to unsigned as well, resulting in a comparison of
    if ( 0xffffffff <= 6 )
    which is obviously false, and therefore nothing happens.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  10. #10
    Registered User
    Join Date
    Nov 2011
    Posts
    83
    So the -1 gets converted to this 4294967295?
    That's really interesting but confusing at the same point.

    Thank you sir.

  11. #11
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Swoorup View Post
    You don't believe me at all
    What I believe is that you had a problem and rather than think it over a bit and fix it, you started throwing code at it to see what stuck...

    Code:
    #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 = 0; d < TOTAL_ELEMENTS; d++)
            printf("%d\n", array[d]);
    
         return 0;
      }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. whats wrong of this program
    By maniel in forum C Programming
    Replies: 7
    Last Post: 08-20-2003, 02:29 PM
  2. Whats wrong with this program?
    By Golden Bunny in forum C++ Programming
    Replies: 7
    Last Post: 05-18-2002, 04:34 AM
  3. whats wrong with this program?
    By gamesguy3000 in forum C++ Programming
    Replies: 1
    Last Post: 04-10-2002, 03:08 PM
  4. Whats wrong with my program?
    By Ruflano in forum C++ Programming
    Replies: 5
    Last Post: 02-21-2002, 05:09 PM
  5. whats wrong with this program please?
    By Leeman_s in forum C++ Programming
    Replies: 1
    Last Post: 02-11-2002, 09:39 PM