Thread: return 0; question???

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    26

    return 0; question???

    On the first programs I compiled, like this:

    Code:
    #include <stdio.h>
    
    
    int main()
    {
        printf("Hello world!\n");
        getchar();
        return(0);
    }
    there is a return function. However in this program I just compiled:

    Code:
    #include <stdio.h>
    
    
    int main()
    {
        int x;
        for (x = 0; x < 100; x++){
        printf("%d\n", x);
        }
        getchar();
    }
    there is not a return function (is return a "function?") to tell the program that it completed successfully, and as I type this I'm thinking it's because the second program is a loop? Did I just answer my own question?

    Thank you! haha
    Last edited by aosterminal; 04-24-2012 at 08:28 PM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by aosterminal
    is return a "function?"
    No, it is a keyword used to implement return statements. Consequently, you may write return 0; instead of return(0);

    Quote Originally Posted by aosterminal
    there is not a return function (...) to tell the program that it completed successfully, and as I type this I'm thinking it's because the second program is a loop? Did I just answer my own question?
    No, the loop is not relevant here. You declared main as returning an int, so it should be expected for you to return an int value. Returning 0 means success here. If you are compiling with respect to the 1999 edition of the C standard (or later), the lack of a return statement for the main function (as a special case) means return 0; at the end.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    No, you did not come close to answering your own question.

    Firstly, return is not a function. It is a keyword that, when used in a statement, causes the function to end, and return any value specified to the caller of the function. "return 0;" is the same as "return (0);" (for a function that returns int).

    Second, your second function is not a loop. It contains a variable definition, a loop construct, and a subsequent statement calling getchar(). However, the main() function receives special consideration in the C standard: if control reaches the end of main() (i.e. the closing curly brace) without a return statement, the effect is the same as if a "return 0;" statement had been placed immediately before the end of the function. Such things do not happen with other functions: falling off the end of any non-void function other than main() causes the value received by the caller to be undefined.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. An Easy Question about Return 0 or 1
    By Dubya in forum C++ Programming
    Replies: 3
    Last Post: 05-25-2011, 10:22 AM
  2. A question about 'Return Value'
    By meili100 in forum C++ Programming
    Replies: 9
    Last Post: 06-05-2007, 12:44 AM
  3. Return variables question
    By mike11 in forum C++ Programming
    Replies: 10
    Last Post: 06-20-2005, 02:55 PM
  4. Return question
    By Paninaro in forum C Programming
    Replies: 2
    Last Post: 06-18-2002, 11:25 AM
  5. sorry about this question but I'm very curious (return)
    By Stealth in forum C++ Programming
    Replies: 4
    Last Post: 10-16-2001, 08:23 PM