return value from main

Discussion in 'C' started by abhishek.biradar, Sep 1, 2010.

  1. abhishek.biradar

    abhishek.biradar New Member

    Joined:
    Sep 1, 2010
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    check the following program
    /*a.c*/
    int main()
    {
    return 0;
    }
    returns 0 to the shell and echo $? gives 0
    /*b.c*/
    void main()
    {
    }
    echo $? in shell gives some different values for each execution what does it represents????
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    Garbage values
     
  3. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    It represents the fact that you haven't returned a value. The values are meaningless; you should not try to use them if the program doesn't return a value. But you should always return zero from main unless you have a good reason for returning some other specific value; zero indicates success, and for any script calling your program and relying on the return codes to check for errors a garbage return will cause undefined behaviour within the script.

    If you know for certain that your program will NEVER EVER be called by a script that checks return values, and that returning garbage will definitely have no negative effects, then go ahead and return garbage. Otherwise at least return 0; it's not a difficult line to add. Or return EXIT_SUCCESS; if you prefer.
     
  4. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    http://www.daweidesigns.com
    Always define main as returning an int, then return one. It's that simple.
     
  5. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    [comment]After long time and so welcome back.[/comment]
     
  6. LordN3mrod

    LordN3mrod New Member

    Joined:
    Sep 4, 2010
    Messages:
    22
    Likes Received:
    11
    Trophy Points:
    0
    Occupation:
    Software Developer
    Location:
    Yerevan, Armenia
    The current C++ standard mandates that the main() function return int. However you don't have to ACTUALLY return a value. If the control flows out of main without reaching a return statement, it is equivalent to return 0;
    On the other hand,
    void main() is ill-formed
     
    shabbir likes this.

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice