return value from main()

This is a discussion on return value from main() within the C Programming forums, part of the General Programming Boards category; main's return value is of type int. I've read that the value is returned to the OS(meaning?). Is there any ...

  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    24

    return value from main()

    main's return value is of type int.
    I've read that the value is returned to the OS(meaning?).
    Is there any possibility of using this value in a C program?
    I have'nt come across any feature in C which does....

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    The return value is passed back to the invoker when the program terminates. It's nice to tell the invoker how you went.
    system() for example might use such a value: http://www.cplusplus.com/reference/c...ib/system.html

    and you might want to write a little math program or something, eg:
    Code:
    #include <stdlib.h>
    
    int main(int argc, char* argv[])
    {
        int res = 0;
        if(argc >= 3)
            res = atoi(argv[1]) + atoi(argv[2]);
        return res;
    }
    Which pointlessly adds 2 numbers like so:
    ./pointless.exe 5 10
    = 15

  3. #3
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    I've read that the value is returned to the OS(meaning?).
    When you write some shell script or bat-file calling external program you'll want to add some branching depending on the success/failure of the action you just executed...
    That's why in the batch file you can exemine the return code of the program and use it in the if conditions...
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  4. #4
    Eager young mind
    Join Date
    Jun 2006
    Posts
    342
    Suppose i do
    Code:
      int rv;
      rv=system("./hello");
    The parameter rv will hold 0 if system() has executed correctly. But, one of his questions were :
    "s there any possibility of using this value in a C program?"
    But, "rv" is not actually the return value of my "hello" program., right? And something like this is easily done in shell scritps as vart suggested. Can we do it in C?
    In the middle of difficulty, lies opportunity

  5. #5
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    rv can indeed be the return value of the program. Whether it is or not depends upon other factors, such as whether or not the command you ran was actually able to be executed or not.

    I wrote a post on the return value of main(), with regard to why it is important to declare main() as returning an int, and to return an int that properly reflects the state of the program when it reliquishes control and terminates. This is somewhat intermediate to advanced, but you can try the programs I have in the post. They may not work, since they are mainly proof-of-concept programs. As you can see, I used the return value of system() to read the return value of the command being executed, which can indeed be an actual C program:

    [C Video Tutorial Collection]

  6. #6
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    Quote Originally Posted by kris.c View Post
    Suppose i do
    Code:
      int rv;
      rv=system("./hello");
    The parameter rv will hold 0 if system() has executed correctly. But, one of his questions were :
    "s there any possibility of using this value in a C program?"
    But, "rv" is not actually the return value of my "hello" program., right? And something like this is easily done in shell scritps as vart suggested. Can we do it in C?
    If we use some advanced API like CreateProcess in Windows we can afterwards retreive the exit code of the process using corresponding function http://msdn2.microsoft.com/en-us/library/ms683189.aspx
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,045
    There's also a POSIX function called spawn*() (spawnl, spawnp, spawnv etc, depending on what you want) that allows you to get the return value of a process. Just use mode P_WAIT.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #8
    Massively Single Player AverageSoftware's Avatar
    Join Date
    May 2007
    Location
    Buffalo, NY
    Posts
    141
    Quote Originally Posted by megastar View Post
    main's return value is of type int.
    I've read that the value is returned to the OS(meaning?).
    Note that (despite what many people think) the requirement for main() to return int only applies to what the C standard called "hosted environments," which usually means that your program is executed by an operating system.

    In non-hosted environments, typically embedded systems, the name, return type, and parameters of the "main" function are implementation defined.

    I've done some embedded software in which main() was required to return void, which is perfectly legal in those cases. (C99 5.1.2.1, in case anyone is interested)
    There is no greater sign that a computing technology is worthless than the association of the word "solution" with it.

  9. #9
    Registered User
    Join Date
    Jun 2007
    Posts
    24
    hi all!
    The replies have been interesting.
    From all of them i could gather the following.
    Correct me if I am wrong.

    Return value of main is passed back to the invoker after the
    progam terminates so i believe OS invokes the program (in an ordinary case)
    and finally the value is returned to it.Fine!

    As to my question where in OS i take it that it is stored in EAX register
    according to MacGyver.

    Now the second part of the question was whether it can be used in another
    C program.

  10. #10
    Registered User
    Join Date
    Jun 2007
    Posts
    24
    If i can put my question in a slightly more blunt way
    For example two program P1 and P2

    Code:
    P1
    int main()
    {
    int a;
    ...
    ...
    a=5;
    return 5;
    }
    
    P2
    {
    ..
    ...
    if(b>a)// a is the one from P1
    ...
    }

    1.
    @zacs7
    Your program returns 15 ok! and it may be utilized by 'system' function
    but how to retain it??

    2.
    @vart 1st post
    In response to my first part(i.e value returned to OS) your reply was how
    the value could be utilized and also you suggested examining the batch file
    to retrieve the value(isnt it?)

    @vart 2nd post and @dwks
    I am not very familiar with API's so excuse me if i get this wrong.
    What you have suggested is the following i guess

    1.Read return value of P1 using certain functions or procedures in API
    like GetExitCodeProcess and spawn*().
    2.From API retrieve the value for use into P2(??)

  11. #11
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,045
    As to my question where in OS i take it that it is stored in EAX register
    according to MacGyver.
    A register is probably not what you think it is. It's one of very few memory locations inside the CPU. But that doesn't really matter.

    Now the second part of the question was whether it can be used in another
    C program.
    Yes, the return value from one program can be used in another program.

    Does that answer your question? Well, yes, but your question should have been, how?

    What operating system are you using? Windows or Linux? If you're using Windows, execute the program with CreateProcess() (a link was posted to it by vart above). If you're using some other system like Linux or UNIX, check out the last post in this thread. http://www.programmersheaven.com/mb/...admessage.aspx

    [edit] You posted again while I was typing . . . okay:
    @vart 2nd post and @dwks
    I am not very familiar with API's so excuse me if i get this wrong.
    What you have suggested is the following i guess

    1.Read return value of P1 using certain functions or procedures in API
    like GetExitCodeProcess and spawn*().
    2.From API retrieve the value for use into P2(??)
    What I mean is this:
    Code:
    /* executor */
    #include <stdio.h>
    #include <stdlib.h>
    #include <process.h>  /* I think this is what spawnl() is in . . . */
    
    int main() {
        int retval = spawnl(P_WAIT, "executee.exe", NULL);
        printf("executor: &#37;d\n", retval);
        return 0;
    }
    Code:
    /* executee */
    #include <stdio.h>
    
    int main() {
        int r;
        printf("Enter the value to return: ");
        scanf("%d", &r);
        return r;
    }
    [/edit]
    Last edited by dwks; 06-28-2007 at 03:30 PM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  12. #12
    Registered User
    Join Date
    Jun 2007
    Posts
    24
    @MacGyver


    Code:
    int iRet;
    	char szProgram[256];
    	
    	while(getProgram(szProgram,sizeof(szProgram)))
    	{
    		iRet = system(szProgram);// HERE THE RETURN VALUE MAY BE ANYTHING !!
    		printf("Return value of \"%s\":\t%d\n\n",szProgram,iRet);
    	}

    Your idea is interesting getting the value from EAX register,
    but i think the part where you try to do
    iRet=system(szProgram);
    the RETURN VALUE OF THE SYSTEM NEED NOT BE THE RETURN VALUE OF PROGRAM.This was according to the link given by zacs7.
    I also ran your 3rd program and i was getting the output 32512 on passing
    the name of any file.
    http://www.cplusplus.com/reference/c...ib/system.html

    Any suggestions?

  13. #13
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,045
    I'd better start a new post.

    2.
    @vart 1st post
    In response to my first part(i.e value returned to OS) your reply was how
    the value could be utilized and also you suggested examining the batch file
    to retrieve the value(isnt it?)
    Yes. Here's an example sh script (I'm not sure about the batch syntax):
    Code:
    #!/bin/sh
    VALUE=$(program.exe)
    if [ $VALUE -gt 0 ]; then
        echo "Error encountered (return value of program.exe was more than 0)"
    fi
    Note that I rarely use shell scripting, so that might be wrong, but it's probably irrelevant since you want to do this in C, not sh.

    [edit] Arrg, you beat me again.

    Yes, the return value of system() is not the return value of the program. To get the return value of the program, use some other function like that Windows one or spawnl(). What OS are you using? [/edit]
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  14. #14
    Registered User
    Join Date
    Jun 2007
    Posts
    24
    @dwks
    It is the right header file I have to still check out
    your shellscript method..

  15. #15
    Registered User
    Join Date
    Jun 2007
    Posts
    24
    @dwks
    oops!
    sorry for posts in between did'nt check your
    posts..
    I am currently running my programs on a LINUX platform.
    I just ran your program.The header file "process.h" was not available..

Page 1 of 2 12 LastLast
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, 07:19 AM
  2. Another weird error
    By rwmarsh in forum Game Programming
    Replies: 4
    Last Post: 09-24-2006, 10:00 PM
  3. DirectInput help
    By Muphin in forum Game Programming
    Replies: 2
    Last Post: 09-10-2005, 11:52 AM
  4. Pong is completed!!!
    By Shamino in forum Game Programming
    Replies: 11
    Last Post: 05-26-2005, 10:50 AM
  5. Algorithm to walk through a maze.
    By Nutshell in forum C Programming
    Replies: 30
    Last Post: 01-21-2002, 12:54 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21