-5

can I write c++ main function without return integer value? I am using visual studio 2010.

#include <iostream>
using namespace std;

int main()
{
    cout <<"In main function" << endl;  
    //return 0; 
}    `
πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190

3 Answers3

4

Yes, since the release of C99, you can skip the return value of main().

Let's look at C++11

3.6.1

A return statement in main has the effect of leaving the main function (destroying any objects with automatic storage duration) and calling std::exit with the return value as the argument. If control reaches the end of main without encountering a return statement, the effect is that of executing

return 0;
Arun A S
  • 6,421
  • 4
  • 29
  • 43
3

According to the C++ Standard (3.6.1 Main function)

5 A return statement in main has the effect of leaving the main function (destroying any objects with automatic storage duration) and calling std::exit with the return value as the argument. If control reaches the end of main without encountering a return statement, the effect is that of executing

return 0;

Thus the shortest C++ program is

int main() {}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
1

With the VS2010 compiler, you actually can declare it as void main() if you're not interested in returning a value.

Though this isn't a C++ standard compliant feature.

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
Cristik
  • 30,989
  • 25
  • 91
  • 127
  • 4
    You are wrong. main shall have return type int. – Vlad from Moscow Apr 30 '15 at 17:57
  • 3
    The compiler that comes with VS 2010 allows declaring `main` as `void`. – Cristik Apr 30 '15 at 18:02
  • @Cristik: That is irrelevant. – Lightness Races in Orbit May 05 '15 at 16:36
  • @LightnessRacesinOrbit: the asker said that he is using VS2010, so I gave an answer that suits that environment, as VS allows you to declare `void` mains, thus the developer doesn't need to type the `return` statement. – Cristik May 05 '15 at 16:37
  • That you don't need to type the `return` statement is unrelated to this notion that you can change the return _type_. The two are distinct considerations. C++ allows you to omit `return 0`; it does not allow you to change `int` to `void`. I don't really care that some implementation has an extension: the question is clearly asking about C++ (with IDE provided as additional, contextual information). – Lightness Races in Orbit May 05 '15 at 16:47
  • 1
    That's your opinion and I respect it, I just wanted to let the asker know that there is a workaround to his `can I write c++ main function without return integer value?` question. Also the good people that edited my answer added a clarification that this is not standard, so I don't think this answer would hurt anyone. That doesn't mean though that people should agree with it :) – Cristik May 05 '15 at 17:20