Program without main!!!!

Pages: 12
1
2
3
4
5
6
7
8
9
#include "stdio.h"

#define decode(s,t,u,m,p,e,d) m##s##u##t
#define begin decode(a,n,i,m,a,t,e)

int begin()
{
printf(" hello C++.Com Users");
}
Last edited on
nice :p
I've seen better programs (e.g http://cplusplus.com/forum/articles/15791/ ) in which main is never excecuted. This is imo just changing the name main to begin, by defining some stuff..

Don't get me wrong, it's quite cool, but I've seen better sollutions :)
Sowhats the trick behind it? main is encrypted to begin or something?
anywho here is a combo of the 2 methods discussed here and in the page linked to by kaduuk:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <cstdlib>
#include <cstdio>
#include <iostream>
#include <stdio.h>

using namespace std;

int before_main(void);

int the_actual_program(void);

 /* we need a wrapper function to set it's output to something. if we don't,
    we can't let the program have a return value. the reason why is we have to
    call exit() as part of a function to give this a return value AND bypass
    main(). exit needs to be called with the program's return value. we need
    to assign a global value by a function's return value... that function will
    execute before main(). this function executes another functions that IS the
    actual program bypassing it's own return statement. it never returns, but it
    IS called and executes. and that's our goal.
     */
int before_main(void)
{
    exit(the_actual_program());
    return 0; // this never actually runs. it's here so we can assign before_main()'s value to something
}

int foo_bar = before_main(); // here we make a global assignment which must occur before main()

#define decode(s,t,u,m,p,e,d) m##s##u##t
#define begin decode(a,n,i,m,a,t,e)
int begin(int argc, char *argv[])
{
    puts("We're inside of Main()"); // this never runs.
    getchar(); // this is a pause so tha twhen you run the program, it'll be proven this ever pops up.
    return foo_bar; // we put this here so the compiler doesn't complain we never use it.
}

int the_actual_program(void)
{
    puts("We're NOT inside of Main()"); // this WILL run
    getchar(); // a pause.

    return 0; // yes, this is the program's return value due to exit() in the wrapper function
}


So am I to assume there is anything useful in knowing how to do this other than in bragging rights?
Last edited on
Sowhats the trick behind it?


After the preprocessor does it's thing with the macros, there actually IS a main(). It replaced begin() with main(). Take a look at the definitions. ;)
BTW compilers/linkers may allow to define the entry point to be different from main
Thats cool guys ;)
That's interesting but why would you want a program without main()? I cant think of any advantage to not having a main() your program has to start somewhere it might as well be main right?
yeah!...its just kind of fun :) quirkyusername
Last edited on
Here's a fun idea: instead of trying to write a program without an entry point, try writing one that is physically incapable of returning control to the caller.
1
2
3
4
5
int main()
{
  atexit(main);
  while(1);
}


??
I created a program without main function:
1
2
3
4
int main[]
{
 
};

Ooh! Main function is null there like ur name :-)
U mean this helios?
1
2
void main(){
cout<<"hello helios :)";}
@Null
Is that supposed to compile?
Last edited on
Yes, it compiles with gcc-mingw 4.4.2 . It seems that linker is too stupid to figure out that main is not a function.
EDIT: I googled a little bit and found this: http://www.ioccc.org/1984/mullender.c
Last edited on
Well, it doesn't compile with whatever comes with Visual C++ 2008.
I get 'LINK : fatal error LNK1561: entry point must be defined'
Maybe I found a linker bug?
I was thinking something along the lines of the program crashing the computer.
Pages: 12