views:

199

answers:

3

For some reason my Visual Studio 2008 began to show warnings for code like: "int main( int argc, char **argv)", which is really annoying.

The detailed warning ouputs are (you can ignore the line numbers): 1>.\main.cpp(86) : warning C4100: 'argv' : unreferenced formal parameter 1>.\main.cpp(86) : warning C4100: 'argc' : unreferenced formal parameter

I wonder if there are settings in Visual Studio 2008 that have been accidentally changed. Or how should I deal with this warning? Thank you all.

+4  A: 

If the parameters are unreferenced, you can leave them unnamed:

int main(int, char**)
{
}

instead of

int main(int argc, char** argv)
{
}

If you really want just to suppress the warning, you can do so using the /wd4100 command line option to the compiler or using #pragma warning(disable: 4100) in your code.

This is a level 4 warning; if you compile at a lower warning level, you won't get this warning. The warning level is set in the project properties (right-click project, select Properties; on the Configuration Properties -> C++ -> General, set "Warning Level").

James McNellis
Is there any setting in VS2008 that I can play with? Because for codes I wrote earlier, this C4100 warning does show up with I compile them.Thank you.
Jimmie
@Jimmie: Yes; you can change the warning level for your project to some lower warning level (e.g., level 3).
James McNellis
Note that having unnamed parameters in the function definition is OK in C++, but if your compiling as C it's an error (a small irritation sometimes when trying to write code that compiles either way).
Michael Burr
+2  A: 

Warning C4100 is issued at warning level 4 which is not default, so at some point someone probably changed it for your project.

You could change the warning level back, or address the warning more directly.

As James McNellis said, you can silence the warning in C++ by removing the parameter name from the parameter list. However, if the code will be compiled as C code you'll get an error in that case.

The Windows headers define the macro UNREFERENCED_PARAMETER() to help deal with this warning. You could use

UNREFERENCED_PARAMETER( argc);
UNREFERENCED_PARAMETER( argv);

to silence the warning. If you don't want to include windows headers, the macro simply expands to a use of the parameter name in a do nothing expression:

#define UNREFERENCED_PARAMETER(P)          \
    /*lint -save -e527 -e530 */ \
    { \
        (P) = (P); \
    } \
    /*lint -restore */
Michael Burr
I wasn't aware of `UNREFERENCED_PARAMETER`; that's good to know. In the Windows headers, there are two versions. The one enabled by default simply expands to `(P)`. The other one, which you quote, expands to `(P) = (P);`, and is used if `lint` is defined; this one will fail for parameters of const-qualified or nonassignable types.
James McNellis
@James - the more 'complicated' definition is just what my ctags jump happened to land on (I should have scrolled up a bit - especially since I was a bit surprised that the expansion wasn't just `(P)`).
Michael Burr
+4  A: 

If you're not using the command line parameters then the other standard signature for main is:

int main();
Charles Bailey
The other standard signature has a void between those brackets, does it not?
paxdiablo
Yes, in C; no difference in C++ because they're equivalent. Question was tagged C++, so I went with the form of no params that I usually use in C++.
Charles Bailey