What is the difference between "int main(void)" and simply "int main()"? Is "int main()" acceptable?

yes, it has no difference, but you should always use the int main(int nargs, args[]);, it is better then just int main()

Some compilers may produce warnings if you declare main() with arguments and never use them. Use int main() if your program does not need command-line arguments, otherwise use the version with two arguments.

you should always use the int main(int nargs, args[]);, it is better then just int main()

Please explain why it's better.

@sergent
RUN FOR THE HILLS!

int main(int argc, char** argv) is better than int main(), because with the former one, your program can analyse the command line parameters passed to it. The number of argument passed is available in the argc parameter, and the arguments themselves can be accessed by the accessing the argv parameter.

Also, see C++ FAQ: http://www.parashift.com/c++-faq-lite/newbie.html#faq-29.3

and if you don't need the command line parameters for your program, explain again how it's better and should always be used?.

I think that they compile to the same thing. I might be mistaken, but I think that there is an implicit void argument to all functions that appear to take no arguments.

You mean you don't even read your own links ???

A conforming implementation accepts

int main() { /* ... */ }

and

int main(int argc, char* argv[]) { /* ... */ }

@WaltP: It is better, because that's what is the standard:

From Bjarne's FAQ: http://www2.research.att.com/~bs/bs_faq2.html#void-main

I don't see anywhere stated that int main(int argc, char* argv[]) is better than int main() .

I wonder whether char main() is acceptable?

I don't see anywhere stated that int main(int argc, char* argv[]) is better than int main() .

I wonder whether char main() is acceptable?

Quoted from wiki-god


C and C++
In C and C++, the function prototype of the main function looks like one of the following:
int main(void)
int main(int argc, char *argv[])

The parameters argc, argument count, and argv, argument vector,[1] respectively give the number and value of the program's command-line arguments. The names of argc and argv may be any valid identifier in C, but it is common convention to use these names. In C++, the names are to be taken literally, and the "void" in the parameter list is to be omitted, if strict conformance is desired.[2] Other platform-dependent formats are also allowed by the C and C++ standards, except that in C++ the return type must stay int; for example, Unix (though not POSIX.1) and Microsoft Windows have a third argument giving the program's environment, otherwise accessible through getenv in stdlib.h:

There was no extra emphasis in the original statement, I just put emphasis on the important statement pertaining to your question.

So both version are valid, neither one is better than the other. Use int main() if you don't need the command line arguments,
or use int main(int argc, char *argv[]) if you do need the command line argument. In C++, if you do need the command line argument, then to conform to standards, you need to use exactly the version shown above with no variation at all.

@AncientDragon:

I read my own links, and I do not know why did you ask that.

I guess you're telling me that I advocated "against" the usage of the form that does not accept parameters, but now my link says it's accepted?

Read my previous post AGAIN. I only said that the version that accepts parameters is better.

Yet another otiose explanation given because of you. I remember the CreateThread topic, where you said you'll use it with MFC and it's all fine. :)

Read my previous post AGAIN. I only said that the version that accepts parameters is better.

Then you failed to answer the question. Why is the version that accepts parameters always better?

Let's look at your replies:

int main(int argc, char** argv) is better than int main(), because with the former one, your program can analyse the command line parameters passed to it.

When you want to parse command line arguments, it's obvious that main should be defined to allow that. What I want to know is why you think the two parameter version is better when the program doesn't care one whit about analyzing command line arguments. You see, both you and sergent appear to think that it's better in that case as well.

@WaltP: It is better, because that's what is the standard:

This is a worthless reason, the zero parameter version is also standard.

Then you failed to answer the question.Why is the version that accepts parameters always better?

I said that - because the version with parameters can analyse command line arguments.

when the program doesn't care one whit about analyzing command line arguments.

Well, this was not in the OP - he wanted to know the difference and what's better, so I gave a reason.

This is a worthless reason, the zero parameter version is also standard.

I was wanting to have said that (in comparison with void main()), but since you think you can be rude, I'll say that your "note" on my comment being worthless, is completely worthless as well.

I said that - because the version with parameters can analyse command line arguments.

That's not a reason for why it's ALWAYS better. Why is this question so difficult for you to understand? There are two fully standard versions of main under discussion right now:

int main()
{
    ...
}
int main(int argc, char *argv[])
{
    ...
}

You and sergent say that the latter is always better. I want to know why it's better in the very common case of not needing command line arguments. If it's your opinion, that's fine, but don't try to palm of your opinion as fact.

I like programs that support command line arguments. There, there's my opinion, now I'll just brace for the backlash.....

commented: Me too. :) Console filters aren't common anymore, but they're super useful. +17

>>@WaltP: It is better, because that's what is the standard:

>>@AncientDragon:
>>I read my own links, and I do not know why did you ask that.

I said that because the link you posted said no such thing. You are attempting to make us believe Bjarne said something that he did not, and that constitutes a flat-out lie.

>>I said that because the link you posted said no such thing. You are attempting to make us believe Bjarne said something that he did not, and that constitutes a flat-out lie.

So, I try to offer some help and you're going out of your way to prove that I'm basically telling a "flat-out lie". It would help if you expend your energies on something better.

You must be one of those "elite" members who will spend time on proving that someone relatively new is less educated. :)

My point was just that the version with parameters is better in the sense that it can parse command line arguments.

So, I try to offer some help and you're going out of your way to prove that I'm basically telling a "flat-out lie".

