Thread: return values from main

  1. #1
    Registered User
    Join Date
    Dec 2001
    Posts
    46

    return values from main

    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

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    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.

  3. #3
    Registered User
    Join Date
    Dec 2001
    Posts
    46
    *

    * - 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

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >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".

    Code:
    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.
    -Prelude
    My best code is written with the delete key.

  5. #5
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    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
    Yes, programs can read the return number of other programs...

    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

  6. #6
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    Can you set a variable in another program dependant upon the return value of main in another program??

    eg.

    int something = RunProgramFunction("whatever.exe")
    What do you think is contained in the int that is returned by system calls ?

    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to combine these working parts??
    By transgalactic2 in forum C Programming
    Replies: 0
    Last Post: 02-01-2009, 08:19 AM
  2. Another weird error
    By rwmarsh in forum Game Programming
    Replies: 4
    Last Post: 09-24-2006, 10:00 PM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. Certain functions
    By Lurker in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2003, 01:26 AM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM