tags:

views:

118

answers:

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

what is the use of int i()

+3  A: 

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.

Carl Norum
+1  A: 

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.

Edmund
thanks..i understood.
elle
+2  A: 

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

Henk Holterman
A: 
    #include <stdio.h> 
    #define main() main(){printf("hi");}int i() 
    main() 
    {//empty main 
    } 
     I guess int i() is another preprocessor directive  which should be called in the program after main.
fahad