September 30th, 2003, 07:08 AM
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:
it gives :
/* This program seeds the random-number generator
* with the time, then displays 10 random integers.
int main( void )
/* 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() );
what is wrong? I copied it directly from the NSDN collection..
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)
September 30th, 2003, 07:26 AM
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.
September 30th, 2003, 10:04 AM
:) I tried to put things like return something and have main (something) in sted of void..but that didnīt work:) thanks!
September 30th, 2003, 10:06 AM
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?
September 30th, 2003, 01:26 PM
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:)
September 30th, 2003, 01:47 PM
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.