Where is the function of main () in c++. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 4

Where is the function of main () in c++.

in evry c++ program, main () is the body of a function . but the function is not defined. so pls help me to understand this.

10th Mar 2017, 1:53 PM
Harsh Bhardwaj
Harsh Bhardwaj - avatar
5 Answers
+ 2
thank you ,, sir.
10th Mar 2017, 7:07 PM
Harsh Bhardwaj
Harsh Bhardwaj - avatar
+ 2
So, your question is "How is function declaration of main(), looks like?" main() is reserved as a function in the global namespace. So you can actually declare as many main 'entites' as you want, including among other things, classes, variables, enumerations, *member functions*, and non-member functions *not in the global namespace*. But those main function will be diff. from the main that designates start of program. main can be only used in two ways: 1. int main(){... ... ...} 2. int main(int, char*[]){... ... ...} if main is used like, void main(){...}, or any other form except 1. or 2. it may or may not run depending on the compiler. This was all what I knew, If I helped you in any form don't forget to upvote this answer.
19th Mar 2017, 4:29 AM
Aditya Bando
Aditya Bando - avatar
0
Structure of a C++ program //Preprocessor directive that copies the contents of file at current location #include <iostream> using namespace std; int main (void) //starting point of user code (PS: NO SEMI-COLON AFTER THIS) { //Start block marker //user code } //End block marker int main (void) int implies that function shall return an integer value main it the entry point hook that informs the compiler about the beginning of the user code. No program can work without main() (void) this function receives no values. //alternate form of main int main (int argc, char *agrv[])
10th Mar 2017, 3:34 PM
เคฆเฅ‡เคตเฅ‡เค‚เคฆเฅเคฐ เคฎเคนเคพเคœเคจ (Devender)
เคฆเฅ‡เคตเฅ‡เค‚เคฆเฅเคฐ เคฎเคนเคพเคœเคจ (Devender) - avatar
0
@ aditya bando 1). plz can you tell me the format of main() function declaration in global namespace??? 2). can you explain about the format of int main(ing, char*[]) {..........}
19th Mar 2017, 6:12 AM
Harsh Bhardwaj
Harsh Bhardwaj - avatar
0
You will be surprised to know that you already know the declaration of main(). Let me elaborate more! Since main is a function it should be declared the same way like you declare other function. And here also you are doing exactly the same thing: return_type function_name( parameter list ) { body of the function } You might now be wondering that this is just the declareration part, now where is calling part of the function. The answer to this "could" be that, main does not need it! Since it is designed in this way, and this is the syntax, so developers created the syntax such that main does not require to be called. And even in Documentation they have mentioned it that main can't be called, but actually it can. This is just the double face thing in which the owner says you can't do this, but in reality you are doing it( but only in main itself).
20th Mar 2017, 11:08 AM
Aditya Bando
Aditya Bando - avatar