Please point out where AD accused you of lying. Try being e a little less defensive when people are correcting your mistakes, and you'll probably learn a lot.

My point was just that the version with parameters is better in the sense that it can parse command line arguments.

And the version without parameters is better in the sense that you don't get warnings about unused variables when you don't need to parse command line arguments.

I think that saying the two parameter version is "better" than the zero argument version when you want to parse command line arguments is like saying water is better for hydration than sand. So rather than assume you were needlessly stating the obvious, I took your meaning as the two argument version is "better" when not parsing command line arguments.

My question still stands for sergent though, who very clearly said that it's better to "always" use the two argument version.

what about void main()? Working fine for me...

what about void main()?

Not portable at best, undefined behavior at worst.

Working fine for me...

You'll find that "it works for me" is not synonymous with "it's correct" in C++. There are a lot of broken things that appear to work when you test, but can and do blow up at the worst possible time.

Haha wow I forgot how mean and critical the people can be in the C++ forum. Thank god I really only use C# now.

@Narue - I think that what Rajesh meant by 'its better to use main() with args' even if the program doesn't have command-line args is if you must choose one to always use then use the one with args. I suppose you can argue that it takes a few more bytes of memory than neccessary if you use that method without any need for the args, but realistically that's not an issue (my future-shop-value-package computer came with 8 gigs of ram and costed about 400 bucks lol)

I suppose you can argue that it takes a few more bytes of memory than neccessary

I really don't care about that part. It's the whole potential for unused variable warnings (I'm a stickler for clean compilation) and basically lying to the reader of your code ("I'm using command line arugments...NOT!") that I disapprove of.

As for me it is not important at all.

@Narue -
Haha you would hate where I work then!

My old boss (the guy I replaced) had to disable windows error reporting on the systems we sold with our software on it (to prevent that annoying 'Report to microsoft' window) because our software crashed everytime it was closed. I've since fixed that problem, but see where you are coming from with respects to clean compiling and not taking shortcuts.

You are right, I don't condone crappy programming and don't expect my employees to either; I formally revoke my last post.

And the version without parameters is better in the sense that you don't get warnings about unused variables when you don't need to parse command line arguments.

Why not use one version for everything? And I thought it was also a standard, but now I looked it up and it is not... So I guess it is a matter of opinion. I am sorry for the false info.

what about void main()? Working fine for me...

You should never use it! Most compilers do not support it and it is not standard!

Haha wow I forgot how mean and critical the people can be in the C++ forum. Thank god I really only use C# now.

void main() is just incorrect, and there is ABSOLUTELY no reason to use it! Go and program in your C# then, if you don't like C++!

Why not use one version for everything?

That's a matter of personal preference. For consistency I can see using the most feature rich version, but let's consider the following familiar program using that convention:

#include <iostream>

int main(int argc, char *argv[])
{
    std::cout<<"Hello, world!\n";
}

On Visual Studio with /W4 (highly recommended) I get this:

1>------ Build started: Project: C++ Scratch, Configuration: Debug Win32 ------
1> main.cpp
1>d:\users\admin\documents\visual studio 2010\projects\c++ scratch\c++ scratch\main.cpp(3): warning C4100: 'argv' : unreferenced formal parameter
1>d:\users\admin\documents\visual studio 2010\projects\c++ scratch\c++ scratch\main.cpp(3): warning C4100: 'argc' : unreferenced formal parameter
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========

And on g++ 4.6.0 with -Wextra (also highly recommended):

-------------- Build: Debug in C++ Scratch ---------------

Compiling: main.cpp
D:\Users\Admin\Documents\CodeBlocks Projects\C++ Scratch\main.cpp:3:5: warning: unused parameter 'argc' [-Wunused-parameter]
D:\Users\Admin\Documents\CodeBlocks Projects\C++ Scratch\main.cpp:3:5: warning: unused parameter 'argv' [-Wunused-parameter]
Linking console executable: bin\Debug\C++ Scratch.exe
Output size is 1.07 MB
Process terminated with status 0 (0 minutes, 0 seconds)
0 errors, 2 warnings (0 minutes, 0 seconds)

If you're like me, you don't want to see any warnings at all. You can fix them by omitting the parameter name and simply going with the type. That's certainly a viable solution:

#include <iostream>

int main(int, char*[])
{
    std::cout<<"Hello, world!\n";
}

But at that point you're already doing something different depending on whether command line arguments are used or not, so what was the benefit again? The only thing I can imagine would be not confusing a rank beginner who might understand functions but not that main is special. On the down side, you'd be tripping up everyone else who's not used to seeing that particular trick used with main.

void main() is just incorrect, and there is ABSOLUTELY no reason to use it! Go and program in your C# then, if you don't like C++!

I didn't say there was a reason to use void main(). I was talking about whether to include an arg list/arg count or not while still having the return type of integer which is completely fine. I also didn't say that I don't like C++ (rusty with it maybe, but I have no bitter feelings towards it). Man, the internet sure makes some people feel big.

@Narue - I think I remember there being some sort of #pragma that you can use do disable certain warnings. This, obviously, isn't a solution but can help clean things up. Any thoughts?

@Narue - I think I remember there being some sort of #pragma that you can use do disable certain warnings. This, obviously, isn't a solution but can help clean things up. Any thoughts?

I'm not a fan of it, unless the warnings are completely stupid. One example of a stupid warning is the "deprecated" warnings Microsoft likes to throw for standard C library functions in their quest to hawk their "safe" string library.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.