Calls before main

1
2
3
4
5
6
7
8
9
#include <stdio.h>

int call = puts("Hello..");

int main()
{
    puts("World!");
    return getchar(); //for being able to see the output ;)
}

This outputs;
Hello..
World!


Is there another way of calling functions before main is called?
Last edited on
The initialisers for globals are run before main, that includes class constructors.

The order is not guaranteed across compiled units.
You might also find this example enlightening...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
using namespace std;

void before_main(int v) {cout << "before_main(" << v << ")" << endl;}
void after_main(int v) {cout << "after_main(" << v << ")" << endl;}
void pause() {cout << "hit enter to continue"; cin.get();}

class OutOfMain
{
    public:
    OutOfMain() {before_main(1); before_main(2);}
    ~OutOfMain() {after_main(3); after_main(4); pause();}
};

OutOfMain out_of_main;

int main()
{
    cout << "this is main!" << endl;
    return 0;
}
thanks both!
You might also find it interesting that m4ster r0shi's code could unexepected crash on any given given due to the static initialization order fiasco. (I realize he did this for illustration purposes, but this is an important point nontheless)

Basically if you have some code running before main starts, you must be sure that any variable used in that code has been construted.

There's no way to know whether or not 'cout' has been constructed by the time his before_main function is run. If cout hasn't been constructed, that code will have bizzare sideeffects and/or will crash and burn.

Likewise, you have the same problem when things are destructed. There's no way to guarantee that after_main is run before cout is destructed. If cout is destructed first, you have another potential program explosion.


EDIT: basically what kbw said.

This is pretty much something you should never do. If you want to run code before main starts, don't use any global objects, or call any code that might use global objects.
Last edited on
I see... So.... if I only use functions inside my global variables' constructors and destructors would it be safe? I mean, like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <cstdio>
#include <iostream>
using namespace std;

void before_main(int v) {printf("before_main(%d)\n",v);}
void after_main(int v) {printf("after_main(%d)\n",v);}
void pause() {printf("hit enter to continue\n"); char c; scanf("%c",&c);}

class OutOfMain
{
    public:
    OutOfMain() {before_main(1); before_main(2);}
    ~OutOfMain() {after_main(3); after_main(4); pause();}
};

OutOfMain out_of_main;

int main()
{
    cout << "this is main!" << endl;
    return 0;
}


Or do printf and scanf also rely on the existence of some object(s)?
Last edited on
I always have to laugh a little at these kinds of discussions.

The main() function is precicely defined as the entry point to a program. Hence, if you want something special to happen at the very beginning of your program, it should be at the very beginning of the main function block.
Or do printf and scanf also rely on the existence of some object(s)?


They might. No way to know.

Don't do it.

(in fact I think they do -- pretty sure they use stdout which is a global FILE*, iirc)

+1 Duoas
Duoas++
I was thinking the same thing.
+1 Duoas

If you want to get your head around something in C++, there are far more interesting and productive things to spend your time on.
Or do printf and scanf also rely on the existence of some object(s)?
pretty sure they use stdout which is a global FILE*, iirc


Yeah, that's right. I'm fairly certain that printf("stuff"); is basically fprintf(stdout, "stuff");
fprintf(stdout, "stuff");
Now i'm someone who does not know C well, and can only read it through my knowledge of C++, but how unnecessary does that look!
Topic archived. No new replies allowed.