Thread: Code Confusion - return 0;

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    1

    Code Confusion - return 0;

    I'm running through the tutorials for c programming on this site, and have noticed an inconsistency in the examples, that haven't been explained (at least thus far in the tutorials, I'm on chapter 6 right now). In the first chapter or so, I'm told that the main() function should have "return 0" at the end of it. Then, after a couple examples, at least to chapter 6, you don't see return 0; at the bottom of the main() function in the examples anymore.

    Is this a do as I say, not as I do, is it just not necessary, or has it been assumed that I will plug this in when I do the example, or create my own code?

    Thanks for any assistance!

    Q

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    The main function should return an integer. You can return any integer value (you should return something), but the values your OS expects are typically what is defined as EXIT_SUCCESS, EXIT_FAILURE, or 0. Zero and EXIT_SUCCESS are typically the same thing.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    In C89 (the first standard for C), return 0; is always necessary.

    In C99 and later, you can omit the return 0 (although main must still be declared as returning int), and the compiler will automatically add return 0; for you. Not everyone has a C99 compiler, or even if they do, they're not interested in writing C99 code.

    The tutorials are wrong to omit the return 0;

    Reading the code as snippets rather than as programs is perhaps better. That is, as demonstrations of use in context rather than as programs you can simply copy and paste.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    When a function returns a non-void value (and main always should), it's standard to have it return something. At the very least, it suppresses warnings on some compilers, but it becomes increasingly important for other functions for the purpose of error checking.

    There are many "conventions" that are omitted in examples for brevity, but the people who wrote the examples expect you to know and use them.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 11-07-2011, 05:07 PM
  2. OK, guys, here is my code to stop confusion
    By meili100 in forum C++ Programming
    Replies: 10
    Last Post: 11-29-2007, 04:06 PM
  3. Confusion : Warning long code
    By mart_man00 in forum C Programming
    Replies: 10
    Last Post: 04-08-2003, 08:07 PM
  4. How to return the ASCII code value of a character in C?
    By DramaKing in forum C Programming
    Replies: 3
    Last Post: 02-11-2002, 12:06 PM

Tags for this Thread