Thread: Newbie question

  1. #16
    Join Date
    Oct 2007
    Inside my computer
    Well, I can still only say the behavior is undefined.
    Visual Studio 2008 always xor's eax with eax, hence returning 0, whether you return void or you leave a return and make int main.
    It does allow for functions returning a value not containing a return statement, though. One of your examples with no return returned 5 due to printf.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  2. #17
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    If it's trying to comply with C99, then that may be why, but yes, you are right. It's undefined behavior.

  3. #18
    Registered User
    Join Date
    Jun 2005
    Quote Originally Posted by matsp View Post
    This will do the same thing as the call to main with a void return type - the return value is completely undefined (and thus will contain "any garbage").
    Sure, forcing the compiler to call a function that returns void as if it returns an int involves undefined behaviour. In some such circumstances, the code will fail to compile, but (for those who enjoy playing in the realm of undefined behaviour) it is possible to force the issue by use of some casting
    Quote Originally Posted by matsp View Post
    Of course, in C++ this won't even work, since the name mangling says that int foo(int) is a different function than void foo(int).
    I suppose that's one way of looking at it. It is more normally stated as "C++ does not support overloaded functions that differ only in return type". Name mangling doesn't really have anything to do with it, except in code samples like yours where one source file declares the function returns int and another source file does not have a matching declaration, but provides a different definition - in such cases, in practice, the problem becomes evident when linking (the linker will normally complain about undefined symbols or something similar). If, within some translation unit, the compiler (whether C or C++) sees both a declaration and a definition that are different it will complain about a mismatch; that has nothing to do with name mangling.
    Quote Originally Posted by matsp View Post
    But I beleive that even in a C++ compiler, "main" is implicitly made
    extern "C" int main(/*whatever */);
    . So the return type is still not involved in the name of the function.
    The C++ standard requires no such thing: the linkage of main() is implementation defined.

    Strictly speaking, a C++ compiler is not even required to implement main() as a function.
    Quote Originally Posted by matsp View Post
    Of course, there are further complications: The call to main may be different for a void main than it is for a int main in some architectures [e.g. one where the return value from a function is passed on the stack], and this could potentially cause problems in those architectures. Just like EBCDIC, I have not experienced an architectuer yet that has this behaviour, but it's entirely concievable to have such an architecture.
    These are only complications for the compiler writer who chooses to support non-standard forms of main(). Such concerns would normally not be visible to the programmer (aka user of the compiler) unless said programmer writes code exhibiting undefined behaviour or uses tools (debuggers, etc) that allow visibility of machine-specific implementation details.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Stupid Newbie question
    By TimL in forum C++ Programming
    Replies: 4
    Last Post: 07-22-2008, 04:43 AM
  2. C prog newbie question
    By Draginzuzu in forum C Programming
    Replies: 1
    Last Post: 02-03-2003, 06:45 PM
  3. a stupid question from a newbie
    By newcomer in forum C++ Programming
    Replies: 4
    Last Post: 01-11-2003, 04:38 PM
  4. confusion with integers (newbie question)
    By imortal in forum C Programming
    Replies: 7
    Last Post: 12-06-2002, 04:09 PM
  5. newbie class templates question
    By daysleeper in forum C++ Programming
    Replies: 2
    Last Post: 09-18-2001, 09:50 AM