Click here to Skip to main content
15,893,337 members
Please Sign up or sign in to vote.
1.09/5 (3 votes)
See more:
i don't understand the main function?


C++
#include <iostream.h>
int main()
{
cout<<"Hello"/n;
 return 0;
}

1) what is this main function?
2) why is this main function used?
3) what is this return 0?
Posted
Updated 26-Dec-12 17:31pm
v3
Comments
zlogdan 3-Jul-13 10:45am    
by the way, in modern C++, since 1998, it is <iostream> not <iostream.h>. Either your tutorial or book are outdated.

Every program needs a main function - it is where your program begins to execute - known as the entry point for the program. (Though in some programs such as Windows Forms, the function is normally hidden from you as you don't need to change it).

When you run your program, it starts with the first line in the main function, and when you leave the main function (either by executing the last instruction in it and falling out through the closing curly bracket, or by executing a return statement) your program terminates.

If you supply an number to the return statement, that that is known as the return code or the error code and it is interpreted by the system (and other apps) as showing if the program succeeded or failed. A value of zero indicates a good, or normal exit and a non-zero value is normally a code which explains why it failed to exit normally. For example, a value of two will often mean "file not found"
 
Share this answer
 
Comments
codingghost143 16-Dec-12 7:01am    
thankyou "griff"!
OriginalGriff 16-Dec-12 7:38am    
You're welcome!
Sk. Tajbir 16-Dec-12 14:28pm    
very clear answer.. 5+
Refer Below Link
Main Function Description[^]
Link 1[^]
 
Share this answer
 
1) what is this main function?
entry point - First line of execution
Main is thread and as the thread start your application start as the thread end your application ends (your application is a process - see task manager it will show your app in process list while it's running.)

2) why is this main function used?
because developers of C++ had set that main will be the block inside which application code will be start working
it could be any if they wanted that time

like you are making your own language you will tell your compiler that suppose START word will your first line of execution when compiler read it then it should start to understand logic and represent it to user.

3) what is this return 0?
the return value from the main() function is used by the runtime library as the exit code for the process. Both Unix and Win32 support the concept of a (small) integer returned from a process after it has finished.
so,.. in short it's a indication about termination of program
suppose you are working with a program that call other exe (which can return value)
and you are receiving it then decide whether to do next step or not based on that result.
see example
http://stackoverflow.com/questions/5728196/how-to-get-return-value-from-exe-and-restart-it[^]


Hope it has solve your difficulty
Happy Coding!
:)
 
Share this answer
 
v2
In Any C++ application, starts with main.

int main()
{
return 0;
}

main is the entry point in C++.when ever you start execute the application.
ITs starting from main.
 
Share this answer
 
v3
 
Share this answer
 
Comments
zlogdan 3-Jul-13 10:43am    
5 stars solution!
Sergey Alexandrovich Kryukov 3-Jul-13 11:33am    
Thank you.
—SA
H.Brydon 3-Jul-13 10:49am    
+5 to compensate downvote...

Question is from 2012. What happened?
Sergey Alexandrovich Kryukov 3-Jul-13 11:32am    
Thank you, Brydon.
It's OK; I suspect some members take a guess on a downvote and consider it appropriate to "revenge"... :-)

Sometimes, I see quite a questionable post of some other member (no, this is NOT OP), so it's really interesting to see what else such a strange guy posted. One typical case is: an author of fully ignorant "question" appears to be the author of tens of "answers"; no wonder they are often fake. For example, I recently saw the question "How to manage a company" and wanted to see, does it look like a clinical case or not, by other posts (no, by other posts the guy seems all right; hopefully, it was a troll's joke :-).

—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900