Thread: Return 0 undeclared?

  1. #1
    CIS and business major
    Join Date
    Aug 2002
    Posts
    287

    Return 0 undeclared?

    Code:
    main()
    {
        int sum;
        /* Compute result */
        sum = 25 + 37 - 19;
        /* Display Results */
        printf("The answer is %i\n",sum);
        Return 0;
    }
    Why does my compiler tell me that Return 0 is undeclared? I've never seen this before.

  2. #2
    Registered User
    Join Date
    Sep 2014
    Posts
    364
    C is case sensitive. 'Return' is not the same as 'return'.
    Other have classes, we are class

  3. #3
    Nasal Demon Xupicor's Avatar
    Join Date
    Sep 2010
    Location
    Poland
    Posts
    179
    What WoodSTokk wrote is indeed true.

    Also - int main()

  4. #4
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,106
    Quote Originally Posted by Xupicor View Post
    What WoodSTokk wrote is indeed true.

    Also - int main()
    Actually, to be thorough, it should be written as:
    Code:
    int main(void)
    {
       // ...
      
      return 0; 
    
      /* or better:
         return EXIT_SUCCESS;
         Declared in stdlib.h
      */
    }

  5. #5
    Nasal Demon Xupicor's Avatar
    Join Date
    Sep 2010
    Location
    Poland
    Posts
    179
    True, int main(void) - it is C after all. My bad.
    Since C99 you can safely omit return 0; - and I'd argue that EXIT_SUCCESS is equivalent, but is it strictly better?
    5.1.2.2.3 Program termination
    1 If the return type of the main function is a type compatible with int, a return from the initial call to the main function is equivalent to calling the exit function with the value returned by the main function as its argument; reaching the } that terminates the main function returns a value of 0.

    7.22.4.4 The exit function
    5 [...] If the value of status is zero or EXIT_SUCCESS, an implementation-defined form of the status successful termination is returned.
    I mean, sure, the issue is it's a magic number - and a named constant is a better way, but it's so widely known magic number - and is used by the standard itself - that by this point there's very little magic to it.

  6. #6
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Out of curiosity, that quote from 7.22.4.4. applies also to the exit function being called from within a function other than main?
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Yes, since the "exit function causes normal program termination to occur", without regard to which function it is called from.
    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

  8. #8
    Nasal Demon Xupicor's Avatar
    Join Date
    Sep 2010
    Location
    Poland
    Posts
    179
    It doesn't address the issue directly [edit - I may have pulled the trigger too early, indeed, as laserlight says:]
    The exit function causes normal program termination to occur.
    thus the only interpretation that makes sense is that exit() works as is laid out when called, no matter where it is called from. (Remembering that part of the rules is that you cannot call it twice, or call exit() and quick_exit(), since that's UB. So there shall be no calls to exit() in functions registered using atexit() or at_quick_exit().)

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    > I'd argue that EXIT_SUCCESS is equivalent, but is it strictly better?

    It's more of a programming style thing than any objective "betterness". I'd favor consistency though; if you use EXIT_FAILURE anywhere, then favor EXIT_SUCCESS as well. Otherwise, returning 0 from main is so pervasively understood that being a magic number is somewhat of a non-issue.
    My best code is written with the delete key.

  10. #10
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,106
    Quote Originally Posted by Prelude View Post
    > I'd argue that EXIT_SUCCESS is equivalent, but is it strictly better?

    It's more of a programming style thing than any objective "betterness". I'd favor consistency though; if you use EXIT_FAILURE anywhere, then favor EXIT_SUCCESS as well. Otherwise, returning 0 from main is so pervasively understood that being a magic number is somewhat of a non-issue.
    It is more than that. There are some O/Ss that do not or may not use a return value of 0 to indicate success, and a specific non-zero value to indicate failure. Using the constants insure the code will work correctly no matter what O/S it is compiled for.

    Also, it is more self documenting to use the constants.
    Last edited by rstanley; 10-16-2016 at 07:43 AM.

  11. #11
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,106
    Quote Originally Posted by Xupicor View Post
    True, int main(void)... Since C99 you can safely omit return 0; - and I'd argue that EXIT_SUCCESS is equivalent, but is it strictly better?
    I wish everyone were using compilers that adhere to C99 or C11, but unfortunately, not the case.

    It is amazing how many people in the world are learning C on Turbo-C, hopefully NOT version 1.0! I am currently using gcc version 6.2.0, and use C11 by default, and I still prefer to use "return EXIT_SUCCESS;" from all my main functions, plus anyone I am teaching or advising, along with "int main(void)" for any program not using command line arguments.

    If you wish to omit it, then fine, but since many of the readers of this forum are learning C, I prefer to be more thorough.

  12. #12
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    There are some O/Ss that do not or may not use a return value of 0 to indicate success,
    From the C99 draft standard.
    7.22.4.4 The exit function

    Synopsis

    #include <stdlib.h>
    _Noreturn void exit(int status);

    Description

    ...

    5 Finally, control is returned to the host environment. If the value of status is zero or
    EXIT_SUCCESS, an implementation-defined form of the status successful termination is
    returned. If the value of status is EXIT_FAILURE, an implementation-defined form
    of the status unsuccessful termination is returned. Otherwise the status returned is
    implementation-defined.
    So both EXIT_SUCCESS and zero indicate the same thing as far as the C standard is concerned. EXIT_FAILURE is a different matter, returning anything other that EXIT_FAILURE may not work properly. For example even though main() is defined to return an int and exit() has an int parameter some operating systems only allow values that can be represented by a signed char.

  13. #13
    Nasal Demon Xupicor's Avatar
    Join Date
    Sep 2010
    Location
    Poland
    Posts
    179
    The important part to notice is that even though you return 0 from main (either implicitly, or explicitly) the successful termination status that the actual compiled program returns is implementation-defined. It may be that on some OS successful termination status value is 1, and exactly that will be returned from the program whether 0 or EXIT_SUCCESS will be used as an operand of return in main() or passed to exit().

    Again, as long as the compiler is standard conforming, 0 is always a safe value for successful termination, no matter what value a particular OS uses.

  14. #14
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    The discussion is interesting for another (ironic) reason. As we come to discuss the standard defined behaviour, we come full circle to post #4 and should realize by now none of that is really necessary.

    On many cases, being thorough doesn't convey a special level of correctness. The advise on post #3 is enough. The void argument isn't really required and even returning 0 (whether from the return statement or exit function) at the end of a main function is an exercise in futility since the C standard dictates the compiler should already do that for us. (We actually didn't discuss that particular, but it was on everyone's mind).
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Mario F.
    The void argument isn't really required
    In theory, the void parameter may be required by some later version of the C standard since:
    Quote Originally Posted by C11
    6.11.6 Function declarators

    The use of function declarators with empty parentheses (not prototype-format parameter type declarators) is an obsolescent feature.

    6.11.7 Function definitions

    The use of function definitions with separate parameter identifier and declaration lists (not prototype-format parameter type and identifier declarators) is an obsolescent feature.
    My reasoning is that the void parameter would fall under "prototype-format parameter type declarators", whereas an empty parameter list would actually be an empty identifier list, and hence is obsolescent under 6.11.7. In practice I think they will retain this status for a long while since there's some convenience for allowing what C++ allows (and in fact typically C++ programmers prefer not to use void) to cater to programs intended to be compiled as C and C++ as the case may be.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Return pointer to a vector of objects as a function return
    By Adaptron in forum C++ Programming
    Replies: 14
    Last Post: 04-07-2016, 09:23 AM
  2. Return 0; undeclared?
    By litzkrieg in forum C Programming
    Replies: 9
    Last Post: 01-23-2011, 10:19 PM
  3. Replies: 12
    Last Post: 04-07-2007, 11:11 AM
  4. cout undeclared/cin undeclared
    By akbigchillin in forum C++ Programming
    Replies: 6
    Last Post: 04-24-2006, 04:06 PM
  5. Replies: 6
    Last Post: 04-09-2006, 04:32 PM

Tags for this Thread