0

It is okay to use main() instead of int main() in C but this is an outdated practice. But I have seen people use main() instead of int main() in C++. If they use main(), do they mean void main() as in this question?

Community
  • 1
  • 1
Ka Wa Yip
  • 2,546
  • 3
  • 22
  • 35
  • 2
    AFAIK, it's nonstandard and will cause compilation errors if your warnings are high enough. I would imagine compilers support it for legacy reasons, such as the fact that C++ is compatible with ANSI C, which at the time needed to support legacy C code that didn't have the initial `int` either, so it probably cascaded into modern C++ compilers. But that's all speculation on my part and I might be wrong. – Qix - MONICA WAS MISTREATED Feb 17 '17 at 04:40
  • 1
    If it's from C, the implicit return type would be `int`, not `void`, so it would be more correct than `void main`. Still ugly though; implicit return types have been frowned upon since the first ANSI C standard. – ShadowRanger Feb 17 '17 at 04:43
  • Even in C, modern C compilers only accept `main()` — rather than `int main()` — when working in a backwards-compliant mode. GCC 5.x and later defaults to C11 mode where the 'plain `main()`' is not acceptable, whereas GCC 4.x and earlier defaulted to C90 mode where it is OK. It (plain `main()`) hasn't been OK in 'standard' (strict) C++ since the early 90s. Some compilers may still allow it (again, for reasons of backwards compatibility), but it hasn't been OK. So, anybody writing plain `main()` in modern code is doing it wrong. Anybody encountering plain `main()` in old code should update it. – Jonathan Leffler Feb 17 '17 at 04:44
  • @ShadowRanger that's what I thought but I may think using `main()` may mean something new in C++ like `void main()`. Cannot find the implicit meaning in C++ over the internet. – Ka Wa Yip Feb 17 '17 at 04:47
  • No, under compilers like GCC and Clang, this is an extension to C++ to preserve the old C behaviour. Not a change like `void main`. – chris Feb 17 '17 at 05:09

2 Answers2

0

interesting . i understood your question peter. but now-a-days no one uses main() declaration without a prototype. the question you have asked is about old c standard but we are in c standard 11. so dont expect the old main() declaration .in old c standard if the function returns nothing then they used to assume a return of an integer . hope this explains your question. if u have more doubts please refere check out the link http://ee.hawaii.edu/~tep/EE160/Book/chapapx/node7.html i think it used to return so in the same way c++ standards might support the old standard.

the most common declarations you find now is
int main(int argc, char** argv) or int main() or void main()

venky513
  • 321
  • 2
  • 15
-1

When using a current version of GCC, both of these compile to exactly the same output:

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

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

I'd therefore conclude that the int is implied.

Scovetta
  • 3,112
  • 1
  • 14
  • 13
  • Which 'current version of GCC' are you talking about? It isn't a 5.x or 6.x compiler, anyway. Or maybe you mean the `g++` compiler from GCC (as opposed to `gcc`). With `g++` and no options, it does compile without complaint with `g++` 6.3.0; the same code with `gcc` 6.3.0 does not compile without warnings. – Jonathan Leffler Feb 17 '17 at 04:45
  • Sorry, you're right -- g++, specifically: `g++ (Ubuntu 5.4.0-6ubuntu1~16.04.4) 5.4.0 20160609` – Scovetta Feb 17 '17 at 04:52
  • I was a bit surprised to find it compiled warning-free under default options with G++; I knew it wouldn't under GCC. It takes a `-pedantic` or `-Wall` or something similar to elicit a warning from `g++` — even `-std=c++11` or `-std=c++14` is not sufficient. I guess it is still allowed because it is still prevalent in practice, and it is in part prevalent in practice because it is still allowed under default options. Though what anyone is doing compiling without `-Wall` or more stringent options is anybody's guess. – Jonathan Leffler Feb 17 '17 at 04:55