return 0; question???

This is a discussion on return 0; question??? within the C Programming forums, part of the General Programming Boards category; On the first programs I compiled, like this: Code: #include <stdio.h> int main() { printf("Hello world!\n"); getchar(); return(0); } there ...

  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 09:28 PM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,166
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,531
    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 in reply to you, it is likely you deserve it. Suck it up, sunshine, 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, 11:22 AM
  2. A question about 'Return Value'
    By meili100 in forum C++ Programming
    Replies: 9
    Last Post: 06-05-2007, 01:44 AM
  3. Return variables question
    By mike11 in forum C++ Programming
    Replies: 10
    Last Post: 06-20-2005, 03:55 PM
  4. Return question
    By Paninaro in forum C Programming
    Replies: 2
    Last Post: 06-18-2002, 12:25 PM
  5. sorry about this question but I'm very curious (return)
    By Stealth in forum C++ Programming
    Replies: 4
    Last Post: 10-16-2001, 09:23 PM

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