void main & int main in C++

C, C++, Visual C++, C++.Net Topics
Post Reply
User avatar
Face
Major
Major
Posts: 727
Joined: Thu Feb 18, 2010 5:06 pm
Location: SRI LANKA.KANDY.

void main & int main in C++

Post by Face » Thu Nov 11, 2010 10:49 pm

NEO I got a problem about int void & int main in C++ when I am trying to make some little codes.You told me about this last post.In my little code.
Can you please what is the difference between that 2.
You told me that I have to use return in void main.& I have to use return x in int void.
please explain that too.
User avatar
Neo
Site Admin
Site Admin
Posts: 2642
Joined: Wed Jul 15, 2009 2:07 am
Location: Colombo

Re: void main & int main in C++

Post by Neo » Fri Nov 12, 2010 12:22 am

okay... I explained the same to Nipuna about a month back over LankaChat.

Let me explain you this with an example.

Say you have two programmes compiled as EXEs named as MyCode1.exe and MyCode2.exe.
  • MyCode1 is going to execute MyCode2.
  • MyCode2 is going to add two numbers and return the value to MyCode1
Here are the codes.

MyCode1.exe

Code: Select all

#include<iostream>

using namespace std;

void main()
{
    int ret;
   
    ret = system("C:\\MyCode2.exe");

    count << "MyCode1 returned " << ret << endl;
}

MyCode2.exe

Code: Select all

#include<iostream>

using namespace std;

int main()
{
    int a = 10, b = 15, sum;

    sum = a + b;

    return sum;   
}
Can you see that MyCode1 expects a return value from MyCode2. This is where we need to define "main" with a return type. If you do not return such a thing to a calling process, the correct way to define main is void main. void means, "main" isn't going to return anything.
User avatar
Nipuna
Moderator
Moderator
Posts: 2729
Joined: Mon Jan 04, 2010 8:02 pm
Location: Deraniyagala,SRI LANKA

Re: void main & int main in C++

Post by Nipuna » Fri Nov 12, 2010 12:07 pm

Now I understand very well Buddy.

Is It OK to use?

Code: Select all

int main() 
Because I am used to it so I always code like that.

And I am Using Dev C++ and it can't compile this code

Code: Select all

    #include<iostream>

    using namespace std;

    void main()
    {
        int ret;
       
        ret = system("C:\\MyCode2.exe");

        count << "MyCode1 returned " << ret << endl;
    }
I have to replace

Code: Select all

void main ()
with

Code: Select all

