Take the 2-minute tour ×

The section $3.6.1/1 from the C++ Standard reads,


Now consider this code,

int user_main()
           std::cout << square(i) << endl;
    return 0;
int main_ret= user_main();
int main() 
        return main_ret;

This sample code does what I intend it to do, i.e printing the square of integers from 0 to 9, before entering into the main() function which is supposed to be the "start" of the program.

Have a look at the output here : http://www.ideone.com/Niy0R

I also compiled it with -pedantic option, GCC 4.5.0. It gives no error, not even warning!

So my question is,

Is this code really Standard conformant?

If it's standard conformant, then does it not invalidate what the Standard says? main() is not start of this program! user_main() executed before the main().

I understand that to initialize the global variable main_ret, the use_main() executes first but that is a different thing altogether; the point is that, it does invalidate the quoted statement $3.6.1/1 from the Standard, as main() is NOT the start of the program; it is in fact the end of this program!


How do you define the word 'start'?

It boils down to the definition of the phrase "start of the program". So how exactly do you define it?

share|improve this question

11 Answers 11

up vote 56 down vote accepted

No, C++ does a lot of things to "set the environment" prior to the call of main; however, main is the official start of the "user specified" part of the C++ program.

Some of the environment setup is not controllable (like the initial code to set up std::cout; however, some of the environment is controllable like static global blocks (for initializing static global variables). Note that since you don't have full control prior to main, you don't have full control on the order in which the static blocks get initialized.

After main, your code is conceptually "fully in control" of the program, in the sense that you can both specify the instructions to be performed and the order in which to perform them. Multi-threading can rearrange code execution order; but, you're still in control with C++ because you specified to have sections of code execute (possibly) out-of-order.

share|improve this answer
+1 for this "Note that since you don't have full control prior to main, you don't have full control on the order in which the static blocks get initialized.After main, your code is conceptually "fully in control" of the program, in the sense that you can both specify the instructions to be performed and the order in which to perform them". This also makes me to mark this answer as accepted answer... I think these are very important points, that sufficiently justifies main() as "start of the program" –  Nawaz Jan 25 '11 at 22:02
@Nawaz: note that on top of no full control over initialization order, you have no control over initialization errors: you can't catch exceptions at a global scope. –  André Caron Mar 12 '11 at 20:24

You are reading the sentence incorrectly.


The standard is DEFINING the word "start" for the purposes of the remainder of the standard. It doesn't say that no code executes before main is called. It says that the start of the program is considered to be at the function main.

Your program is compliant. Your program hasn't "started" until main is started. The constructor is called before your program "starts" according to the definition of "start" in the standard, but that hardly matters. A LOT of code is executed before main is ever called in every program, not just this example.

For the purposes of discussion, your constructor code is executed prior to the 'start' of the program, and that is fully compliant with the standard.

share|improve this answer
Sorry, but I disagree with your interpretation of that clause. –  Lightness Races in Orbit Jun 2 '11 at 9:53
I think Adam Davis is right,"main" is more like some sort of coding restrictions. –  laike9m Jul 10 '13 at 6:47

Your program will not link and thus not run unless there is a main. However main() does not cause the start of the execution of the program because objects at file level have constructors that run beforehand and it would be possible to write an entire program that runs its lifetime before main() is reached and let main itself have an empty body.

In reality to enforce this you would have to have one object that is constructed prior to main and its constructor to invoke all the flow of the program.

Look at this:

class Foo

 // other stuff

Foo foo;

int main()

The flow of your program would effectively stem from Foo::Foo()

share|improve this answer
+1. But note that if you have multiple global objects in different translation units, this will get you in trouble quickly since the order in which the constructors are called is undefined. You can get away with singletons and lazy initialization, but in a multithreaded environment, things get very ugly quickly. In one word, don't do this in real code. –  Alexandre C. Jan 24 '11 at 15:07
Whilst you should probably give main() a proper body in your code and allow it to run the execution, the concept of objects outside that start up is what a lot of LD_PRELOAD libraries are based on. –  CashCow Jan 24 '11 at 15:11
@Alex: The standard says undefined, but as a practical matter link order (usually, depending on compiler) contols initiazation order. –  ThomasMcLeod Jan 24 '11 at 18:48
@Thomas: I surely would not even remotely try to rely on that. I also surely wouldn't try to manually control the build system. –  Alexandre C. Jan 24 '11 at 18:50
@Alex: not so important anymore, but back in the day we would use link order to control the build image so as to decrease physical memory paging. There are other side reasons you might want to control initization order even when it doesn't affect program semantics, such as startup performance comparison testing. –  ThomasMcLeod Jan 24 '11 at 19:21

You tagged the question as "C" too, then, speaking strictly about C, your initialization should fail as per section 6.7.8 "Initialization" of the ISO C99 standard.

The most relevant in this case seems to be constraint #4 which says:

All the expressions in an initializer for an object that has static storage duration shall be constant expressions or string literals.

So, the answer to your question is that the code is not compliant to the C standard.

You would probably want to remove the "C" tag if you were only interested to the C++ standard.

share|improve this answer
@Remo.D could you tell us what is in that section. Not all of us have C standard :). –  UmmaGumma Jan 24 '11 at 15:21
Since you are so picky: Alas, ANSI C has been obsolete since 1989. ISO C90 or C99 are the relevant standards to cite. –  Lundin Jan 24 '11 at 15:30
@Lundin: Nobody is ever picky enough :) I was reading ISO C99 but I'm pretty confident it applies to C90 too. –  Remo.D Jan 24 '11 at 16:40
@Ashot. You're right, added the sentence that I think is most relevant here. –  Remo.D Jan 24 '11 at 16:45
@Remo: +1 for providing the info that it's not valid C; i didn't know that. See this is how people learn, sometimes by plan, sometimes by chance! –  Nawaz Jan 24 '11 at 16:53

