void and int

Pages: 12
1
2
3
4
5
6
7
8
9
10
11
12
void main()
{

}

OR

int main()
{

return 0;
}


which one is better?
and tell me the reason..
thank's
Last edited on
int main
The C++ standard mandate it
I think both of them have the same function. void main() is used as a main function for C languages standard while int main() is used as a main function for C++ languages standard. So it's depend upon what language do you use ...
No, void main is not valid C either.

int main is the only valid return type for main in C or C++.
Isn't this somewhat of a black-hole discussion? Some people prefer void main, some prefer int main, and each group has strong reasons why it does or doesn't matter and have e tendancy to get rather argumentive about it.

Myself, I'm agree that int main is the way to go.
I don't see what "strong arguments" there could be for void main(). It is not valid C++ and will therefore not compile with a standard-compliant C++ compiler. It's not even an option.
Last edited on
+1 Athar.

There are no opinions in this. There is only valid C++ and that which is not valid C++. void main() is not valid C++.
My apologies... I could have swore I'd seen void main() code samples before, but apparently they either a) weren't for c++ or b) weren't compiled code. And I thought I had seen at least one discussion on the pros/cons of each, but again it must have been something else.

thanks for clarifying.
Some compilers (mainly Windows ones I think) allow for void main() and people tend to use it in early tutorials because it avoids explaining return codes, but it's not allowed by the propper C++ standard, so isn't guaranteed to be portable between compilers.

I don't think any of the main compilers enforce the C++ standard 100%.

That said, go with int rather than void. It doesn't make that much difference to most programs, but it's better to be standards compliant when not doing so saves you nothing. And having main return based on success/failure is a good thing. :)
Last edited on
Some compilers allow nonstandard behaviour but you can force them to follow the standards.
If you see void main you know that the code/tutorial you are reading is rubbish
Last edited on
why we use int main() when char main is smaller than it
any reason ?

1
2
3
4
5
6
7
8
9
#include<iostream>
using namespace std;

int main()
{
	cout << sizeof(main());
	cin.ignore();
return 0;
}

above = 4 bytes

1
2
3
4
5
6
7
8
9
#include<iostream>
using namespace std;

char main()
{
	cout << sizeof(main());
	cin.ignore();
return 0;
}

and abov is 1 bytes


i just want to know, i believe that to be an expert i should know at the very bottom of the topic
i searched it, but i don't get the reason
and, i ask it because my college recommend void main()
maybe sometimes i can replies if other people ask it to me..
i'm sorry if i'm bothering you all
thank's
Last edited on
When I say "There are no opinions in this", what I mean is that it is a defined part of the language according to the standards committee so there is no ambiguity. You can and will meet code that uses void main() but it is not proper C++ according to the defined standards.
it avoids explaining return codes

That's akin to trying to teach someone to fish without bothering them with the details of a fishing pole. In any case, glad to see that I wasn't 100% off... when I see lazy code like void main I have a tendancy to assume that if the programmer can't get something like that the way it "should be", chances are the rest of the code won't have all that much integrity either.

Again, thanks for the clarification... for a minute there I thought I might have been losing it, remembering things I've never seen.

why we use int main() when char main is smaller than it
any reason ?


I think we use int because the 32 bit integer is the "native type". When something executes, it is expected to return a value based on that process, and that value is expected to be in the form of a 32 bit integer (or 16 on 16-bit systems). I'm not positive (still fairly new to this myself), but that's what I gather.

cheers

Some compilers allow nonstandard behaviour but you can force them to follow the standards.
If you see void main you know that the code/tutorial you are reading is rubbish

yes, i've to know the reason to claim that it's trash
because i don't want to be a machine-like ppl that only know true or false

and maybe the conclusion is int main() because it is valid in many(all maybe) compilers
so, it takes portability advantages - cmiiw
Actually, void main is valid C. :-@

C++ fixed that loophole and mandates only int main.

Read more:
http://www.gidnetwork.com/b-66.html
http://homepage.ntlworld.com/jonathan.deboynepollard/FGA/legality-of-void-main.html
maybe the conclusion is int main() because it is valid in many(all maybe) compilers


Correct. It is mandated by ISO/ANSI standards, and therefore needs to be compliant with all compilers that are as well ANSI/ISO compliant (any compiler we use should be). We should always code within this standard for many reasons, one of them being portability.
dandy: what you're finding there is that the size of a char is only 1 byte (8 bits) as it's ASCII, 256 values is enough. An int takes up 32 bits (4 bytes), because it can hold 2^32 values (somewhere in the region of 4.2 billion). It's looking at the return type, not the actual function.
Last edited on
what is the return type is for?
what it's for compatibility with OS such as jleach explained?
This is the one value that the program can return to the operating system as an "exit" code.
0 usually means the program completed successfully. non-zero usually means the program
did not complete successfully.
You should be able to read the exit value from a program through your IDE. Create and run a console program... when the program exits normally, you may see a notification such as "program blah.exe terminated with status 0".

Now run it again, but instead of exiting normally, close the console through the X or by killing the process, etc, while the console is waiting for some input. You should see something like "program blah.exe terminated with status -6505424" (that number may vary, I forget what the actual code is for that type of exit").

I use Code::Blocks anyway, and that gives the termination status, though I suspect other IDEs should as well.
Pages: 12