Question

I see this interesting question here asking for the possibility of a program without main(). There, I saw eon giving one answer as follows, which works well in C/C++.

#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 ");
}

Can someone explain how the above code works? Isn't really there a main() here or just hiding it from our eyes archly?

Était-ce utile?

La solution 3

The compiler doesn't see "begin" whatsoever. It's completely substituted by the time it gets to the compiler, because macros are simply text-substitutions. Perhaps a helpful diagram to add on to the other great answers.

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

Take a look at m, and see where m is in the argument list.

decode(s,t,u,m,p,e,d)
             ^
             | 
decode(a,n,i,m,a,t,e)

Therefore the first letter is m => m. Then repeat the process, s => a, u => i, t => n.

decode(s,t,u,m,p,e,d)
       ^ ^ ^   
       | | |
decode(a,n,i,m,a,t,e)

Then the resulting letters are "pasted" together with token concatenation, and it looks like main as far as the compiler is concerned.

Autres conseils

Have a close look at the macro: it just spliced together main from the characters in animate and gets them replaced for begin().

After the macro substitution:

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

begin becomes decode(a,n,i,m,a,t,e), which then becomes main. There you have it.

This works because the compiler doesn't really see int begin(){}, The preprocessor will replace begin with main after performing macro substitution and concatenation operator.

First The preprocessor will replace begin with decode(a,n,i,m,a,t,e), after that it will do a rescanning on the replacement list for further replacement, it will find function-like macro decode which will be replaced by it's replacement list after performing the concatenation operator, like this:

m##a##i##n => main

So the compiler will only see the preprocessor output which contain int main(){} and not int begin(){} thus legal code.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top