Genesi Posted August 5, 2004 Share Posted August 5, 2004 #include <iostream> #include <string> using namespace std; void main() { int c, nb, nt, nl; nb = 0; nt = 0; nl = 0; while (c = getchar() != EOF) { if (c == ' ') ++nb; if (c == '\t') ++nt; if (c == '\n') ++nl; } } If I hit enter then it keeps going, never terminates. Link to comment Share on other sites More sharing options...
0 Kestrel Posted August 7, 2004 Share Posted August 7, 2004 If someone knows what they are doing, what is the big deal? You do realize that all branch structures in all compiled languages end up as "goto"'s anyways, I would hope. Aside from "style", what purpose does including the return type and arguments if they are not used?? Please, share this with me. I've spent a good few years coding in various assembly languages, so yes, I do realise that branches end up as gotos. The problem with gotos in higher-level languages is that they tend to disrupt programme flow and can very easily end up creating spaghetti code that jumps around all over the place. The whole point of higher-level languages is to simplify programming by abstracting low-level constructs through the use of higher-level constructs and all higher-level languages offer alternatives that provide the same functionality in a cleaner and more structured way. "if someone knows what they are doing, what's the big deal?" - you've missed the point... someone who takes the sloppy way out clearly does NOT know what they are doing. As for the arguments and return value thing. A compliant compiler is NOT required to recognise "void main()" - the fact that YOUR compiler does is simply the result of a friendly compiler designer. Read the posted links (and equivalent comments by Brian Kernighan) and you'll see quite clearly that main (as required by the language spec itself) is *REQUIRED* to return an integer. Failing to do so is not only bad style but INCORRECT CODE. Using "void main()" is sloppy coding and I wouldn't hire someone who used it because it shows a lack of discipline, style and understanding of the language that surely will manifest itself in other areas of their programming. At any rate, this has been discussed, the appropriate links have been posted and I'm not going to sit here and debate something that has been well established in the literature. Do some reading, get some experience. Link to comment Share on other sites More sharing options...
0 kjordan2001 Posted August 7, 2004 Share Posted August 7, 2004 Aside from "style", what purpose does including the return type and arguments if they are not used?? Please, share this with me. The point is they may be used, and like Kestrel said, a lot of compilers don't recognize it. You need to return something just in case someone actually uses your program and they need a return code. Return codes let you know when something went wrong and something went right. Link to comment Share on other sites More sharing options...
0 Genesi Posted August 7, 2004 Author Share Posted August 7, 2004 kumbia my lord kumbia Link to comment Share on other sites More sharing options...
0 vcv Posted August 8, 2004 Share Posted August 8, 2004 If you're going to insist that those 2 arguments for main should always be in place, why not also use the third argument, which contains the environment variables? Granted, it's only used on some unix systems and VC++ usually. If you don't need the arguments, there is no reason to define main as such. In fact, I think it would be in better interest to leave them out when unused to signify to any outside source that they are not used. Sloppy way out.. that's cute. I said someone who DOES know what they are doing. Meaning they understand the standards.. they understand the underlying code that will be produce. They understand how it works at a machine level. The fact that you wouldn't hire someone who may be a very very good, experienced programmer based on them not using "int" instead of "void" when there is no need for it, makes me sick. It is this same mindset that teachers have that make them take off points from a students code because they didn't do things THEIR [the teachers] way, and THEIR style. Granted, sometimes there is perfectly valid reasons for this, and other times it is simply retarded. I've seen teachers who know 10x less than the student they were grading take off points for a differing style. That makes me sick. If it compiles, is understandable to anyone reading the code, and works.. what is the problem? Link to comment Share on other sites More sharing options...
0 azcodemonkey Posted August 8, 2004 Share Posted August 8, 2004 Ya know, it's pretty simple. Those that don't use int main aren't following the standard. Do you really think that a professional programming job won't involve coding standards? You'll be writing code someone else's way for the rest of your life unless you're the one making the standards. Link to comment Share on other sites More sharing options...
0 vcv Posted August 8, 2004 Share Posted August 8, 2004 I'm not talking about all standards.. I'm talking about one little one here that is, IMHO, pointless. Don't get me wrong.. if I'm ever helping a newbie with C++, I always use int main().. but I know what I'm doing. I know the difference, and I know the reasoning behind it. As far as professional programming job.. I have one and I write code MY way. It's working out just fine so far. Link to comment Share on other sites More sharing options...
0 Kestrel Posted August 9, 2004 Share Posted August 9, 2004 I'm not talking about all standards.. I'm talking about one little one here that is, IMHO, pointless.Don't get me wrong.. if I'm ever helping a newbie with C++, I always use int main().. but I know what I'm doing. I know the difference, and I know the reasoning behind it. As far as professional programming job.. I have one and I write code MY way. It's working out just fine so far. So if you're helping a newbie with a problem you always use int main() ? How is that any different from my first post on the issue? WHY do you always teach a newbie int main()? Because the spec calls for int main() ergo that is the correct and standard way to define main. That is all my post said in the first place. Link to comment Share on other sites More sharing options...
0 vcv Posted August 9, 2004 Share Posted August 9, 2004 Because a newbie doesn't understand how it works beneath the surface. Anyways.. I was just playing devils advocate :) Though I DO use void main() sometimes.. but only when making quick disposable test applications. Link to comment Share on other sites More sharing options...
0 IDGAF Posted August 9, 2004 Share Posted August 9, 2004 Indifferent to the standard, does it make sense in any other function to return a value when none is needed? I can't say I add a return type to a function that doesn't return anything, so why should main be any different? I'm not questioning what the current standard (thanks weenur for the direct link) covers, but rather if that standard is logical. Link to comment Share on other sites More sharing options...
0 azcodemonkey Posted August 9, 2004 Share Posted August 9, 2004 Indifferent to the standard, does it make sense in any other function to return a value when none is needed? I can't say I add a return type to a function that doesn't return anything, so why should main be any different? I'm not questioning what the current standard (thanks weenur for the direct link) covers, but rather if that standard is logical. Well, if I'm calling a function/method and I don't need a return value, then obviously it'll be a void. However, I'm not calling main, the OS is, and possibly another program via IPC. Link to comment Share on other sites More sharing options...
0 IDGAF Posted August 9, 2004 Share Posted August 9, 2004 The biggest argument I can think of for always using int main(/**/) is ANSI compatibility; this is a huge reason. But, I'm just pondering whether it should be part of the standard to allow it; I think it should. I read about it being bad coding style in this thread, but is it only bad because it is not ANSI compatible? It just seems so right to use void when you have no intent to return meaningful values. Just something to think about. Link to comment Share on other sites More sharing options...
0 azcodemonkey Posted August 10, 2004 Share Posted August 10, 2004 I think the point of the standard is even if you don't return meaningful values, you can't always account for how your code is used. What if the OS expects a return? I know it's anal, but(no pun intended) how hard is it to not use void? You're never calling it, and techincally, you don't even have to specify your return value. // this is legit int main() { } Link to comment Share on other sites More sharing options...
0 clonek Posted August 13, 2004 Share Posted August 13, 2004 FYI, using void main{} is not portable. Some compilers such as GCC/G++ will give a compile error when void main{} is used. Also, the reason for returning a value (or using exit() ) is because the operating system expects to see some kind of exit status when the program has finished executing. Link to comment Share on other sites More sharing options...
0 Hosiah Posted August 16, 2004 Share Posted August 16, 2004 wow! quite a bruhaha! OK, I'm still just a noob at C++, and may be yet for years. But, I think, part of the confusion might stem from some books on the language, which use the void main() convention. And many of us have been programming for years using void main() without problems, and compilers allow void main(). It strikes me as odd that if this is a crucial issue, then whole books have been written the wrong way, and whole compilers which have been used for years have been designed badly. I confess that I've been using the void convention up until now, and this is the first time I've ever heard that that's wrong. I've always gone on the assumption that if you don't need it, don't use it! However, I'm hearing here, that if my program, compiled and released as an executable, does not return a value (even zero!) then certain operating systems will have an error because of that? If so, then which operating systems/platforms have that requirement? And let's keep the peace, pretty please? I'm not trying to stir up trouble, fan a flame: I'm genuinely interrested in hearing the pros and cons with an open mind. And if there were some kind of harm done to some systems by using void main, I can certainly see where it makes sense to use int instead, since it's no trouble at all to type one little extra "return 0;", and I might as well make my programs as robust as possible. Peace and Love, Hippy Hosiah PS edit: I have tried "int main()" with no return value, my MVC++ coughs at that - says a return value is expected. Link to comment Share on other sites More sharing options...
Question
Genesi
#include <iostream> #include <string> using namespace std; void main() { int c, nb, nt, nl; nb = 0; nt = 0; nl = 0; while (c = getchar() != EOF) { if (c == ' ') ++nb; if (c == '\t') ++nt; if (c == '\n') ++nl; } }If I hit enter then it keeps going, never terminates.
Link to comment
Share on other sites
39 answers to this question
Recommended Posts