int main ()
It says this
6 C:\Users\Nipuna Dananjaya\Desktop\Untitled1.cpp `main' must return `int'
C:\Users\Nipuna Dananjaya\Desktop\Untitled1.cpp In function `int main(...)':
11 C:\Users\Nipuna Dananjaya\Desktop\Untitled1.cpp invalid operands of types `<unknown type>' and `const char[18]' to binary `operator<<'
What is this? This is a Major reason why i didn't understood void main well.
User avatar
Neo
Site Admin
Site Admin
Posts: 2642
Joined: Wed Jul 15, 2009 2:07 am
Location: Colombo

Re: void main & int main in C++

Post by Neo » Fri Nov 12, 2010 2:35 pm

If your compiler can handle void main and there is nothing you need to return, I think void main is fully okay to use. In my experience, I never found any problem with that in both Windows, Linux, etc…

However, if you use a C/C++ standard compiler (C99 compliant), then you will get an error message stating that you will not be able to compile with void main. Dev C++ gives “`main’ must return `int’ “.
If that is the case, use int main since that compiler can’t handle it.

As long as your compiler can handle void main such as Microsoft C++ compiler and gcc, I don’t see any reason for a stack error to be occurred when processing either epilog or prolog. If your compiler doesn’t give you an error for “void main” and produce an error/crash while running (starting, running or ending), then you are using a bad compiler. Stop using it :) . It would have produced an error while compiling just as Dev C++ compiler, if it can’t handle “void mian”.

It is always good to try coding according to the standard and the Compiler must be responsible to maintain the standard.

However, I agree "int main" is the recommended way for C/C++.

Modern languages like C# and Java supports void main. See http://en.wikipedia.org/wiki/Main_funct ... ramming%29 for details.
User avatar
Nipuna
Moderator
Moderator
Posts: 2729
Joined: Mon Jan 04, 2010 8:02 pm
Location: Deraniyagala,SRI LANKA

Re: void main & int main in C++

Post by Nipuna » Fri Nov 12, 2010 2:40 pm

Thanks Buddy

You mean My Compiler can't handle Void Main () ?

If so then I will try CodeBlock. But The Problem is those are No easy to use like Dev C++. Dev C++ can compile and Run a Code in few clicks. :)

Thanks
User avatar
Neo
Site Admin
Site Admin
Posts: 2642
Joined: Wed Jul 15, 2009 2:07 am
Location: Colombo

Re: void main & int main in C++

Post by Neo » Fri Nov 12, 2010 2:56 pm

Dev C++ is the best free compiler I know (though I use Microsoft C++ and some other embedded C/C++ compilers). This int main and void main isn't a big thing at all to change the compiler. Simply nothing.
User avatar
Nipuna
Moderator
Moderator
Posts: 2729
Joined: Mon Jan 04, 2010 8:02 pm
Location: Deraniyagala,SRI LANKA

Re: void main & int main in C++

Post by Nipuna » Fri Nov 12, 2010 9:45 pm

Now I understand very well Buddy.

I did some experiments too. Those were. I added 1 to 100 at the end of the return 0; and i ran the program1 then it said myprogram2 returned 1 when i add 100 it said returned 100. I added characters to check then it didn't work. now i understand that I have to give a value not a character.

But My Compiler doesn't support int main(); that is the Problem. :?

Thanks for Helping ME. :)
User avatar
Nipuna
Moderator
Moderator
Posts: 2729
Joined: Mon Jan 04, 2010 8:02 pm
Location: Deraniyagala,SRI LANKA

Re: void main & int main in C++

Post by Nipuna » Sat Nov 13, 2010 1:57 pm

Buddy

I can remember that you gave me another code in Lankatalk. Can you give it again to Have a look?

That code was to calculate perimeter.

Thanks
LeeShane
Corporal
Corporal
Posts: 3
Joined: Wed Nov 17, 2010 1:17 pm

Re: void main & int main in C++

Post by LeeShane » Wed Nov 17, 2010 1:42 pm

G-sparkZ wrote:NEO I got a problem about int void & int main in C++ when I am trying to make some little codes.You told me about this last post.In my little code.
Can you please what is the difference between that 2.
You told me that I have to use return in void main.& I have to use return x in int void.
please explain that too.
Void would be written when function can not return any value.
its generic datatype.
if function return value then you have define return type as here int type otherwise it will show an error.
it can be any data type.

--------------------------
<link removed>
Last edited by Neo on Wed Nov 17, 2010 3:15 pm, edited 1 time in total.
Reason: Link removed by admin
User avatar
Nipuna
Moderator
Moderator
Posts: 2729
Joined: Mon Jan 04, 2010 8:02 pm
Location: Deraniyagala,SRI LANKA

Re: void main & int main in C++

Post by Nipuna » Mon Apr 11, 2011 10:04 pm

After a Long Time Something to Say

Today Evening it was raining heavily with Thunderings.

So I decided to Read my Printed C++ tutorial because I can't do anything with my Computer or TV or anything since it was Thundering.

And I read that tutorial to get an Idea about more in Programing Theory.

Then I finished reading Functions

And I understood About Returning More.

Like this

We use

Code: Select all

int main ()
in main function in C++ and we end the main function with

Code: Select all

return 0;
because using

Code: Select all

int main ()
we are expecting a return from that function actually the return must be an integer value. And we end the function using

Code: Select all

return 0;
because we don't want to return anything.

And we use

Code: Select all

void main ()
this doesn't return anything so we don't want

Code: Select all

return 0;

And in C++ programs we make more functions to do things like this

Code: Select all

int do (int a, int b, int c)
{
a=10;
b=20;
c=a*b;

return (c);

}
Using that when we call this function we get the return value of do function which is c.

So far As I know I met very similar Syntax in Java (Very Similar to C++).
So I thinks these function things would be same too.


But What I still Don't get is "Why Did I waste So much time of Neo by asking such Simple Thing" :?




Thanks
Post Reply

Return to “C/C++ Programming”