Thread: Void main() issue

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    151

    Void main() issue

    I have been reading why void main() is bad , and I have seen a line ;

    >(void)fgets(buf, 1024, stdin);

    What does that void at the beginning mean?

    ----------------------

    Also , The wiriting was mentioning that library startup code always need a return value. If the main is void , it will take the return value from another component. Depending on what the compiler chooses that value? Or is there really a value or am I wrong?

    Thank you.

  2. #2
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by ozumsafa View Post
    >(void)fgets(buf, 1024, stdin);
    This will cast away the value returned by fgets. In essence, you're telling the compiler that you know better, that fgets doesn't return anything you're interested in and that it should shut up about it. No idea whay you'd want to do that.

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >What does that void at the beginning mean?
    It says that you really aren't using the return value and you don't want to see any warnings. I think it's clutter, but some lint programs will warn you about unused return values on their most persnickety setting.

    >The wiriting was mentioning that library startup code always need a return value. If the main
    >is void , it will take the return value from another component. Depending on what the
    >compiler chooses that value? Or is there really a value or am I wrong?
    That sounds like a weak argument, but I don't think you described it well enough for me to be sure.
    My best code is written with the delete key.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    > some lint programs will warn you about unused return values on their most persnickety setting.

    Yes, I remember trying lint a long time ago and it would throw off warnings whenever you used printf() without casting it to void (which was needed about 99.99% of the time).

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Prelude View Post
    >The wiriting was mentioning that library startup code always need a return value. If the main
    >is void , it will take the return value from another component. Depending on what the
    >compiler chooses that value? Or is there really a value or am I wrong?
    That sounds like a weak argument, but I don't think you described it well enough for me to be sure.
    Casting the return value from another function will not change what the called function does, and it's the called function that "issues" the return value (e.g. it puts some value in EAX in common x86 code). If you omit the return from main (or make it void), the "return value" will be whatever is in EAX (or the relevant return register in other architectures). If your code ends with a getchar() and someone presses 'A', the return value will be 65 (most likely - it is possible that it gets "lost" somehwere - try it if you like...).

    Note that if you have no return statement in main, and the last bit of main is some function call that is has a void return or some calculations, the return value will be some unpredictabel value [as in unpredictable without examining the generated assembler code and knowing what happens in that code - and this will change if you modify the source, change the compiler settings or use a different compiler for example].

    --
    Mats
    Last edited by matsp; 09-24-2007 at 01:12 PM.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. saying hello and 1st question
    By darksys in forum C Programming
    Replies: 12
    Last Post: 10-31-2008, 02:58 PM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. i cannot see the mouse arrow
    By peachchentao in forum C Programming
    Replies: 6
    Last Post: 12-10-2006, 04:14 AM
  5. need help with handelling multiple source files
    By DarkMortar in forum C++ Programming
    Replies: 38
    Last Post: 05-26-2006, 10:46 PM