Page 1 of 1
void main & int main in C++
Posted: Thu Nov 11, 2010 10:49 pm
by Face
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.
Re: void main & int main in C++
Posted: Fri Nov 12, 2010 12:22 am
by Neo
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.
Re: void main & int main in C++
Posted: Fri Nov 12, 2010 12:07 pm
by Nipuna
Now I understand very well Buddy.
Is It OK to use?
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
with
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.
Re: void main & int main in C++
Posted: Fri Nov 12, 2010 2:35 pm
by Neo
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.
Re: void main & int main in C++
Posted: Fri Nov 12, 2010 2:40 pm
by Nipuna
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
Re: void main & int main in C++
Posted: Fri Nov 12, 2010 2:56 pm
by Neo
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.
Re: void main & int main in C++
Posted: Fri Nov 12, 2010 9:45 pm
by Nipuna
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.

Re: void main & int main in C++
Posted: Sat Nov 13, 2010 1:57 pm
by Nipuna
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
Re: void main & int main in C++
Posted: Wed Nov 17, 2010 1:42 pm
by LeeShane
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>
Re: void main & int main in C++
Posted: Mon Apr 11, 2011 10:04 pm
by Nipuna
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
in main function in C++ and we end the main function with
because using
we are expecting a return from that function actually the return must be an integer value. And we end the function using
because we don't want to return anything.
And we use
this doesn't return anything so we don't want
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