473,326 Members | 2,110 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

is main necessaty?

can a c++ program run wiyhout main( )?
Mar 19 '08 #1
4 1657
Banfa
9,065 Expert Mod 8TB
No, main is the entry point for the program. In some environments it appears that you have an alternate entry point (e.g. WinMain under Windows) but even that still has a main hidden away in a library.

A program always needs a main to get it started.
Mar 19 '08 #2
JosAH
11,448 Expert 8TB
A program always needs a main to get it started.
In C++ that's hardly needed anymore, i.e. a main() function has to exist to keep
the linker happy but the entire shebang can be started from a global class object
that does it all, starting from its initialization code. The main method won't even
be called.

kind regards,

Jos
Mar 19 '08 #3
Banfa
9,065 Expert Mod 8TB
In C++ that's hardly needed anymore, i.e. a main() function has to exist to keep
the linker happy but the entire shebang can be started from a global class object
that does it all, starting from its initialization code. The main method won't even
be called.
I am well aware of that, in fact came up with the minor brain teaser what does this code do in C and C++

Expand|Select|Wrap|Line Numbers
  1. struct SomeStruct StructInstance;
  2.  
  3. int main(int argc, char **argp)
  4. {
  5.     return 0;
  6. }
  7.  
To which the answer is in C it does nothing and in C++ it could do anything you don't know without seeing more code. This is not the only case of this, references provide another opportunity for this sort of confusion with similar question of does this function call change the calling functions data?

Additionally in many embedded platforms (especially in C) a main is not strictly speaking required, the argc and argp arguments are often not used for anything and you often have direct access to the cstartup code written in assembler often ending in
[indent]bra $main[/code]

or something similar. There is absolutely nothing preventing you from altering the cstartup code and changing "main" to some other name. You do still have to have and entry point function though.
Mar 19 '08 #4
weaknessforcats
9,208 Expert Mod 8TB
Hmm......

You guys seem to have stumbled upon the design of Microsoft's MFC where the entire application is inside the constructor of the single global CWinApp object.

BTW: Most implementations permit a linker setting for the entry point of the executable. Here you can specify the name of the entry point function. main() is just the default. When your executable loads, the OS calls the entry point function, whatever the name is.
Mar 19 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

192
by: Kwan Ting | last post by:
The_Sage, I see you've gotten yourself a twin asking for program in comp.lang.c++ . http://groups.google.co.uk/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&th=45cd1b289c71c33c&rnum=1 If you the oh so mighty...
45
by: Steven T. Hatton | last post by:
This is a purely *hypothetical* question. That means, it's /pretend/, CP. ;-) If you were forced at gunpoint to put all your code in classes, rather than in namespace scope (obviously classes...
15
by: Fred Zwarts | last post by:
In C++ execution of a program starts already before execution of main(). The initialization of static variables defined outside the scope of main is performed first. I could imagine a program where...
75
by: Beni | last post by:
I have been programming in C for about a year now. It sounds silly, but I never took the time to question why a C(or C++ or Java) program execution begins only at the main(). Is it a convention or...
5
by: Seong-Kook Shin | last post by:
Hi, I'm reading Steve's "C Programming FAQs" in book version, and have two question regarding to Q11.16 ... Also, a `return' from `main' cannot be expected to work if data local to main might be...
13
by: Sokar | last post by:
I have my main function set up as int main(int argv, char *argv) so taht i can read in a variable which is passed to the program on the command line. The problem is that main calls other...
2
by: psuaudi | last post by:
I have a main query that I would like to call two different subqueries. In MS Access, I usually just save the two subqueries as separate queries which are then called by a third separate and main...
28
by: ravi | last post by:
Hello everybody, I am writing a small application which does some work before the user main function starts execution. I am trying to #define the main function. But the problem is that,
11
by: aarklon | last post by:
Hi all, I have heard many discussions among my colleagues that main is a user defined function or not. arguments in favour:- 1) if it is built in function it must be defined in some header...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.