cmacoscompilationc-preprocessorpreprocessor

Can a C program execute successfully if main() is defined as a macro?


We know that main() is the entry point of a C program. But what happens if we define main as a macro, like this?

#define main something_else

Solution

  • Barring any linker magic, your program would fail to link due to a missing main function.

    For example if this was your full code:

    #define main something_else
    
    int main()
    {
        return 0;
    }
    

    The output of the preprocessor would be this:

    int something_else()
    {
        return 0;
    }
    

    And you'd get a linker error:

    /usr/lib/gcc/x86_64-redhat-linux/4.8.5/../../../../lib64/crt1.o: In function `_start':
    (.text+0x20): undefined reference to `main'
    collect2: error: ld returned 1 exit status