What is the point of having main return a value?
Can you set a variable in another program dependant upon the return value of main in another program??
eg.
int something = RunProgramFunction("whatever.exe")
Just wonderin' :P
Thanks,
-Max
What is the point of having main return a value?
Can you set a variable in another program dependant upon the return value of main in another program??
eg.
int something = RunProgramFunction("whatever.exe")
Just wonderin' :P
Thanks,
-Max
The operating system uses that value for something*. If it doesn't get an int then the result is undefined.
* - ( Boring rant on how operating systems work removed for brevity )
-Prelude
My best code is written with the delete key.
*
* - would appreciate the rant, because i really would like to know what's the purpose of having any return value, and if you can have programs "interact" with one another by using values returned from main.
thanks,
-Max
>and if you can have programs "interact" with one another by using values returned from main
No. Well, yes, but it takes a LOT of work and is never worth it unless you're writing an operating system. As for why main returns a value, here's a snippet from a tutorial I'm working on called "C Programming: Sins, Heresy, and Other Such No-no's".
-PreludeCode:VOID MAIN --------- You may have seen programs that look something like this: void main ( void ) { printf ( "Hello, world!\n" ); } It looks okay, right? If you sprayed the monitor with a moutful of soda then you really don't need to read this section thoroughly. What's wrong with void main? Well, to begin with main is defined by the ISO C standard as returning an int. If a function is supposed to return a value then that value is probably important to the routine calling it, in this case the operating system. I'm sure somewhere in your operating system code there is a line that looks like this: returnValue = main ( argc, argv ); Okay, maybe not...but something similar. The variable that main sends its return value to could be very important. If I'm not making sense, let's try something a bit different. Consider the strcmp function in string.h. What if some bright spark decided to define strcmp as such: void strcmp ( const char * src, const char * dst ) { int ret = 0; while( !( ret = *src - *dst ) && *dst ) ++src, ++dst; if ( ret < 0 ) ret = -1; else if ( ret > 0 ) ret = 1; } That's pretty dumb, isn't it? How would the calling function know if the two strings were the same? Using void main is just as dumb, and will result in undefined behavior. That's not the way to live life as a programmer.
My best code is written with the delete key.
Yes, programs can read the return number of other programs...Originally posted by maxthecat
*
* - would appreciate the rant, because i really would like to know what's the purpose of having any return value, and if you can have programs "interact" with one another by using values returned from main.
thanks,
-Max
In Windows, you would use GetExitCodeProcess() which reads the exit code or the exception value (if your program crashed)
As the whether you sould use int or void....echo Prelude......you wouldnt try to recieve a return value from a void function in your own code as your compiler woud scream at you.......therefore the same applies to the main function........if the operating system's runtime expectes an int....give it one
What do you think is contained in the int that is returned by system calls ?Can you set a variable in another program dependant upon the return value of main in another program??
eg.
int something = RunProgramFunction("whatever.exe")
int something = system("whatever.exe");
It's not the best way, but the easiest to demonstrate the concept. Calls the command interpreter, executes whatever.exe and returns the value that was returned from whatever.exe to the command interpreter.
hth
-nv
She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."
When in doubt, read the FAQ.
Then ask a smart question.