Menu

void main?

2005-07-21
2012-09-26
1 2 > >> (Page 1 of 2)
  • Nobody/Anonymous

    Why wont this let me use void main()? In my c++ book that is perfectly acceptable. This is forcing me to use int main() and have a return 0; at the end. Is there something I don't know about?

     
    • Nobody/Anonymous

      This is great.
      Kindly note that in returning a value to batch file a 1,2 or 3 ... does not have to mean an error.
      Batch files are clever things, they can test the final return of the program and do something according to what the number given was.
      The same appies to called routines, the number returned is there for the use of the programmer not to maintain a standard.
      Programmers are or should be resourceful people.
      Any programming rule or compiler standard is only a means to doing the job better.
      I cannot imagine the gymnastics I would have had to go through if I insisted on void main() all my life.
      In the days of King Arthur the OS produced by Burroughs had a number of bugs in it. These were classed as 'facilities' for smart people to get the job done more easily and better.

       
    • Nobody/Anonymous

      Yes, the older code from the book is no longer in compliance with current standards. int main( ) and a return are currently accepted practice and some compilers enforce that you do so.

       
    • Kip

      Kip - 2005-07-21

      And let that comment be the end of it ;)

      Kip

       
    • Nobody/Anonymous

      oh...haha....well I was not aware of that

       
    • Nobody/Anonymous

      Hi Everyone:

      Cause your book is wrong/out of date. You have to always used int main(), void main() is incorrect.

      You have to return 0 cause 0 is an integer and you said you were going to return an integer when you used int main().

      See Ya
      Butch

       
      • Nobody/Anonymous

        Hiya Butch,

        I like return(25); because it's like my favorite number and stuff. Is there any problem in doin that or go against some standard? Thanks ahead of time dood.

         
    • Nobody/Anonymous

      your c++ book seems to be depreciated as void main() also is.
      Dev-Cpp is fully standard compliant to ANSI-C++ where main() has to return an int.

      please do a forum search, this topic has come up very often and was already explained very well (+ excessive) there.

       
    • Nobody/Anonymous

      The standard only mandates two forms of main

      int main(void);
      int main( int argc, char** argv);

      Now, compiler vendors are perfectly free to implement other forms of main, although the standard prefers that they be backwards compatible with one of the two forms above. Some implementations provide additional arguments to main.

      Certainly, for an embedded application (which is not running under any OS) void main(void) would be perfectly acceptable, but otherwise it should return a value which can then be used by the OS.

      rr

       
    • qWake

      qWake - 2005-07-21

      Let's not forget that C++ doesn't "force" you to have a "return 0;" statement at the end of main. A compliant compiler will automatically add one to main (and main only) if it is omitted. No need to expend the energy required to type that final statement! You even save an additional keystroke by typing "int" instead of "void" as function type. How is that for economy? ...not like all the previous keystrokes I've just typed. Man, I'm beat.

      qWake

       
      • Nobody/Anonymous

        If I remember correctly, not only can you save keystrokes by not specifying a return and using int instead of void for main, but you can save even more by just using main() (no void or int) and it will be "assumed" that main returns an int.

         
    • Kip

      Kip - 2005-07-22

      "And the Lord said, let their be go-no-where dialog. And there was void main." - Algorithmus 10:12

      Kip

       
    • Nobody/Anonymous

      • Sorry, it was Petius 10:12

      Kip

       
    • Nobody/Anonymous

      I believe some OSs use the return value from a program for various tasks. 25 might confuse them and cause unexpected behaviour. The best way to guarentee working properly is return EXIT_SUCCESS;

       
    • Ian Walker

      Ian Walker - 2005-07-22

      In Windoze, the return value can be used by a calling program (ie a batch file, or an MSI installer) but it is ignored by the OS.

       
    • S. Thomas Bradley

      Hi Everyone:

      As I recall, which, as always could be wrong, in some environments the value you return can be used by an error handling function to denote a specific error. The Standard Library has a library called <error.h> or cerror, I think, it's been a while since I used it. I believe the value returned can be passed along to that, but like I said, it's been years since I've used it so I'm a little hazy on it exactly. Maybe someone else has used it more recently.

      For functions in your own programs, you probably already are aware that you can use return 0; or return 1; to denote success or failure in calling your function. You might write something like this:

      if(!myFunc(var))
      HandleError();

      Now if some error occurred when you called your function myFunc() you would have your function return 0 (false), instead of 1(true) then the '!' in !myFunc will switch the returned 0 (false) to a 1(true). The if statement receives a true so your error handling routine (HandleError();) is called.

      You can even become fancier, and assign specific errors an error number. Then when your function returns you would check for 0. If a number other then zero is returned (most references say "if non-zero is returned") you could pass it long to your error handler as a parameter; like (HandleError(err);). You error handler could then use something like switch/case statements to select the appropiate error message and print it out to tell you exactly what that error was.

      Often in a Windows program the value return 0; and return 1; are used to denote returning false or true. In particular, when you create, say, a dialog box, you have to return 1; (true) in order for Windows to display it properly. If you return 0; (false) Windows will will not display it, or not display it properly.

      I've had this engrained into me only because I've forgotten this more then once and had to beat my head on the wall for a couple of days until I released that I had used return 0; instead of return 1;.

      This is probably more info then what you wanted. But using return x is a very useful thing.

      See Ya
      Butch

       
    • Kip

      Kip - 2005-07-22

      "And the Lord said, let their be go-no-where dialog. And there was void main." - Petius 10:12

      Kip

       
    • S. Thomas Bradley

      You know, I got to thinking about what I said and released that I didn't directly answer the question:

      "I like return(25); because it's like my favorite number and stuff. Is there any problem in doin that or go against some standard? "

      Reading what I said previously, you may or may not see the answer to the question. So I'm going to state it another way here.

      Return 0; actually has a meaning. When a program returns with a zero that means that no errors occurred. Returning with a non-zero means that some type of error had occurred. Therefore, if you use return 25; in your main program, which exits back to the Operating System, you would be telling the OS that an error has occurred. Now, nothing much would probably happen, at least on a Windows computer. But it is just standard procedure to tell the OS that everything is okey when you exit your program, that is why you use return 0;.

      See Ya
      Butch

       
      • Anonymous

        Anonymous - 2005-07-23

        Generally nothing much happens until your program is being called by something that cares. If for example a command issues from a makefile rule returns non-zero, make will abort the build. (Hence the [Error 1] message you often see at the end of a build when some part of the build has failed - the program invoked returned 1).

        A the risk of Kip's ridicule for getting involved in this perrenial discussion, one the void main() issue:

        The ISO C and C++ require that the compiler support two forms of main() - int main() and int main( int, char**). Both standards allow (but do not require) the compiler to support other other forms. Such forms are not therefore portable. Moreover, the C++ standard is clear that all forms of main() must return int. The C standard is more ambiguous, and due to what is probably just poor punctuation appears to allow void main() as a non-standard but allowed form. This is all academic however, since GCC does not support any non-standard forms of main().

        Clifford

         
    • Nobody/Anonymous

      Clifford: "... GCC does not support any non-standard forms of main()."

      If you use C you can very well use void main(void). GCC yields a warning but works fine anyway:

      [Warning] return type of 'main' is not `int'

      I understand you point, however, as I believe you recommend using the -Werror switch which makes it tedious to compile such code! ;-)

      RaX

       
      • Anonymous

        Anonymous - 2005-07-23

        Ah! Got me! I seldom compile code as C these days, even if the code would do so, since C++ has better error checking.

        Clifford

         
    • Kip

      Kip - 2005-07-24

      "And the Lord said, let their be go-no-where dialog. And there was void main." - Petius 10:12

      Kip

      PS ROFLMAO, Cliff!

       
    • Nobody/Anonymous

      Why have you posted that same quote 3 times?

       
      • Kip

        Kip - 2005-07-24

        It could have been four, and still been to no avail.

        Kip

         
    • Nobody/Anonymous

      // Actually, it's:

      class CMain
      {
      public:

      static int main ()
      {
        return 0;
      }
      
      static int main (const int strings, const char * const pointers[])
      {
        return 0;
      }
      

      };

      int main ()
      {
      return (CMain::main ());
      }

       
1 2 > >> (Page 1 of 2)

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.