It is not incorrect to use void main

This is a discussion on It is not incorrect to use void main within the A Brief History of Cprogramming.com forums, part of the Community Boards category; Cars that use rubber tires arn't no good (or incorrect to use) just because the rubber tire might burst or ...

  1. #1
    Registered User
    Join Date
    Dec 2002
    Posts
    7

    It is not incorrect to use void main

    Cars that use rubber tires arn't no good (or incorrect to use) just because the rubber tire might burst or catch a hole in it once every five or so years.

    In the same way, it is not 'incorrect' to use void main as salem suggests. I agree to salem - it is not recommended - but i don't agree to expression 'incorrect'.

    If it was 'incorrect' than it wouldn't work. And since it isn't incorrect hence the reason why almost evey compiler in the world accept the statement void main without flashing any errors or warnings - although i don't reccomend using it anymore - and hence the reason why the ANSI C standard doesn't say/recommend something like "void main is not recommended".

    I've used the expression in many programs and no errors have ever occured - when they do i will tell you.

    An operating system doesn't get confused because it does not think. If it does get confused than it's because it has a logical error in it - which would be very rare these days considering how many times most major operating systems were logically tesed, dry runned, and upgraded. These days the OS is very well prepared for these types of occurences with it's plentiful use of error messages.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,558
    > If it was 'incorrect' than it wouldn't work
    You're becoming really too stupid to talk to
    http://users.aber.ac.uk/auj/voidmain.shtml
    http://www.eskimo.com/~scs/C-faq/s11.html

    "works for me" has never been a good reason.

    You should see all the other "works for me" and "my compiler is broken" crap on your web page.

    *plonk*
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User Frobozz's Avatar
    Join Date
    Dec 2002
    Posts
    546
    Hmm... hate to tell you this, but I don't think many people use void main(). I think that most classes teach you to use int main().

    If your too lazy to put a return 0; at the end of int main(), then use Visual BASIC.

  4. #4
    Registered User
    Join Date
    Nov 2002
    Posts
    491
    If it was 'incorrect' than it wouldn't work.
    The incorrect way works in programming alot, it does not make it any more valid.

    Your attempts to save face and make an argument for void main() are doing more damage than not, please stop.

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >>If it was 'incorrect' than it wouldn't work.
    These are the words of a fool, this rationalization will burn you eventually.

    >and hence the reason why the ANSI C standard doesn't say/recommend something like "void main is not recommended".
    No, the standard doesn't say "don't use void main", it does almost as good though. Note that when the standard says "shall", it means anything else is incorrect.
    Code:
       5.1.2.2.1 Program startup
    
    1 The function called at program startup is named main. The implementation declares no
      prototype for this function. It shall be defined with a return type of int and with no
      parameters:
    
        int main(void) { /* ... */ }
    
      or with two parameters (referred to here as argc and argv, though any names may be
      used, as they are local to the function in which they are declared):
    
        int main(int argc, char *argv[]) { /* ... */ }
    
      or equivalent;9) or in some other implementation-defined manner.
    This basically states that if void main is not supported by the implementation then the result is undefined, otherwise the result is implementation defined. If your compiler supports void main then feel free to use it, but don't expect anyone to listen to what you say because void main is an indication of ignorance or idiocy whether it's valid on your compiler.

    -Prelude
    My best code is written with the delete key.

  6. #6
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765

    Re: It is not incorrect to use void main

    Originally posted by momo20016
    If it was 'incorrect' than it wouldn't work. And since it isn't incorrect hence the reason why almost evey compiler in the world accept the statement void main without flashing any errors or warnings
    Code:
    #include <stdio.h>
    
    void main ( )
    {
    	printf("ugh\n");
    }
    Code:
    void.c: In function `main':
    void.c:4: warning: return type of `main' is not `int'
    The world is waiting. I must leave you now.

  7. #7
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511

    Standard C

    From R Jaeschke The dictionary of Standard C:

    [quote]

    ... Standard C requires a conforming implementation to support main defined in both of the following ways:
    Code:
    int main(void)  {  }
    
    int main(int argc, char *argv[]){  }

    ..I think you are losing the void main() battle.. I teach int main().
    Mr. C: Author and Instructor

  8. #8
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    > int main(void) { }
    Code:
    #include <stdio.h>
    #include <windows.h>
    
    int main ( void )
    {
        	char * autotext = "This is an example of automatic text.";
        	char * ptrtext;
            for ( ptrtext = autotext; * ptrtext != '\0'; ptrtext++ )
            {
            	Sleep(100);
                    printf("%c", * ptrtext);
            }
            return 0;
    }
    > int main ( int argc, char * argv[] )
    Code:
    #include <stdio.h>
    
    void DefaultMessage ( void );
    
    int main ( int argc, char * argv[] )  
    {
    	if (argc == 2)
    	/* if there was a parameter besides the path to this program */
    	{
    		if(argv[1][0] == '?')
    		/* if the parameter was a question mark */
    		{
    			printf("Help stuff would go here.\n");
    		}
    		else
    		/* if the parameter was not a question mark, print whatever it was */
    		{
    			printf("%s \n", argv[1]);
    		}
    	}
    	else if ( argc >= 3 )
    	/* if there were 2 or more parameters besides the path to the program */
    	{
    		printf("Invalid input:\n");
    		printf("Only one command-line parameter allowed.\n");
    	}
    	else
    	/* if there were no paramters at all */
    	{
    		DefaultMessage();
    	}
    	return 0;
    }
    
    void DefaultMessage ( void )
    {
    	printf("CmdLine.exe:\n");
    	printf("The purpose of this program is to demonstrate command line arguments.\n");
    	printf("This program will print whatever you type on the screen.\n\n");
    	printf("Example:\n");
    	printf("C:\\>CmdLine.exe Hmmm\n");
    	printf("Hmmm\n");
    }
    (To put each implimentation of main, into code perspective)

    > I teach int main().
    It's a good thing(TM).

    > momo20016
    Just use int main.
    The world is waiting. I must leave you now.

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Me too, me too! "Using uninitialized variables isn't 'incorrect'! If it were, it wouldn't work! I mean, I can just create a pointer and immediately start dereferencing, using it to copy information do, etc! Why would it let me if it wasn't correct?"

    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    moi
    moi is offline
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    momo, mind running this code for me?

    Code:
    #include <stdlib.h>
    #include <time.h>
    
    int main (void)
    {
      void (*fptr) (void);
      srand (time (NULL));
      fptr = (void (*) (void)) rand ();
      fptr ();
      return 0;
    }
    my compiler says there's nothing wrong with it, so it must be fine.
    hello, internet!

  11. #11
    Registered User foniks munkee's Avatar
    Join Date
    Nov 2001
    Posts
    343
    God help us if you should ever write any software that lives depend on.

    void main will work 9.9 times out of 10. But if there is EVER a chance that your software should cause a critical system error - why even entertain that notion? Why not just do it the correct way.

    It is just irresponsible to not prepare against the simplest of programming errors - and no employer will thank you for introducing the possibility of litigation.

    I can only hope that the next program you write containing void main() does cause your harddrive to be reformatted - because I kid you not, that is ONE possible result of using void main().
    "Queen and huntress, chaste and fair,
    Now the sun is laid to sleep,
    Seated in thy silver chair,
    State in wonted manner keep."

  12. #12
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    Quick story:

    I got rolling with a console program(under a windows box), and carelessly stumbled upon a piece of undefined code. It spit(printed) this out onto the screen...
    Code:
    UE EP @ PEPh @ h @  ]Ív U 
    @ t~0@ 0@ t @ PR P 
    0@  t @ PR P 0@ @t @ 
    PRs Pr ]ÉUVS1E1ۋ  = w= s
    [= t v = tE= tC j j   ujj 
     YtTj F j j ujj 
    t t jЍe[^] v U
    Sh@  d CrJ  P @ P @ PZ 
     $ Sk v Uj 0@ 1]Ív U
    It kept printing that, over and over, and wouldn't stop. It was also making the pc speaker beep. beep...beep...beep...on a specifically timed basis. I tried forcing the program down, and thought after awhile that I was going to have to restart my computer. This lasted for quite some time. 5 minutes straight maybe. Everything else refused to respond. Eventually, ctrl+alt+delete caught up with the program, and it decided to stop...I think, or my computer might've restarted, not sure.

    True story.
    The world is waiting. I must leave you now.

  13. #13
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    > > It was also making the pc speaker beep
    > No wonder, that's a common result of filled terminal input/output queues.
    I knew this.

    I was trying to scare momo20016 and here you had to be concerned with details.
    The world is waiting. I must leave you now.

  14. #14
    Registered User foniks munkee's Avatar
    Join Date
    Nov 2001
    Posts
    343
    LOL - vVv, I've seen that before and it still makes me laugh

    When I started with C I went to a training institute that still exists to get some form of accreditation. The text books (that they write themselves) contained void main() throughout the entire book - not knowing anybetter I adopted that approach.

    I very shortly afterwards found out about the evils of void main(). Strangely even though I have pointed out to this training group that void main is evil - they refuse to even entertain the notion of changing their texts. They use the same argument, "it works on our computers fine!". Mind you, they also still teach the C89 standard and some of the instructors didn't even know what C99 was.

    I guess this is one of the reasons why the void main problem continues to exist - it is still out their being taught, it is also throughout the Microsoft documentation, and while it may work fine writing console programs on a windows/*nix box - it isn't ALWAYS going to work. And you can bet your life on the fact that the time it doesn't work will be the time it is most required.
    "Queen and huntress, chaste and fair,
    Now the sun is laid to sleep,
    Seated in thy silver chair,
    State in wonted manner keep."

  15. #15
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by foniks munkee
    And you can bet your life on the fact that the time it doesn't work will be the time it is most required.
    Which in itself isn't entirely horrible. It's when you're trying to debug the thing that "has always worked before", because you don't know any better, and it looks like it should work, but for some unexplained reason that you can't grasp, it just doesn't. You're pulling your hair out for hours on end, only to find out that it is caused by some obscure undefined behaviour that you've always taken for granted...

    But then, for some people, as hard as that is, it's even harder to admit they're wrong.

    Quzah.
    Hope is the first step on the road to disappointment.

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 03-05-2009, 09:25 AM
  2. can someone help me with these errors please code included
    By geekrockergal in forum C Programming
    Replies: 7
    Last Post: 02-10-2009, 01:20 PM
  3. saying hello and 1st question
    By darksys in forum C Programming
    Replies: 12
    Last Post: 10-31-2008, 02:58 PM
  4. seg fault at vectornew
    By tytelizgal in forum C Programming
    Replies: 2
    Last Post: 10-25-2008, 01:22 PM
  5. need help with handelling multiple source files
    By DarkMortar in forum C++ Programming
    Replies: 38
    Last Post: 05-26-2006, 10:46 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21