what is return 0; in english mean?

This is a discussion on what is return 0; in english mean? within the C Programming forums, part of the General Programming Boards category; Here is an example program that I wrote. I want to understand what "return 0;" is, and why I need ...

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    7

    what is return 0; in english mean?

    Here is an example program that I wrote. I want to understand what "return 0;" is, and why I need it and what it does.

    A friend answered this above question with:

    "in standard c, every function must have a return type. main returns an integer to the OS, and every other function can optionally return any type to where it was called at. if you dont need to return anything, you specify void. (for example, you can have a function that takes two arguements and adds them together, then returns an int as the result)"

    I dont understand most of this ^

    Code:
    #include <stdio.h>
    int main(void)
    {
    char age = 'A';
    
    printf("Then he said %d\n", age);
    return 0;
    }
    Here is another example program, how come in the function I made I did not have to write "return 0;" but in the main function I did?

    And how come I had to write "void" before "show(void);", because for the main funtion I dont have void written before it, but at the same time its returning something I guess? Where void doesnt need to return something, but what does return mean, as I think physical or some output....This is where I get confused.

    Code:
     
    #include <stdio.h>
    void show(void); 
    int main(void)
    {
    char *phrase;
    phrase = "For he is a jolly good fellow!";
    printf("%s\n%s\n%s\n",phrase,phrase,phrase);
    show();
    return 0;
    }
    
    void show(void) 
    {
    
     printf("Which nobdoy can deny!\n");
    }
    Last edited by scuzzo84; 09-20-2005 at 11:31 AM.

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,175
    Look at this:
    Code:
    int main(void)
    And then look at this:
    Code:
    void show(void)
    The keyword just before the function name is the return type of the function. So you're saying main() returns an int (and it should) and that show() returns void (which is nothing). If a function has void as its return type then you can't return a value, so you can't have return 0; inside that function.

    By the way, the value returned from main() is given to the calling program. So you can create a program that starts another program and capture its return value. The return value from main() is usually used to indicate whether or not the program succeeded or not. 0 historically indicates that the program succeeded, 1 indicates that it did not.
    Last edited by itsme86; 09-20-2005 at 11:27 AM.
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User cbastard's Avatar
    Join Date
    Jul 2005
    Location
    India
    Posts
    167
    return 0;
    it signifies that the program execution was successful.it is a macro defined in stdio.h as exit_success.
    when you write a single program it is not so much useful to return value but when you will call one program from other or system proramming then return from main is useful

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,175
    It tends to be really useful in a *NIX environment where scripting is a lot more commonplace. In fact, there's standard programs called true and false with the sole purpose of returning 0 and 1 respectively.
    If you understand what you're doing, you're not learning anything.

  5. #5
    Registered User
    Join Date
    Sep 2005
    Posts
    7
    thanks guys

  6. #6
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,257
    0 historically indicates that the program succeeded, 1 indicates that it did not.
    There is nothing historic about it, this is something that is specified in the standard. There is nothing in the standard about a program returning 1 though.

    it is a macro defined in stdio.h as exit_success
    Actually the macro is EXIT_SUCCESS and EXIT_FAILURE, and these are defined in stdlib.h, not stdio.h

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 10:42 PM
  2. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  3. problem with open gl engine.
    By gell10 in forum Game Programming
    Replies: 1
    Last Post: 08-21-2003, 05:10 AM
  4. opengl code not working
    By Unregistered in forum Windows Programming
    Replies: 4
    Last Post: 02-14-2002, 10:01 PM
  5. Algorithm to walk through a maze.
    By Nutshell in forum C Programming
    Replies: 30
    Last Post: 01-21-2002, 01:54 AM

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