Thread: void main question..

  1. #16
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by cpjust
    But on platforms where the return value of main() isn't used, why doesn't the compiler just allow you to use int main() but throw away the return value?
    What do you mean?
    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

  2. #17
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I think what cpjust means is this:
    On some embedded systems, main typically returns void because nothing is returned to the operating system. So why could the compiler not just accept int main and ignore whatever main returns?

    A cookie to anyone who solves the problem!
    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.

  3. #18
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    In many embedded platforms, where the code starts to execute your code isn't necessarily called main() anyways...

    And of course, there's nothing stopping some startup code that calls main from ignoring the return value. We do that all the time with functions like fread, fwrite, printf, sprintf, scanf, etc that are all returning an integer, but most applications do not use that return value.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #19
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by matsp
    In many embedded platforms, where the code starts to execute your code isn't necessarily called main() anyways...
    Precisely, on freestanding implementations the rules we often quote concerning main do not apply at all as it is all implementation defined.
    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

  5. #20
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Elysia View Post
    I think what cpjust means is this:
    On some embedded systems, main typically returns void because nothing is returned to the operating system. So why could the compiler not just accept int main and ignore whatever main returns?

    A cookie to anyone who solves the problem!
    If there is no name mangling, the support library that calls main won't know whether main returns a value or not.

    Of course, on really tiny architectures, returning a value may not be done in a register. Say the system only really has ONE 8 bit general purpose register, so if you return a 16-bit integer value, it has to be passed like a struct is passed back on 32-bit compilers when you return a struct: The compiler provides space for the return value on the stack, and passes the address to that space to the as a hidden parameter to the function - in that case, it would take up extra code in both the startup code and the main function to provide the return value, and if it's completely ignored, the embedded user may not want to "spend" memory on returning a meaningless value.

    But void main() is not exactly common.

    In our embedded OS, "main" isn't called "main", but it still returns a value.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #21
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I think what cpjust means is this:
    On some embedded systems, main typically returns void because nothing is returned to the operating system. So why could the compiler not just accept int main and ignore whatever main returns?

    A cookie to anyone who solves the problem!
    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.

  7. #22
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by Elysia View Post
    I think what cpjust means is this:
    On some embedded systems, main typically returns void because nothing is returned to the operating system. So why could the compiler not just accept int main and ignore whatever main returns?

    A cookie to anyone who solves the problem!
    Deja vu?

    Basically, on systems that only support void main(), they could see int main() and change it to void main() when you compile it. I don't deal with embedded devices, so I don't really care one way or another, but that would seem one way to make main() consistent across multiple platforms.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  8. #23
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Elysia
    On some embedded systems, main typically returns void because nothing is returned to the operating system. So why could the compiler not just accept int main and ignore whatever main returns?
    The answer then, is that it could.
    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. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 03:25 PM
  2. Replies: 3
    Last Post: 05-13-2007, 08:55 AM
  3. Error, not sure what it means.
    By RealityFusion in forum C++ Programming
    Replies: 1
    Last Post: 09-25-2005, 01:17 PM
  4. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM
  5. what does this warningmean???
    By kreyes in forum C Programming
    Replies: 5
    Last Post: 03-04-2002, 07:53 AM