Thread: Parameter and return value specifics

  1. #1
    Registered User
    Join Date
    Jul 2006
    Location
    Detroit
    Posts
    3

    Parameter and return value specifics

    Hello everyone,
    Studying C in school and have just a couple questions that I have seemingly answered with the compiler but have not been able to find in any books..

    1.) Is there any difference between a funtion with no parameters that does not include anything between the parenthesis and one that only includes the data type void? In other words, any difference between "int main ()" and "int main (void)"? I've experimented with both and observed no difference.

    2.) Is there any difference between an integer main function that returns 0 and a void main function? I noticed that most reputable C books teach the reader to declare main as an integer returning function but many tutorials also just use void ("void main (void) and void main()"). Likewise, I've experimented with this in the compiler as well and observed no difference.

  2. #2
    Just kidding.... fnoyan's Avatar
    Join Date
    Jun 2003
    Location
    Still in the egg
    Posts
    275
    A links from C-FAQ

    http://c-faq.com/decl/main.html

    Also, I recommend you to have a look at http://c-faq.com/

    foo(void) means this function accepts no parameters. void is used to declare that the function has no parameters.

  3. #3
    1 == 1 tzpb8's Avatar
    Join Date
    Jul 2006
    Posts
    36
    I know one.. but dont know if it is relevant though..
    The "Dev-C++ (v4.9.9.2) " compiler (dont know about earlier versions), gives an error if you try to define main() as a void function though in memory i think i remeber defining main() as a void function some time ago..

  4. #4
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    Quote Originally Posted by Zaff
    1.) Is there any difference between a funtion with no parameters that does not include anything between the parenthesis and one that only includes the data type void? In other words, any difference between "int main ()" and "int main (void)"? I've experimented with both and observed no difference.
    not in today's versions... but I think... now don't quote me on this cause I wasn't around for this, but I think some of the original versions had a different meaning for the empty parameter list.
    Quote Originally Posted by Zaff
    2.) Is there any difference between an integer main function that returns 0 and a void main function? I noticed that most reputable C books teach the reader to declare main as an integer returning function but many tutorials also just use void ("void main (void) and void main()"). Likewise, I've experimented with this in the compiler as well and observed no difference.
    the difference is that void main is non-standard.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >In other words, any difference between "int main ()" and "int main (void)"?
    It depends on where you use it. For a function declaration, there's a subtle and disturbing difference:
    Code:
    void foo(); // Takes an unknown number and type of parameters
    void foo ( void ); // Takes no parameters
    For a function definition, there's still a minor difference:
    Code:
    // Does not ensure a complete prototype
    void foo()
    {
    }
    
    // Ensures a complete prototype
    void foo ( void )
    {
    }
    A full prototype, of course, guarantees that you'll be informed of improper use of the function. You should always use void when you intend for a function to have no arguments, and you should refrain from using empty parameter lists because it's a feature of the language that's being phased out with new revisions of the standard. It's also dangerous.

    >Is there any difference between an integer main function that returns 0 and a void main function?
    Yes, main returning an integer is correct and main returning anything else is not. The C standard makes it very clear that the following declarations of main (and anything directly equivalent) are allowed:
    Code:
    int main ( void );
    int main ( int argc, char *argv[] );
    Anything else is implementation-specific. It could either work if your implementation allows it, or invoke undefined behavior. That kind of uncertainty makes using void main exceptionally stupid.
    My best code is written with the delete key.

  6. #6
    Registered User
    Join Date
    Jul 2006
    Location
    Detroit
    Posts
    3

    Thumbs up

    Thanks alot guys, great answers in no time at all. Definetly one of the better C forums online.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  4. Simple thread object model (my first post)
    By Codeplug in forum Windows Programming
    Replies: 4
    Last Post: 12-12-2004, 11:34 PM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM