InterviewSolution
Saved Bookmarks
| 1. |
Solve : int main() vs int main(void)? |
|
Answer» Got into a discussion at work with another C++ hobbyist and they looked at a program I wrote and asked why not use Standard C++ And the int main(void) seems to be dated back to earlier C++ standards Quote The accepted answer appears to be targetted for C++, so I thought I'd add an answer that pertains to C, and this differs in a few ways. So does it really not matter int main() or int main(void), its just 2 ways of accomplishing the same thing, since after all the programs both will run fine with whatever style is used? I was taught this way without the use of int main(void) between 2000-2004 back in college. Quite a big discussion here http://stackoverflow.com/questions/12225171/difference-between-int-main-and-int-mainvoid As the linked StackOverflow question/answers indicate, the difference would only really affect C. As noted therein in one of the answers, In C, when declaring a function prototype, an empty argument list basically meant that you could pass any parameters to that function. In order to make a function and have it take no arguments at all, you would have to specify "void". C++ of course being a superset of C allows you to do this but an empty parameter list means the same thing. Personally, any C++ main() routine I write (I don't write C/C++ very often, though) get's the full int main(int argc, char** argv) definition. (Should be noted that char** argv is identical to char* argv[] as a parameter type, though I'm not sure how I got into that habit)Quote Personally, any C++ main() routine I write (I don't write C/C++ very often, though) get's the full int main(int argc, char** argv) definition. (Should be noted that char** argv is identical to char* argv[] as a parameter type, though I'm not sure how I got into that habit) Being explicit with the parameter types I guess might be better practice vs leaving it undefined (). There are times when you have to be explicit or else you end up with strange problems or compiler warnings or errors. When compiling it was my understanding that the compiler whether int main() or int main(void) basicially interpreted it the same such as when using cout and printf(). However I have read that printf() seems to run faster than cout calls so if the compiler accepted both as equal when compiling there wouldnt be a speed advantage between one and the other when they are executed, so the statement that a software engineer told me years back may be completely false with how the compiler compiles the source from what we can read as source code into machine language. Thanks for the inputs on this subject |
|