-1

For the following program, some compilers execute it with a warning

3:5: warning: 'int main(int)' takes only zero or two arguments [-Wmain]

and some without any. Can I use main() with one argument?

#include <iostream>

int main(int x)
{
   x = 5;
   std::cout << x;
   return 0;
}
Rakete1111
  • 47,013
  • 16
  • 123
  • 162
  • It is not playing the game. – Ed Heal Mar 18 '17 at 14:12
  • 6
    The C++ standard specifies the arguments to the `main` function. A single `int` argument is not among the valid alternatives. See e.g. [this `main` function reference](http://en.cppreference.com/w/cpp/language/main_function) for more information. – Some programmer dude Mar 18 '17 at 14:13
  • 2
    @Someprogrammerdude, Not among the required alternatives, but still valid if the implementation supports it (source: http://eel.is/c++draft/basic.start.main#2) – chris Mar 18 '17 at 14:13
  • 3
    "What happens with one argument in main()?" Implementation defined behaviour. – George Mar 18 '17 at 14:14
  • Did you get a ***warning*** or an ***error***? You got a *warning*, which means you can continue, but your code is definitely doing something unusual. – abelenky Mar 18 '17 at 14:15
  • Is there any specific need, reason, justification, motivation, incentive or reward for breaking the standard rule? – A.S.H Mar 18 '17 at 14:16
  • 1
    @A.S.H, It's very common to allow a third parameter for environment variables. – chris Mar 18 '17 at 14:17
  • @ abelenky, I got warning. I learned from you that I can use main() with one argument but doing something unusual. – George Theodosiou Mar 18 '17 at 14:18
  • @chris that's allowed by the standard (form (3) in the link posted by SPD), but one and only one parameter isn't. – A.S.H Mar 18 '17 at 14:19
  • Possible duplicate of [Pass only one argument to main() in C](http://stackoverflow.com/questions/42470292/pass-only-one-argument-to-main-in-c) – msc Mar 18 '17 at 14:19
  • @ A.S.H., reason is that I am just learning C++ and explore it. – George Theodosiou Mar 18 '17 at 14:21
  • @GeorgeTheodosiou I understand that, curiosity is a must in the learning process. But I wanted to say that I failed to find any use-cases. :) – A.S.H Mar 18 '17 at 14:24
  • @rsp: That's a C question. This one is about C++. – Christian Hackl Mar 18 '17 at 14:26
  • @A.S.H, The page is a bit misleading. From the current standard draft: *This function shall not be overloaded. Its type shall have C++ language linkage and it shall have a declared return type of type int, but otherwise its type is implementation-defined.* – chris Mar 18 '17 at 14:32
  • @chris Thanks. This is getting really really confusing. Reminds me someone said the "nice thing with standards is that you have so many to chose from". Same for so-called "references" I guess... – A.S.H Mar 18 '17 at 14:39
  • 1
    @A.S.H, In reality, it's probably written like that because there's no implementation I know of that allows one parameter, but there are plenty that allow 3. – chris Mar 18 '17 at 14:44

1 Answers1

4

The C++ standard §3.6.1/2 says the following about the main function:

It shall have a return type of type int, but otherwise its type is implementation-defined. All implementations shall allow both of the following definitions of main:

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

and

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

So it's ultimately up to your compiler to decide whether int main(int x) is OK or not. It goes without saying that using such a main function makes your code non-portable.

Christian Hackl
  • 27,051
  • 3
  • 32
  • 62