#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    60
    Rep Power
    11

    why is it that every time I copy/paste some code into VC++ 6 it will NEVER compile?


    I tried a lot of things..take for instance this one:
    Code:
    // crt_rand.c
    /* This program seeds the random-number generator
     * with the time, then displays 10 random integers.
     */
    
    #include <stdlib.h>
    #include <stdio.h>
    #include <time.h>
    
    int main( void )
    {
       int i;
    
       /* Seed the random-number generator with current time so that
        * the numbers will be different every time we run.
        */
       srand( (unsigned)time( NULL ) );
    
       /* Display 10 numbers. */
       for( i = 0;   i < 10;i++ )
          printf( "  %6d\n", rand() );
    }
    it gives :
    Code:
    visual studio projects\rand\rand.cpp(22) : warning C4508: 'main' : function should return a value; 'void' return type assumed
    
    rand.obj - 0 error(s), 1 warning(s)
    what is wrong? I copied it directly from the NSDN collection..
  2. #2
  3. I'm Baaaaaaack!
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    5,538
    Rep Power
    244
    NSDN or MSDN? I find I have very poor results getting MS's own examples to compile in their own compiler, so join the club. In this particular case your compiler is complaining because the bonehead who wrote the example didn't put a return statement at the bottom of main. Just before the final brace, add a 'return 0;' and things should run fine. Some compilers give errors, others warnings, and others don't give a damn, so it really depends. You do realize, I hope, that warnings are just that? You should have an executeable that runs fine.

    My blog, The Fount of Useless Information http://sol-biotech.com/wordpress/
    Free code: http://sol-biotech.com/code/.
    Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
    Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.
    LinkedIn Profile: http://www.linkedin.com/in/keithoxenrider

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    60
    Rep Power
    11
    :) I tried to put things like return something and have main (something) in sted of void..but that didnīt work:) thanks!
  6. #4
  7. I'm Baaaaaaack!
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    5,538
    Rep Power
    244
    You are not making sense to me. According to your post, the program compiled completely, just nagged that it wanted a return value from main(). I told you how to get rid of the warning. What exactly are you having a problem with?

    My blog, The Fount of Useless Information http://sol-biotech.com/wordpress/
    Free code: http://sol-biotech.com/code/.
    Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
    Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.
    LinkedIn Profile: http://www.linkedin.com/in/keithoxenrider

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    60
    Rep Power
    11
    sorry for not being clear. Before you told me to put return 0, I tried what i wrote above, and THAT did not work.

    using return 0 DID work:)

    thanks again
  10. #6
  11. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,145
    Rep Power
    2222
    The basic rule in C is that if you give the function a non-void return type then you must return a value. This applies to every single function you will ever write, including main().

    The special thing about main() is that the return value is the program's exit code. It's used all the time in UNIX/Linux, so you are practically required to declare main() as int and never void. An exit code of zero means that the program ran successfully. If the program encounters an error, like an input file failed to open, then you call exit() with a non-zero exit code.

    While DOS supports reading the exit code, it was rarely done, so we got lazy in DOS and just declared main() to return void and so we never got used to including a return statement in main().

    Either way, it looks like whoever wrote that sample code had never tried to compile it.

    BTW, this seems like a common mistake for C newbies to make. Experienced C programmers also make that same mistake, only we know what that error message means so we can correct it on our own and never let on that we made a mistake.

IMN logo majestic logo threadwatch logo seochat tools logo