Section 3.6 as a whole is very clear about the interaction of main and dynamic initializations. The "designated start of the program" is not used anywhere else and is just descriptive of the general intent of main(). It doesn't make any sense to interpret that one phrase in a normative way that contradicts the more detailed and clear requirements in the Standard.

share|improve this answer

The compiler often has to add code before main() to be standard compliant. Because the standard specifies that initalization of globals/statics must be done before the program is executed. And as mentioned, the same goes for constructors of objects placed at file scope (globals).

Thus the original question is relevant to C as well, because in a C program you would still have the globals/static initialization to do before the program can be started.

The standards assume that these variables are initialized through "magic", because they don't say how they should be set before program initialization. I think they considered that as something outside the scope of a programming language standard.

Edit: See for example ISO 9899:1999 5.1.2:

All objects with static storage duration shall be initialized (set to their initial values) before program startup. The manner and timing of such initialization are otherwise unspecified.

The theory behind how this "magic" was to be done goes way back to C's birth, when it was a programming language intended to be used only for the UNIX OS, on RAM-based computers. In theory, the program would be able to load all pre-initialized data from the executable file into RAM, at the same time as the program itself was uploaded to RAM.

Since then, computers and OS have evolved, and C is used in a far wider area than originally anticipated. A modern PC OS has virtual addresses etc, and all embedded systems execute code from ROM, not RAM. So there are many situations where the RAM can't be set "automagically".

Also, the standard is too abstract to know anything about stacks and process memory etc. These things must be done too, before the program is started.

Therefore, pretty much every C/C++ program has some init/"copy-down" code that is executed before main is called, in order to conform with the initialization rules of the standards.

As an example, embedded systems typically have an option called "non-ISO compliant startup" where the whole initialization phase is skipped for performance reasons, and then the code actually starts directly from main. But such systems don't conform to the standards, as you can't rely on the init values of global/static variables.

share|improve this answer

Your "program" simply returns a value from a global variable. Everything else is initialization code. Thus, the standard holds - you just have a very trivial program and more complex initialization.

share|improve this answer

main() is a user function called by the C runtime library.

see also: Avoiding the main (entry point) in a C program

share|improve this answer

Seems like an English semantics quibble. The OP refers to his block of code first as "code" and later as the "program." The user writes the code, and then the compiler writes the program.

share|improve this answer

Yes, main is the "entry point" of every C++ program, excepting implementation-specific extensions. Even so, some things happen before main, notably global initialization such as for main_ret.

share|improve this answer

main is called after initializing all the global variables.

What the standard does not specify is the order of initialization of all the global variables of all the modules and statically linked libraries.

share|improve this answer

Your Answer


