开发者

what is the use of int i()

开发者 https://www.devze.com 2023-01-15 00:10 出处:网络
#include <stdio.h> #define main() main(){printf(\"hi\");}int i() main()开发者_StackOverflow社区
#include <stdio.h>
#define main() main(){printf("hi");}int i()
main()开发者_StackOverflow社区
{//empty main
}

what is the use of int i()


That's a pretty silly program, but the purpose of the int i() is so that it will compile - the braces at the end:

{//empty main
}

will cause an error if there isn't a function declaration included in the #define statement. If I delete it, gcc gives the error:

testfile.c:4: error: expected identifier or ‘(’ before ‘{’ token

You can use the -E flag to gcc to see why the int i() is necessary - it will show you the output of the preprocessor without compiling.


Think about what you get if you expand the macro main() in the program:

#include <stdio.h>
main(){printf("hi");}int i()
{//empty main
}

The int i() is needed there to make the remaining { ... } part of a syntactically valid function definition.

As for intention, I can only guess that the point of the macro is to replace the existing main with a stub one. It's a bit icky IMO.


In that code main() will be expanded and the result will end with

int i()
{//empty main
}

what is the use of int i()

It makes the output of a very strange and broken macro compilable

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号