Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++
i don't understand the main function?
 

#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 16-Dec-12 0:39am
Edited 26-Dec-12 17:31pm
v3
Comments
zlogdan at 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.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

Refer Below Link
Main Function Description[^]
Link 1[^]
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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"
  Permalink  
Comments
codingghost143 at 16-Dec-12 7:01am
   
thankyou "griff"!
OriginalGriff at 16-Dec-12 7:38am
   
You're welcome!
Sk. Tajbir at 16-Dec-12 14:28pm
   
very clear answer.. 5+
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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!
Smile | :)
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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.
  Permalink  
v3
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

  Permalink  
Comments
zlogdan at 3-Jul-13 10:43am
   
5 stars solution!
Sergey Alexandrovich Kryukov at 3-Jul-13 11:33am
   
Thank you.
—SA
H.Brydon at 3-Jul-13 10:49am
   
+5 to compensate downvote...
 
Question is from 2012. What happened?
Sergey Alexandrovich Kryukov at 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)

  Print Answers RSS
0 OriginalGriff 443
1 Sergey Alexandrovich Kryukov 197
2 PhilLenoir 145
3 RyanDev 125
4 Richard Deeming 120
0 Sergey Alexandrovich Kryukov 6,415
1 OriginalGriff 5,856
2 CPallini 2,473
3 Richard MacCutchan 1,667
4 Abhinav S 1,530


Advertise | Privacy | Mobile
Web03 | 2.8.140821.2 | Last Updated 3 Jul 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100